Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension onSuspend is never called

I develop Chrome extension.

I try to add event listener to chrome.runtime.onSuspend, but it is never called.

I use the following code, but localStorage is not modified and there is no log messages in the console also (I use --enable-logging --v=1 to save log messages to the file).

chrome.runtime.onSuspend.addListener(function() {
  localStorage["suspend"] = new Date();
  console.log("On suspend");
});

Test box: WinXP SP3 x86 with Chrome 28.0.1500.72 m

I've created test extension to easily reproduce this issue:

manifest.json

{
  "manifest_version": 2,

  "name": "Chrome onSuspend test",
  "version": "1.0",

  "background": { 
    "scripts": ["background.js"] }
}

background.js

chrome.runtime.onSuspend.addListener(function() {
  localStorage["suspend"] = new Date();
  console.log("On suspend");
});
like image 811
Anton Avatar asked Jul 23 '13 10:07

Anton


1 Answers

The onSuspend event is only triggered when the event page becomes inactive.

Because you didn't declare persistent: false in your manifest file, the background page is a background page, not an event page. Consequently, the page will never become inactive, and the onSuspend event will never be triggered.

If you wish to turn your background page in an event page, use

...
    "background": { 
        "scripts": ["background.js"],
        "persistent": false
    }
}
like image 194
Rob W Avatar answered Sep 28 '22 06:09

Rob W