注意 - 这个问题是基于这个问题(虽然没有必要阅读上一个问题):How to set value of textarea in different HTML file?
我有以下代码,它应该在 firefox/chrome 的本地存储中添加一个值,然后更改扩展的 UI(代码会根据使用的浏览器略有不同,截至目前,代码用于 firefox 中的扩展):
function createSummaryBox(summary) {
console.log("Setting textarea content to: " + summary);
const currentSummary = {
currentSummary: summary
}
browser.storage.local.set(currentSummary);
window.location.href = '../summary_page/summary_page.html';
}
但是,每当调用此函数时,我都会收到以下错误:
ReferenceError: browser.storage is not defined
如何解决这个错误?我通读了有关此方法的 MDN 文档,所以我不确定我遗漏了什么。
最佳答案
如评论所示,您需要声明权限。
在 manifest.json
添加:
"permissions": [
"storage"
],
或将其添加到任何现有权限:
"permissions": [
"activeTab",
"storage"
],
Mozilla doc page
Chrome doc page
存储API可用于内容脚本。
不要混淆,这不是问题,因为它在评论中链接的另一个问题中可能是重复的,但事实并非如此。
关于javascript - 如何修复 TypeError : browser. storage is undefined in chrome/firefox web extension?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49743198/