9

I am trying to access my extension's background page using chrome.extension.getBackgroundPage function.

However, I get the following error:

Uncaught TypeError: chrome.extension.getBackgroundPage is not a function

I am calling the function from my bar.js file which is defined as a web_accessible_resource in my manifest.json

How do I make it work?

manifest.json

{
  "manifest_version": 2,
  "name": "XPath Helper",
  "version": "1.0.13",
  "description": "Extract, edit, and evaluate XPath queries with ease.",
  "background": {
    "page": "background.html"
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "css": ["content.css"],
      "js": ["content.js"]
    }
  ],
  "permissions": ["http://*/", "tabs", "identity", "identity.email"],
  "icons": {
    "32": "static/icon32.png",
    "48": "static/icon48.png",
    "128": "static/icon128.png"
  },
  "web_accessible_resources": [
    "bar.css",
    "bar.html",
    "bar.js"
  ]
}

bar.js is a script inside bar.html (not a content script):

// ...

document.addEventListener('DOMContentLoaded',function(){
  // previosuly chrome.extension.getBackgroundPage()
  chrome.runtime.getBackgroundPage(function(page){
    alert("hello");
  })  
})

content.js

// ...

  this.barFrame_ = document.createElement('iframe');
  this.barFrame_.src = chrome.extension.getURL('bar.html');

  document.body.appendChild(this.barFrame_);

// ...
19
  • Is it a content script, or are you injecting it into a page anyhow, or?..
    – Xan
    Commented Jun 1, 2015 at 10:29
  • I'm fairly certain that the chrome internal API is only available to JS files bundled with the extension. Commented Jun 1, 2015 at 10:29
  • calling it from my bar.js which is a web accessible resource. Attached my manifest.json
    – pd176
    Commented Jun 1, 2015 at 10:32
  • I suppose you inject "bar.html" into the page, correct? As an iframe? Can you add that code from content.js?
    – Xan
    Commented Jun 1, 2015 at 10:34
  • 1
    try chrome.runtime.getBackgroundPage(callback)
    – woxxom
    Commented Jun 1, 2015 at 17:36

1 Answer 1

9

Most extension APIs can only be used if the page runs in the extension process, i.e. the top-level frame is a non-sandboxed chrome-extension: page.

chrome-extension:-frames in a non-extension process can only access the extension APIs available to content scripts and web pages. And unlike content scripts, they can also use web platform APIs at the extension's origin. For example, if you use localStorage in a content script, then the DOM storage of the page where the content script runs is accessed. If you use localStorage in a chrome-extension: page, then you'll get the extension's storage.

If you want to access functionality of the background page in your frame, use the extension messaging APIs to communicate between your frame and background page.

2
  • 1
    I really wanted you to answer, as it would be most authoritative. Thank you.
    – Xan
    Commented Jun 2, 2015 at 10:41
  • Managed to get it working by message passing. Thanks everyone for your help! :)
    – pd176
    Commented Jun 3, 2015 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.