为什么 getBackgroundPage() 没有在 chrome.runtime 中为我定义?

Why is getBackgroundPage() not defined in chrome.runtime for me?

我正在尝试从我的内容脚本访问 chrome.runtime.getBackgroundPage(),但我得到:

Uncaught TypeError: chrome.runtime.getBackgroundPage is not a function(anonymous function) @ VM11844:1InjectedScript._evaluateOn @ VM11665:883InjectedScript._evaluateAndWrap @ VM11665:816InjectedScript.evaluateOnCallFrame @ VM11665:942window.onload @ run.js:101

这是我的 manifest.json 的样子:

{
  "background": {
    "scripts": ["background.js"],
    "persistent": true
  },
  "content_scripts": [{
    "matches": ["<all_urls>"],
    "js": ["run.js"],
    "run_at": "document_start"
  }],
  "description": "Hello world!",
  "homepage_url": "https://tryprospect.com",
  "icons": {
    "16": "icons/16x16.png"
  },
  "manifest_version": 2,
  "name": "Hello world",
  "permissions": ["storage", "management"],
  "version": "v0.1",
  "web_accessible_resources": ["html/*"]
}

我错过了什么吗?也许是许可?

谢谢!

大多数 chrome.* API 不可用于内容脚本。它们只能从背景或事件页面、弹出窗口或您定义的其他扩展视图中使用。

特别是您不能使用 chrome.runtime.getBackgroundPage(),因为扩展的 window 对象与内容脚本存在于不同的进程中。内容脚本与扩展的其余部分之间进行通信的唯一方法是通过消息传递或存储。