"chrome.runtime.reload is not a function" 尝试重新加载我的扩展运行时时

"chrome.runtime.reload is not a function" when trying to livereload my extension runtime

我正在构建一个 chrome 扩展,并将其与 livereload-js 集成。它正在工作,除了对 chrome.runtime.reload() 的调用抛出一个错误,即函数 reloadchrome.runtime 上不存在。这很奇怪,因为文档说应该有这个方法,而当我阅读 the docs for the reload method 时它说这个方法应该可用而无需任何额外的权限。如果这是在 Manifest 版本 3 中删除的,则文档中没有解释。

为简化起见,我已从应用程序中删除了大部分代码,我只是想让 livereload 正常工作。这是我的 manifest.json:

{
    "name": "Cool AF",
    "description": "something cool",
    "version": "1.1",
    "manifest_version": 3,
    "background": {
        "service_worker": "background.js"
    },
    "content_scripts": [
        {
            "matches": ["https://nevermind/*"],
            "css": ["main.css"],
            "js": ["main.js", "livereload.js"]
        }
    ],
    "host_permissions": ["http://localhost:35729/"],
    "permissions": ["storage", "management", "activeTab", "alarms", "background"]
}

livereload.js 中的代码考虑它是否是 chrome 扩展并调用 chrome.runtime.reload(),这会引发错误。除了让我知道扩展正在加载的 console.log 之外,我已经从扩展中删除了所有其他代码。关于如何让 reload 函数出现在 chrome.runtime 对象上的任何想法?

chrome.runtime.reload 不适用于内容脚本。

有关内容脚本可用的方法列表,请参阅 documentation一些 chrome.runtime 方法是,但不是全部:

runtime:

  • connect
  • getManifest
  • getURL
  • id
  • onConnect
  • onMessage
  • sendMessage

惯用的方法是 message 您的后台脚本请求它执行不允许内容脚本执行的操作。