7

I am starting with chrome extension development and have a couple of questions regarding extension install/update flow and testing during the development :

  1. What happens with the background script after extension update, does chrome perform background script reload ?
  2. Are content scripts detached from background script after extension update ?
  3. 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) ?
  4. 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 ?
  5. 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 ?

Thanks!

vladamon
  • 297
  • 1
  • 3
  • 11
  • see the tag [tag:google-chrome-extension] that you tagged this question with? there's an info [link](https://stackoverflow.com/tags/google-chrome-extension/info) – Jaromanda X Aug 24 '16 at 13:24

1 Answers1

13
  1. 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.

  • If you have a persistent background page:
    • If you handle this event and call chrome.runtime.reload(), the extension is unloaded and then updated before being loaded again.
    • If you handle this event and do not call chrome.runtime.reload(), then the update will only apply when the extension is next reloaded - likely the next full browser restart.
    • If you do not handle this event at all, the extension will be unloaded immediately to be updated.
  • If you have a non-persistent Event page:
    • If you handle this event and call chrome.runtime.reload(), the extension is updated before being loaded again.
    • If you do not call 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.

  1. 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:

  • Inject scripts programmatically in existing tabs, without making an assumption that it did not execute before: cleanup first if necessary.
  • Make sure orphaned copies stop executing: either by noticing in the old copy that it's orphaned, or by broadcasting a DOM event from the new copy.

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:

  1. 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.

  1. 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.

  1. 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.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • Is it possible to prevent an `onInstalled` event during update? I'm using it in a background script for setting default options for an extension I build, which is fine during installation, but if the code also gets executed after an update that would reset users' saved options back to default. – Costas Dec 27 '21 at 13:32
  • 1
    @Costas `onInstalled` handler received a `details` argument, which includes an update reason - you can filter out updates from installs based on that. That said, it's probably best if your "setting defaults" code is robust against already having options instead. It would help you when a later version will have a new setting requiring defaults. See https://stackoverflow.com/a/23638867/934239 for an example of such a pattern. – Xan Dec 28 '21 at 09:42
  • I think you refer to this solution that happened to found myself while searching about the matter: https://stackoverflow.com/questions/70494975/chrome-extension-keeping-user-preferences-unchnaged-after-update Thanks for pointing to the correct method! Does the get method work with `sync` to or only with `local`? – Costas Dec 28 '21 at 17:11
  • 1
    Yep, it does work. – Xan Dec 28 '21 at 17:14
  • When background is persist, and the user get update, the background is "reload" by default, or if we want this behaviour, we should do it manually via the event handlers? – Ballon Ura May 13 '22 at 13:54
  • @BallonUra If you don't have a registered handler for `onUpdateAvailable`, then the default is to reload the extension ASAP. – Xan May 13 '22 at 16:06