grigoryvp
grigoryvp

Reputation: 42463

How to correctly write new "event page" for chrome extensions?

In new chrome extension API "background pages" was replaced with "event pages" that are loaded only one on extension install and need to register callback that will be called by chrome on some events.

I want to create "event page" that will execute my code every second. I have created an extension with "manifest.json":

{
  "manifest_version" : 2,
  "name" : "myextension",
  "version" : "1.0.0",
  "background" : { "scripts" : [ "background.js" ], "persistent" : false }
}

and "background.js":

function onTimer()
{
  console.log( "on timer" );
}

function onStartup()
{
  console.log( "on startup" );
  window.setInterval( onTimer, 1000 );
}

console.log( "script load" );
chrome.runtime.onStartup.addListener( onStartup );

After i install extension, i can see "script load" in developer tools console, but not "on startup" and "on timer" :(. And after i close and reopen my browser, even "script load" is not display (that is expected). What am i doing wrong?

Upvotes: 3

Views: 1959

Answers (1)

willthefirst
willthefirst

Reputation: 1619

From the Google Event Pages API:

If your extension uses window.setTimeout() or window.setInterval(), switch to using the alarms API instead. DOM-based timers won't be honored if the event page shuts down.

Looks like event pages are trashing you setInterval() function.

Upvotes: 3

Related Questions