Google chrome extension Chrome扩展-Chrome.Extension.sendRequest返回空响应

Google chrome extension Chrome扩展-Chrome.Extension.sendRequest返回空响应,google-chrome-extension,Google Chrome Extension,我有一个Chrome扩展,我正在尝试添加带有启用/禁用选项的功能 我在popup.html中有复选框,一旦单击,就会将复选框的值写入localStorage。然后在我的内容脚本中,我发送一个值请求 我以前只有一个请求,而且效果很好。然后我尝试添加另一个请求,无论我尝试了什么,它都不起作用,即使我注释掉了另一个请求。以下是我的内容脚本中的代码: autopoke = 'default'; secondbutton = 'default'; chrome.extension.sendRequest

我有一个Chrome扩展,我正在尝试添加带有启用/禁用选项的功能

我在popup.html中有复选框,一旦单击,就会将复选框的值写入localStorage。然后在我的内容脚本中,我发送一个值请求

我以前只有一个请求,而且效果很好。然后我尝试添加另一个请求,无论我尝试了什么,它都不起作用,即使我注释掉了另一个请求。以下是我的内容脚本中的代码:

autopoke = 'default';
secondbutton = 'default';

chrome.extension.sendRequest({localstorage: "autopoke"}, function(response)
{ 
if (response.autopoke == "true")
        {
        autopoke = true;
        }
})

chrome.extension.sendRequest({localstorage: "secondbutton"}, function(response)
{ 
console.log(response) //for debugging
if (response.secondbutton == "true")
        {
        secondbutton = true;
        }
})
无论“secondbutton”localStorage变量是否为true,第二个请求的输出都为空。 Chrome控制台的输出也是一个空白对象。我尝试将该行移动到第一个请求,它输出了一个对象,其中某个地方的“autopoke=false”

为了确保设置了localStorage值,我将其添加到popup.html中:

alert(localStorage.secondbutton);
当我点击弹出窗口时,会出现一个警告,说true。我真的搞不懂这一点,我甚至尝试过setTimeout()等待几秒钟,但结果是一样的:localStorage“secondbutton”的输出为空

background.html:

<html>

<script type='text/javascript'>
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {

if (request.localstorage == "autopoke") {
sendResponse({autopoke: localStorage.autopoke});
}
else {
sendResponse({}); // snub them.
}    
});
</script>

</html>

chrome.extension.onRequest.addListener(
功能(请求、发送方、发送响应){
if(request.localstorage==“autopoke”){
sendResponse({autopoke:localStorage.autopoke});
}
否则{
sendResponse({});//怠慢它们。
}    
});
popup.html:

<html>
<head>

<style type='text/css'>
    body
    {
        font-size:10pt;
        width:220px;
        font-family: "Verdana";
        font-weight: bold;
        text-shadow: 2px 2px 2px green;
    }

</style>

<script type='text/javascript'>
    function update()
    {
    localStorage.autopoke = document.myform.check.checked;
    localStorage.secondbutton = document.myform.secondbutton.checked;
    }
</script>
</head>
<body>
    <form name='myform'>
    <table border="0">
        <tr>    
            <td>
                <label for='check'>Autopoke:</label> 
            </td>   
            <td>    
                <input type='checkbox' id='check' onclick='update()'/>
            </td>
        </tr>

        <tr>
            <td>
                <label for='secondbutton'>Additional Poke Button:</label> 
            </td> 
            <td>
                <input type='checkbox' id='secondbutton' onclick='update()'/>
            </td>
        </tr>

    </table>
    </form>
    <script type='text/javascript'>
    if( localStorage.autopoke == "true")
    {
      document.myform.check.checked=true;
    }
    if ( localStorage.secondbutton == "true" )
    {
      document.myform.secondbutton.checked=true;
    }
    </script>
</body>
</html>

身体
{
字号:10pt;
宽度:220px;
字体系列:“Verdana”;
字体大小:粗体;
文本阴影:2px2px2px绿色;
}
函数更新()
{
localStorage.autopoke=document.myform.check.checked;
localStorage.secondbutton=document.myform.secondbutton.checked;
}
自动对焦:
附加戳按钮:
if(localStorage.autopoke==“true”)
{
document.myform.check.checked=true;
}
如果(localStorage.secondbutton==“true”)
{
document.myform.secondbutton.checked=true;
}

好的,让我们看看这里发生了什么。您首先使用
localstorage=“autopoke”
发送请求。请求处理程序中的if语句保持不变,并发回
localStorage.autopoke
值。现在,当您发送
localstorage=“secondbutton”
时,为什么它不响应任何内容?嗯,在代码中没有返回值的地方。尝试下面的代码,它应该可以做到这一点

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) 
{
    if (request.localstorage == "autopoke")
    {
        sendResponse({autopoke: localStorage.autopoke});
    }
    else if (request.localstorage == "secondbutton")
    {
        sendResponse({secondbutton: localStorage.secondbutton});
    }
    else
    {
        sendResponse({}); // snub them.
     }    
});

我们需要查看背景页面javascript,这可能就是问题所在。