Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension: Execute background page only once when chrome starts

I am playing with chrome extensions, my manifest loads a background page with:

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

The .js code looks like this:

var once = false;
window.addEventListener("load", function () {
  if ( once == true ) { return; }
  alert( 'test' );
  once = true;
}, false);

This seems to works fine, but i want to run the background_page.js code only once every time the browser starts.

Right now, I am not sure why, but the code is executed more than once. At first I thought it was being executed every time I open a new tab but that's not the case. The alert does appear once I start the browser, then if I open a new tab quickly the alert wont appear, but if I wait about 1 minute or so and open a new tab then the alert appears again.

How can i make sure the background_page.js code runs only once every time the browser starts?

like image 853
PeterF Avatar asked Jul 13 '13 18:07

PeterF


People also ask

Can Chrome run background extensions?

Run In Background. Run Chrome in the background continuously. This extension simply keeps Chrome running in the background, so that push notifications can be received while the browser is closed.

What does the background script do in Chrome extension?

The background script ('background. js') is a JavaScript script that runs once our extension either gets installed or the user refreshes the extension manually. The background script doesn't actually have access to any of the webpages your user is viewing, so your background script can't manipulate the DOM.

How do I make Chrome run in the background?

In your notification center, you should see the Google Chrome icon. Right click it and check the 'Let Google Chrome run in the background' option. Check that Chrome is running in the background: Manage background pages and apps.

Why does Google Chrome keep running in the background?

This might be to enable functionality such as email checks or virus scans, or because an app needs to update itself or stay aware while you work on other tasks. Google Chrome for Mac runs installed extensions and Web apps in the background, if they request it.


2 Answers

What you are using is an Event Page(background_page.js). Event pages are unloaded when the browser detects that the page is not doing anything. So what's happening is that when you open a new tab, the Event page is being reloaded and starts executing from the top again. This way chrome is able to have your app use less memory and speed up the browser.

If you want to fix the problem simply use persistent:true which will make sure the page "persists" indefinitely or until the user closes the browser. If you would really like to keep your app efficient with memory, you should take a look at the runtime.onSuspend method which gets called each time your Event page unloads. This way you can save stuff before the page gets unloaded so that you can resume where you left off.

like image 81
1337holiday Avatar answered Sep 24 '22 10:09

1337holiday


UPDATE: According to current documentation you simply have to delete the persistent key (no need to change it to "persistent": true).

This is an event page:

{
  "name": "My extension",
  ...
  "background": {
    "scripts": ["eventPage.js"],
    "persistent": false
  },
  ...
}

This is a background page:

{
  "name": "My extension",
  ...
  "background": {
    "scripts": ["background.js"]
  },
  ...
}
like image 39
quackkkk Avatar answered Sep 21 '22 10:09

quackkkk