Consider the following Chrome extension:
{
"name": "Test onStartup",
"version": "0.0.0",
"manifest_version": 2,
"background": {
"persistent": false,
"scripts": ["eventPage.js"]
},
"permissions": ["storage"]
}
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With