【问题标题】:Extension onUpdateAvailable confusion扩展 onUpdateAvailable 混淆
【发布时间】:2020-12-15 04:13:29
【问题描述】:

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

据我了解,如果有可用的更新,那么一旦用户重新启动 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(当前最新版本更高)并将其作为正在开发的扩展加载,但没有显示提示。

【问题讨论】:

    标签: javascript google-chrome-extension firefox-addon microsoft-edge-extension


    【解决方案1】:

    您的功能是正确的,尽管我会使用带有 popuptype 的 chrome.windows.create 在 html 中显示非阻塞对话框,部分原因是在 Firefox在后台脚本中调用。

    我该如何测试?

    https://stackoverflow.com/a/26276485

    最后一句没看懂

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

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

    所以文档可以改进如下:

    如果没有处理程序在具有持久背景页面的扩展程序中侦听此事件,Chrome 会立即通过内部调用 chrome.runtime.reload() 更新扩展程序。

    【讨论】:

    • 如果我理解正确,在我的情况下(持久背景)实际上不需要此事件侦听器,因为一旦执行检查并且更新可用,扩展程序就会更新?但是,如果我的背景不是持久的,那么它会在背景卸载时更新。那么这就引出了一个问题,这个事件监听器的目的是什么?
    • 目的是在您选择的时候执行更新和/或通知它,等等。
    • 哦,好吧,我想我现在明白了。基本上对于非持久性背景,它会在扩展程序运行时被卸载,但是这个事件监听器可以在它被卸载之前提醒更新事件。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    相关资源
    最近更新 更多