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 is 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 is not available. I get this error:

 Uncaught TypeError: Cannot call method 'testRequest' of null 

When I check chrome.extension.getBackgroundPage() , I get a null value. I think I made a mistake in the manifest file, but I don’t see what I did wrong.

Thanks.

+7
source share
2 answers

You are missing background resolution, see my manifest.json file of my chrome extension:

 { "content_scripts": [ { "matches": ["http://*/*"], "js": ["jquery.js", "asserts.js"] } ], "name": "Test Extension", "version": "1.0", "description": "A test extension to inject js to a webpage.", "background_page": "background.html", "options_page": "options.html", "browser_action": { "default_icon": "icon.png", "popup": "popup.html" }, "permissions": [ "tabs", "http://*/*", "https://*/*", "<all_url>", "background" ] } 

EDIT: Are you sure you have the background.html file in the same folder as all your chrome extension files ?, and if so, try reloading the extension from the add control page, I remember as soon as I had an error, which my extension did not reload, so I went over to the developer tools for my background page and executed window.location.reload(true); from the console that fixed it. Please answer if this worked, I will continue to study

+1
source

Here is another answer with additional options:

chrome.extension.getBackgroundPage () returns null after some time

According to the link page (the difference between the event and the Background page), there is a better option for getting the background while still using the event page:

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

+2
source

All Articles