Reputation: 307
I am starting with chrome extension development and have a couple of questions regarding extension install/update flow and testing during the development :
Thanks!
Upvotes: 8
Views: 2760
Reputation: 77523
- What happens with the background script after extension update, does Chrome perform background script reload?
The behavior depends on whether you have a handler to chrome.runtime.onUpdateAvailable
event registered and whether your extension has a persistent background page or event page.
chrome.runtime.reload()
, the extension is unloaded and then updated before being loaded again.chrome.runtime.reload()
, then the update will only apply when the extension is next reloaded - likely the next full browser restart.chrome.runtime.reload()
, the extension is updated before being loaded again.chrome.runtime.reload()
, or do not handle the event at all, Chrome will update the extension when the Event page next gets unloaded.There is no way to programmatically prevent the update once the background page gets unloaded for whatever reason.
- Are content scripts detached from background script after extension update?
Yes, and it's not pretty. They enter an "orphaned" state when using Chrome API gives inconsistent errors (some do nothing, some trigger exceptions), but are still running — for example, any DOM event listeners will still trigger.
As such, if you want the content scripts to work immediately again, your job is to:
Important note about WebExtensions: Firefox, unlike Chrome, always reinjects content scripts on load into pages that match manifest entries. Make sure to take that into account.
There are a few question that cover this; for example:
- If there's an
onInstalled
event handler in background script, what happens with that event handler when chrome updates extension (is this event handler detached, and when update finishes, the new handler is attached and then executed or some other flow is exercised)?
Since an update can only happen while the background page is unloaded, there is no complex logic; it will simply fire on first load of the extension afterwards with details.reason == "update"
. Be sure to register the handler synchronously on script load (e.g. in top level code), or else you may miss the event — normally this only concerns Event pages, but I suspect it's also important here.
- Is there a way to simulate update process during development in order to debug events that happen during the update process, for example to host extension on some local server and update from there?
Sadly, this is no longer possible to the best of my knowledge, unless you can use Enterprise Policy install. Your best bet is to have an extension in CWS that's published as Private.
To a certain extent, pressing "Reload" after making some changes to an unpacked extension simulates what happens during the update - with the exception of onInstalled
event.
- Where to search for documentation on topics like this and similar, is the chromium source code the right place or at least the starting point?
Well.. For detailed questions Chromium code is, of course, the authoritative source. You should search StackOverflow as well, as there's quite a body of knowledge amassed here already. Finally, the official docs provide a lot of information, even if it's not immediately evident - the chrome.runtime
API docs, for example.
Upvotes: 15