扩展 onUpdateAvailable 混淆

Extension onUpdateAvailable confusion

我的扩展有一个持久的后台脚本,我希望它在有可用更新时提示用户。

据我了解,如果有可用的更新,那么一旦用户重新启动 chrome(由于持久的后台脚本),扩展将自动更新。为此,我想补充如下:

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

我的困惑

发件人: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.

最后一句没看懂:

如果没有处理程序正在侦听此事件,并且您的扩展有一个持久的后台页面,它的行为就好像 chrome.runtime.reload()调用以响应此事件

问题

  1. 如果事件没有处理程序,这怎么可能?
  2. 我的函数正确吗?
  3. 我该如何测试?

问题

我试过将版本设置为 1.0.0(当前最新版本更高)并将其作为开发中的扩展加载,但没有显示任何提示。

你的函数是正确的,尽管我会使用 chrome.windows.create 和 popuptype 来在 html 中显示非阻塞对话框,部分原因是 confirm/alert/prompt 在后台脚本中调用时在 Firefox 中不起作用。

How can I test this?

I do not understand the last sentence

扩展文档有很多措辞不佳的部分,所以我们将阅读 source code:

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

所以文档可以改进如下:

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.