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

When my chrome extension loads when chrome starts, everything looks fine, and chrome.extension.getBackgroundPage () returns the correct value (lunched from popup.js). But after a while (2-3 minutes), especially if the browser is not standing aside, the function returns null. The only problem is closing and reopening chrome. I tried to manipulate it:

if (chrome.extension.getBackgroundPage() == null) window.location.reload(true); 

As suggested in Why does chrome.extension.getBackgroundPage () return null? , which sometimes helps, but in most cases it just keeps updating the application so that the chrome needs to be closed. Does anyone know what could be wrong?

+3
source share
2 answers

According to the link page ( The difference between the event and the background page ) there is a better option for getting the background when using the event page

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

This worked for me, just do your job in an asynchronous callback that gets the background page as a function parameter.

Here is the specification of the runtime.getBackgroundPage method

+6
source

Change your background (Event Page) to a real man page.

Change manifest file with

 "background": { "scripts": [ "background.js" ], "persistent": false } 

before

 "background": { "scripts": [ "background.js" ], "persistent": true } 

Event pages are very similar to background pages with one important difference: event pages load only when they are needed. When an event page does something inactive, it is unloaded, freeing up memory and other system resources.

Link

+1
source

All Articles