【问题标题】:Is there a way to use the `chrome` namespace in a webpage?有没有办法在网页中使用“chrome”命名空间?
【发布时间】:2019-07-15 16:55:00
【问题描述】:

我正在开发一个在 firefox 和 chrome 中运行的 WebExtension(此外,当我访问构建留下的 html 时,我需要该扩展作为网页工作)。在这种情况下,我想使用chrome.storage.local 存储。但是,当我尝试在 Chrome 的网页中使用它时,storage 未定义,我得到:

Uncaught (in promise) TypeError: Cannot read property 'local' of undefined

作为一种解决方法,如果location.protocol 与“chrome-extension:”或“moz-extension:”不同,我正在考虑使用 window.localStorage,但我想知道是否有办法使chrome.storage 在网页中工作。

【问题讨论】:

    标签: javascript google-chrome firefox google-chrome-extension


    【解决方案1】:

    一些 chrome API(例如 chrome.runtime.sendMessage)可用于常规页面,但 chrome.storage 不是其中之一。它仅适用于在其manifest.json 中请求storage 权限的扩展。这在 ChromeMozilla Firefox 的文档中都有说明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      相关资源
      最近更新 更多