Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法在popup.js中执行chrome.storage.local.set的回调_Javascript_Html_Jquery_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 无法在popup.js中执行chrome.storage.local.set的回调

Javascript 无法在popup.js中执行chrome.storage.local.set的回调,javascript,html,jquery,google-chrome,google-chrome-extension,Javascript,Html,Jquery,Google Chrome,Google Chrome Extension,我正在编写一个简单的chrome扩展,它包含一个HTML弹出窗口(popup.HTML)和一个相关的popup.js文件。当用户单击popup.js中的按钮时,我想使用chrome.storage.local.get和chrome.storage.local.set检索并设置一个值。在这个过程之后,我想通知用户更新已经完成(使用一些简单的jQuery)。但是,我将这个jQuery更新代码放在了我的chrome.storage.local.set的回调中,当单击按钮时,动态更新从未发生,因此,我怀

我正在编写一个简单的chrome扩展,它包含一个HTML弹出窗口(
popup.HTML
)和一个相关的
popup.js
文件。当用户单击
popup.js
中的按钮时,我想使用
chrome.storage.local.get
chrome.storage.local.set
检索并设置一个值。在这个过程之后,我想通知用户更新已经完成(使用一些简单的jQuery)。但是,我将这个jQuery更新代码放在了我的
chrome.storage.local.set
的回调中,当单击按钮时,动态更新从未发生,因此,我怀疑回调从未执行过。我相信这是因为我将通知片段放在
chrome.storage.local.get
回调中,并且能够显示正确的消息:

popup.html

<html>
   <head>
      <title>Test Extensio</title>
      <script src='jquery.min.js'></script>
      <script src='popup.js'></script>
  </head>
  <body>
       <button class='get-notified'>Run Test</button>
       <div class='update-display'></div>
  </body>
</html>
对于上面的代码示例,计数器消息永远不会显示。但是,当我删除
chrome.storage.local.set
并在
chrome.storage.local.get
的回调中运行更新时,它确实起作用:

chrome.storage.local.get(['counter'], function(result){
    var new_count = result.counter === undefined ? 1 : parseInt(result.counter)+1
    $('.update-display').html(`The count is: ${new_count}`)
    //this works
});
为什么
$('.update display').html(…)
在第一个回调中工作,即
chrome.storage.local.get
的回调,而不是
chrome.storage.local.set
的内部回调

我的
manifest.json
文件:

{
  "manifest_version": 2,
  "name": "Test extension",
  "description": "A Description coming soon",
  "version": "2.0",
  "browser_action": {
     "default_icon": "/images/icon.png",
     "default_popup": "popup.html"
   },
   "background" : {
     "scripts" : ["jquery.min.js", "popup.js"]
   },
   "permissions": [
   "storage",
   "activeTab",
   "declarativeContent",
   "webNavigation",
   "unlimitedStorage",
   "tabs",
   "cookies",
   "webRequest",
   "webRequestBlocking",
   "http://*/",
   "https://*/",
   "http://fonts.googleapis.com/",
   "https://fonts.googleapis.com/"

 ],

   "web_accessible_resources": ["popup.html"],
  ....
 }
有人知道为什么会发生这种令人困惑的行为吗?多谢各位

.set
,它返回一个承诺

您只需执行以下操作:

chrome.storage.local.set({'counter':new_count}).then(function(){
    $('.update-display').html(`The count is: ${new_count}`)
});

对于那些感兴趣的人,我发现了问题:我在我的
chrome.storage.local.get
上面添加了一个
window.close()
,这将不可避免地在用户单击按钮一定次数后关闭弹出窗口。我只是简单地移动了
窗口。在我原来的回调中关闭
,chrome.storage.local.set
工作正常:

/*this did not work
if (somecondition){
    window.close();
}
*/
chrome.storage.local.get(['counter'], function(result){
   var new_count = result.counter === undefined ? 1 : parseInt(result.counter)+1
    chrome.storage.local.set({'counter':new_count}, function(){
        $('.update-display').html(`The count is: ${new_count}`)
        //this worked
        if (somecondition){
            window.close();
        }
    });
 });

谢谢你的回答。奇怪的是,我得到了一个错误:“错误处理响应:TypeError:无法读取未定义的属性'then',带有
chrome.storage.local.set({'counter':new_count})。然后(function(){
被突出显示。你知道为什么吗?我在清单中指定了
存储
。事实上,我很抱歉,我终于解决了这个问题。如果你有兴趣,我发布了一个答案。@dave,你的答案完全错了,应该删除或完全重写。这会把WebExtensions和chrome扩展混淆,w它使用回调,不返回任何内容。
/*this did not work
if (somecondition){
    window.close();
}
*/
chrome.storage.local.get(['counter'], function(result){
   var new_count = result.counter === undefined ? 1 : parseInt(result.counter)+1
    chrome.storage.local.set({'counter':new_count}, function(){
        $('.update-display').html(`The count is: ${new_count}`)
        //this worked
        if (somecondition){
            window.close();
        }
    });
 });