Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does chrome.extension.getBackgroundPage() return null?

I have a manifest.json file that looks like this:

{
  "name": "Zend Debugger Extension",
  "version": "0.1",
  "background_page": "background.html",
  "permissions": [
    "cookies", "tabs", "http://*/*", "https://*/*"
  ],
  "browser_action": {
    "default_title": "Launch Zend Debugger",
    "default_icon": "icon.png",
    "popup": "popup.html"
  }
}

Here's my background.html:

<html>
    <script>
    function testRequest() {
        console.log("test Request received");
    }
    </script>
</html>

And my popup.html:

<script>
function debug(target) {
    if (target.id == 'thisPage') {
        console.log('sending request');
        chrome.extension.getBackgroundPage().testRequest();
    }
}
</script>

<div onclick="debug(this)" id="thisPage">Current Page</div>

However, the background.html page doesn't seem to be accessible. I'm getting this error:

Uncaught TypeError: Cannot call method 'testRequest' of null

When I inspect chrome.extension.getBackgroundPage() I get a null value. I'm thinking I have made a mistake in my manifest file, but I can't see what I've done wrong.

Thanks.

like image 394
AntBrown Avatar asked Nov 01 '11 19:11

AntBrown


1 Answers

Here's another answer with more options:

chrome.extension.getBackgroundPage() returns null after awhile

According to the referenced page (Difference between Event and Background Page) there is a better option to get the background while still using Event Page :

If your extension uses, extension.getBackgroundPage, switch to runtime.getBackgroundPage instead. The newer method is asynchronous so that it can start the event page if necessary before returning it.

like image 175
user3285954 Avatar answered Oct 17 '22 16:10

user3285954