Matchu
Matchu

Reputation: 85794

chrome.runtime.onStartup never fires?

Consider the following Chrome extension:

manifest.json

{
    "name": "Test onStartup",
    "version": "0.0.0",
    "manifest_version": 2,
    "background": {
        "persistent": false,
        "scripts": ["eventPage.js"]
    },
    "permissions": ["storage"]
}

eventPage.js

chrome.runtime.onStartup.addListener(function() { 
    console.log("I started up!");
    chrome.storage.local.set({"startedUp": true});
});

chrome.runtime.onStartup is documented as firing "when a profile that has this extension installed first starts up", and I would've suspected that it also fires upon reloading the extension. However, upon restarting the browser or reloading the extension, I do not see the console.log message in the _generated_background_page.html's console, and chrome.storage.local.get("startedUp", function(v) { console.log(v) }) yields no results, so I suspect that the listener was not called.

Am I misunderstanding when this event is triggered or binding to it incorrectly or anything like that? Is it an issue with Chrome 28.0.1500.71 on Linux?

Upvotes: 14

Views: 13949

Answers (3)

Marnes
Marnes

Reputation: 661

Heads-up about this event which - in hindsight - is kinda obvious: your eventListener won't be fired if you register it after any async callbacks. You have to register it during the initial loading, outside of any callbacks.

In my case it wasn't getting fired because I was registering it as part of my "main" method, which was called after a settings validation, which requires a callback.

Upvotes: 5

Uzair Farooq
Uzair Farooq

Reputation: 2427

When you close chrome its background process keep on running. Make sure to kill all the process named as chrome or similar before starting chrome again.

Upvotes: 13

Chris McFarland
Chris McFarland

Reputation: 6169

chrome.runtime.onStartup is only called when Chrome starts, not when the extension starts.

chrome.runtime.onInstalled is called when you manually reload the extension within chrome://extensions, or when the extension calls chrome.runtime.reload().

Upvotes: 15

Related Questions