admin管理员组

文章数量:1432620

I am trying to access chrome.runtime.getBackgroundPage() from my content script but I get:

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

Here's what my manifest.json looks like:

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

Am I missing something? Perhaps a permission?

Thanks!

I am trying to access chrome.runtime.getBackgroundPage() from my content script but I get:

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

Here's what my manifest.json looks like:

{
  "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.",
  "icons": {
    "16": "icons/16x16.png"
  },
  "manifest_version": 2,
  "name": "Hello world",
  "permissions": ["storage", "management"],
  "version": "v0.1",
  "web_accessible_resources": ["html/*"]
}

Am I missing something? Perhaps a permission?

Thanks!

Share Improve this question asked May 2, 2015 at 23:35 Curtis SaccoCurtis Sacco 211 silver badge3 bronze badges 1
  • Could you post the bit when you're calling the function? – Samurai Commented May 2, 2015 at 23:52
Add a ment  | 

1 Answer 1

Reset to default 7

Most chrome.* APIs are not available to content scripts. They can only be used from background or event pages, popups, or other extension views you define.

In particular you can't use chrome.runtime.getBackgroundPage() because the window objects for the extension live in a different process than the content script. The only way to municate between content scripts and the rest of your extension is via messaging or storage.

本文标签: javascriptWhy is getBackgroundPage() not defined in chromeruntime for meStack Overflow