Search code examples
javascriptgoogle-chrome-extensionfirefox-addonmicrosoft-edge-extension

Extension onUpdateAvailable confusion


My extension has a persistent background script and I would like it to prompt a user when an update is available.

From my understanding, if an update is available, then the extension will be automatically updated once the user restarts chrome (due to persistent background script). For this reason, I want to add the following:

chrome.runtime.onUpdateAvailable.addListener((details) => {
  var response = window.confirm(`${details.version} is now available`);
  if (response) {
    chrome.runtime.reload();
  }
});

My Confusion

From: https://developer.chrome.com/docs/extensions/reference/runtime/#event-onUpdateAvailable

onUpdateAvailable

chrome.runtime.onUpdateAvailable.addListener(listener: function)

Fired when an update is available, but isn't installed immediately because the app is currently running. 
If you do nothing, the update will be installed the next time the background page gets unloaded, if you want it to be installed sooner you can explicitly call chrome.runtime.reload().
If your extension is using a persistent background page, the background page of course never gets unloaded, so unless you call chrome.runtime.reload() manually in response to this event the update will not get installed until the next time chrome itself restarts.
If no handlers are listening for this event, and your extension has a persistent background page, it behaves as if chrome.runtime.reload() is called in response to this event.

I do not understand the last sentence:

If no handlers are listening for this event, and your extension has a persistent background page, it behaves as if chrome.runtime.reload() is called in response to this event.

Questions

  1. How is that possible if there are no handlers for the event?
  2. Is my function correct?
  3. How can I test this?

Problem

I've tried setting the version to 1.0.0 (current latest is much higher) and loading it as an extension under development, but no prompt was shown.


Solution

  • Your function is correct, although I would use chrome.windows.create with a type of popup to show a non-blocking dialog in html, partly because confirm/alert/prompt don't work in Firefox when called in the background script.

    How can I test this?

    https://stackoverflow.com/a/26276485

    I do not understand the last sentence

    The extension documentation has quite a few poorly phrased pieces so we'll read the source code:

    if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
      const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
      return extensions::EventRouter::Get(profile_)->ExtensionHasEventListener(
                 extension->id(), kOnUpdateAvailableEvent)
                 ? DELAY
                 : INSTALL;
    }
    

    So the documentation could be improved as follows:

    If no handlers are listening for this event in an extension with a persistent background page, Chrome updates the extension right away by calling chrome.runtime.reload() internally.