Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chrome.extension.getBackgroundPage is undefined in an extension page in an iframe

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_);

// ...
like image 500
pd176 Avatar asked Jun 01 '15 10:06

pd176


1 Answers

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.

like image 121
Rob W Avatar answered Oct 11 '22 03:10

Rob W