如何修复 TypeError: browser.storage is undefined in chrome/firefox web extension?

Roy*_*son 10 javascript local-storage google-chrome-extension firefox-addon-webextensions

注意 - 这个问题是基于这个问题(虽然没有必要阅读上一个问题):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';
}
Run Code Online (Sandbox Code Playgroud)

但是,每当调用此函数时,我都会收到以下错误:

参考错误:browser.storage 未定义

我该如何解决这个错误?我通读了关于这种方法的 MDN 文档,所以我不确定我错过了什么。

pap*_*apo 11

正如评论所暗示的那样,您需要声明许可。
另外manifest.json

"permissions": [
    "storage"
],
Run Code Online (Sandbox Code Playgroud)

或将其添加到任何现有权限:

"permissions": [
  "activeTab",
  "storage"
],
Run Code Online (Sandbox Code Playgroud)

Mozilla 文档页面
Chrome 文档页面

存储 API 可用于内容脚本。
不要感到困惑,这不是问题,因为它在评论中链接的另一个问题中尽可能重复,但事实并非如此。