使用chrome.storage.get获取变量

Using chrome.storage.get to get a variable

本文关键字:获取 变量 get storage chrome 使用      更新时间:2023-09-26

我正在做一个小的Chrome扩展,我已经成功地使用选项页面来使用Chrome.storage.sync.set来设置变量pws。但是,在弹出页面上,我无法成功检索变量并在字符串中使用它。这是它的代码:

weather.open("GET", "http://api.wunderground.com/api/"+WU_API_KEY+"/conditions/q/CA/pws:"+pws+".json", true);
weather.send()
})

如何从chrome.sync.storage.get中获取pws作为变量?

我试过类似的东西

chrome.storage.sync.get(pws, function(result){
var pws = result.pws
console.debug('result:',pws);
});

但无济于事。

如果使用"pws"作为存储pws的密钥,则可以:

chrome.storage.sync.get("pws", function(result) {
    var pws = result.pws;
    console.log("result:", pws);
});