Chrome.extension.getBackgroundPage is undefined on extension page in iframe

I am trying to access my main extension page using a function chrome.extension.getBackgroundPage

.

However, I am getting the following error:

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

I am calling a function from my file bar.js

which is defined as web_accessible_resource

in mymanifest.json

How do I get it to 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 the script inside bar.html

(not the 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_);

// ...

      

+3


source to share


1 answer


Most of the extension APIs can only be used if the page is being executed during extension, that is, the top-level frame is sandboxed chrome-extension:

.

chrome-extension:

-fragments in a non-extension process can only access the extension API, accessible content scripts, and web pages. And unlike content scripts, they can also use the web platform APIs at the beginning of the source. For example, if you are using localStorage

a script in the content, then the DOM store of the page on which the script content is executed will be available. If you use localStorage

on the page chrome-extension:

you will get the extension.



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

+5


source







All Articles