我使用以下代码在 chrome.storage 中加载和保存一个值:
$(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
就像预期的那样。但是,当使用相同的参数再次调用该方法时,第一个警报 AGAIN 会显示GET:old=undefined
,而不是GET:old=true
我预期的。
当我使用 storage.local 而不是 storage.sync 时,这是相同的行为
“存储”在清单的权限中。JS是从我的扩展的选项页面调用的-