Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 229
Matchu Avatar asked Jul 12 '13 18:07

Matchu


1 Answers

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.

like image 178
Marnes Avatar answered Sep 30 '22 06:09

Marnes