尝试实现 Firefox 插件/扩展版本 64.0 的选项页面。我在用browser.storage.local.se
t 存储数据。但是当我使用browser.storage.local.get
拉取数据,结果是<unavailable>
在控制台日志上。
以下是我在我的中运行的函数options.js
文件(我输入njnj
在表单字段上gateway
并点击提交按钮)
function saveOptions(e) {
e.preventDefault();
console.log("you are here")
console.log(document.querySelector("#gateway").value)
browser.storage.local.set({
"gateway": document.querySelector("#gateway").value });
console.log(browser.storage.local.get("gateway"))
}
document.querySelector("form").addEventListener("submit", saveOptions);
我在控制台日志中的实际输出如下:
you are here options.js:4:3
njnj options.js:5:3
<unavailable> options.js:8:3
好的,所以我确实部分地弄清楚了为什么上面的代码不起作用。问题是 browser.storage.local.get() 在 javascript 中返回一个“promise”(我实际上还不知道它是什么意思)。因此,您必须有一个代码可以实际从这个“承诺”中检索答案/保存的值。我将举一个例子来说明如何检索该值:
// first save a key value pair into storage
browser.storage.local.set({"key": 'value'})
// to retrieve this value, first declare a new variable
var savedvalue = "zero"
// retrieve the 'promise' of key value pair, then run the associated function to get
//the savedvalue and set it equal to previously declared variable.
browser.storage.local.get(['key'], function(result) {savedvalue = result.key});
// now, when you call savedvalue (even outside the function above), it will return 'value'
console.log(savedvalue)
output>> value
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)