2

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.

lbragile
  • 7,549
  • 3
  • 27
  • 64

1 Answers1

1

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.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • If I understand correctly, in my case (persistent background) this event listener is not actually needed since the extension will update as soon as a check is performed and an update is available? But if my background was not persistent then it would update when the background gets unloaded. So that begs the question, what is the purpose of this event listener? – lbragile Dec 15 '20 at 17:56
  • 1
    The purpose is to perform the update at the time of your choosing and/or notify about it, and so on. – wOxxOm Dec 15 '20 at 18:08
  • Oh ok I think I see now. Basically for non-persistent background, it gets unloaded while the extension is running but this event listener can alert an update event before it gets unloaded. Thanks! – lbragile Dec 15 '20 at 18:23