【问题标题】:How to cause a chrome app to update as soon as possible?如何让 Chrome 应用尽快更新?
【发布时间】:2013-03-24 09:37:42
【问题描述】:

部署 chrome 打包的应用程序并在 chrome 网上商店发布更新允许用户自动接收应用程序更新。在某些情况下,您想知道正在运行的应用程序是否是最新的,并对其进行更新。例如:

  • 只是让用户使用最新版本。
  • 检测到应用程序和服务器端 API 不匹配,并要求客户端应用程序更新以使用新的服务器端 API。

chrome.runtime.requestUpdateCheck() 的文档提供了"throttled""no_update""update_available" 的状态,但没有说明如果需要更新版本该怎么做。

【问题讨论】:

    标签: google-chrome-app


    【解决方案1】:

    chrome.runtime.onUpdateAvailable 安装一个侦听器,当新的 .crx 文件已下载并准备好安装新版本时会触发该侦听器。然后,拨打chrome.runtime.requestUpdateCheck

    chrome.runtime.onUpdateAvailable.addListener(function(details) {
      console.log("updating to version " + details.version);
      chrome.runtime.reload();
    });
    
    chrome.runtime.requestUpdateCheck(function(status) {
      if (status == "update_available") {
        console.log("update pending...");
      } else if (status == "no_update") {
        console.log("no update found");
      } else if (status == "throttled") {
        console.log("Oops, I'm asking too frequently - I need to back off.");
      }
    });
    

    【讨论】:

    • throttled 记录在哪里?你怎么知道是什么意思?
    • developer.chrome.com/apps/… 中提到了“节流”状态作为可能的返回值。这只是意味着您一直在询问过于频繁,而不是实际进行网络获取以查看是否有更新,Chrome 正在限制您的请求,您需要等待几分钟才能再次询问。我想如果你也能得到你需要等待多长时间的指示会很有用。
    • 是否值得将 chrome.runtime.requestUpdateCheck(...) 调用包装在 if 语句中,将其限制为每 15 分钟、30 分钟甚至每小时才调用一次?我想把它放在 popup.html 中,但我想这是一个昂贵的操作。
    • requestUpdateCheck除了知道更新状态还有其他意义吗?
    • @AntonySargent,当您需要知道更新何时可用但用户的浏览器尚未下载 crx 时会发生什么?
    【解决方案2】:

    根据您的应用程序,当检测到更新时,您可能需要使用 setTimeout 之类的内容,然后再调用 chrome.runtime.restart()chrome.runtime.restart()

    【讨论】:

    【解决方案3】:

    根据您需要的 Google Chrome 文档

    chrome.runtime.onUpdateAvailable.addListener(function(details) {
      chrome.runtime.reload(); // To restart the chrome App instantaneously
    });
    

    但这需要时间将 JS 更改反映到 chrome 中,因为 background.js 已加载到后台,需要卸载并再次加载

    要应对这种情况,您需要包括

    chrome.runtime.onInstalled.addListener(function(details) {
      chrome.runtime.reload();
    });
    

    也一样。

    每当第一次安装 google 扩展(全新安装)、更新 google 扩展或更新 google chrome 时都会调用 onInstalled。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      相关资源
      最近更新 更多