Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chrome.extension.getBackgroundPage() returns null after awhile

When my chrome extension loads on chrome startup, everything seems to be ok and chrome.extension.getBackgroundPage() returns the right value (lunched from popup.js). But after awhile (2-3 minutes), especially if the browser is let alone, the function returns null. Only closing and re-opening chrome solves the problem. I tried to manipulate it by:

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

As suggested at Why does chrome.extension.getBackgroundPage() return null?, which sometimes helps, but most of the times it just keeps refreshing the app so that chrome must be closed. Does anybody have an idea what could be wrong?

like image 944
shay__ Avatar asked Dec 20 '22 09:12

shay__


1 Answers

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.

This worked well for me, just do your job in the asynchronous callback, which receives the background page as a function parameter.

Here is the specification of the method runtime.getBackgroundPage

like image 129
S. Goumy Avatar answered Dec 28 '22 06:12

S. Goumy