Javascript 无法在popup.js中执行chrome.storage.local.set的回调
我正在编写一个简单的chrome扩展,它包含一个HTML弹出窗口(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的回调中,当单击按钮时,动态更新从未发生,因此,我怀
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();
}
});
});