Google chrome extension 无法在chrome存储中保存
我使用以下代码在chrome.storage中加载和保存值:Google chrome extension 无法在chrome存储中保存,google-chrome-extension,google-chrome-storage,Google Chrome Extension,Google Chrome Storage,我使用以下代码在chrome.storage中加载和保存值: $(document).ready(function() { $( "#filterPlus" ).click(function() { SaveSwitch("plus1","#filterPlus","plus1"); }); } function SaveSwitch(propertyName, imageId, imageSrc) { chrome.storage.sync
$(document).ready(function()
{
$( "#filterPlus" ).click(function()
{
SaveSwitch("plus1","#filterPlus","plus1");
});
}
function SaveSwitch(propertyName, imageId, imageSrc)
{
chrome.storage.sync.get(propertyName, function (result) {
var oldValue = result.propertyName;
alert('GET:old='+oldValue);
if (oldValue==null)
{
oldValue=false;
}
var newValue=!oldValue;
chrome.storage.sync.set({propertyName: newValue}, function()
{
alert('SET:'+newValue);
});
});
}
当我运行此方法时,第一个警报显示:GET:old=undefined
,第二个警报显示:SET:true
,与预期的一样。但是当使用相同的参数再次调用该方法时,第一个警报再次显示GET:old=undefined
,而不是我预期的GET:old=true
当我使用storage.local而不是storage.sync时,这是相同的行为
“存储”在清单的权限中。JS是从我的扩展名的选项页面调用的-您正在执行.get(“plus1”),然后执行.set({“propertyName”:newValue},…)
。您将存储在键“propertyName
”下,但获取从未设置过的键“plus1
”
也许您的误解是,对象文本中的键本身就是文本(即使没有引用),而不是变量标识符。在这种情况下,你可能从阅读中受益