How to cause a chrome app to update as soon as possible?

拟墨画扇 提交于 2019-11-28 21:26:34

问题


Deploying a chrome packaged app and publishing updates on the chrome web store allows users to automatically receive application updates. There are situations where you want to know if the running application is the most current or not, and updating it. E.G.:

  • Just keeping the user on the most recent version.
  • Detecting a mismatch between the application and server side APIs, and requiring the client side application to update to use new server side APIs.

Documentation for chrome.runtime.requestUpdateCheck() offers a status of "throttled", "no_update", "update_available", but doesn't indicate what to do if a newer version is required.


回答1:


Install a listener for chrome.runtime.onUpdateAvailable, which fires when the new .crx file has been downloaded and the new version is ready to be installed. Then, call 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.");
  }
});



回答2:


Depending on your application, when an update is detected you may want to use something like setTimeout and call chrome.runtime.restart() or chrome.runtime.restart() later




回答3:


According to the Google Chrome documentation you need to have

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

But this take time to reflect JS changes into the chrome because background.js loaded into the background and it needs to be unloaded and loaded again

To cop this situation you need to include

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

as wel.

onInstalled called whenever google extension installed first time (fresh installation), google extension updated or google chrome updated.



来源:https://stackoverflow.com/questions/15775187/how-to-cause-a-chrome-app-to-update-as-soon-as-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!