Javascript 扩展OnUpdate可用

Javascript 扩展OnUpdate可用,javascript,google-chrome-extension,firefox-addon,microsoft-edge-extension,Javascript,Google Chrome Extension,Firefox Addon,Microsoft Edge Extension,我的扩展有一个持久的背景脚本,我希望它在更新可用时提示用户 据我所知,如果有更新可用,那么一旦用户重新启动chrome(由于持久后台脚本),扩展就会自动更新。因此,我想补充以下内容: chrome.runtime.onUpdateAvailable.addListener((details) => { var response = window.confirm(`${details.version} is now available`); if (response) { c

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

据我所知,如果有更新可用,那么一旦用户重新启动chrome(由于持久后台脚本),扩展就会自动更新。因此,我想补充以下内容:

chrome.runtime.onUpdateAvailable.addListener((details) => {
  var response = window.confirm(`${details.version} is now available`);
  if (response) {
    chrome.runtime.reload();
  }
});
我的困惑 发件人:

我不明白最后一句话:

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

问题
  • 如果没有事件的处理程序,这怎么可能呢
  • 我的功能正确吗
  • 我如何测试这个
  • 问题
    我尝试将版本设置为1.0.0(当前最新版本要高得多),并将其作为正在开发的扩展加载,但没有显示任何提示。

    您的功能是正确的,尽管我会使用chrome.windows.create,并使用
    类型
    弹出窗口
    以html显示非阻塞对话框,部分原因是,在后台脚本中调用confirm/alert/prompt时,confirm/alert/prompt在Firefox中不起作用

    我如何测试这个

    我不明白最后一句话

    扩展文档中有很多措词不当的部分,因此我们将阅读:

    if(extensions::BackgroundInfo::HasPersistentBackgroundPage(旧版)){
    const char kOnUpdateAvailableEvent[]=“runtime.onUpdateAvailable”;
    返回扩展::EventRouter::Get(profile_U4;)->ExtensionHasEventListener(
    扩展->id(),kOnUpdateAvailableEvent)
    延迟
    :安装;
    }
    
    因此,文件可以改进如下:

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


    如果我理解正确,在我的情况下(持久性背景),实际上不需要这个事件侦听器,因为一旦执行了检查并且更新可用,扩展就会更新?但是,如果我的背景不是持久的,那么它将在卸载背景时更新。这就引出了一个问题,这个事件侦听器的目的是什么?目的是在您选择时执行更新和/或通知更新,等等。哦,好的,我想我现在明白了。基本上,对于非持久性后台,它在扩展运行时被卸载,但此事件侦听器可以在卸载之前警告更新事件。谢谢
    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.