How do I check if a Google Chrome Extension needs to be updated?

前端 未结 2 1332
故里飘歌
故里飘歌 2021-01-05 01:51

I know for extensions hosted on the webstore, when I push updates Google will automatically update the extensions of users but there is a lag (couple of hours).

Is t

2条回答
  •  广开言路
    2021-01-05 02:30

    Another way is to use chrome.runtime.onUpdateAvailable.addListener.

    Documentation for chrome.runtime.requestUpdateCheck says that this method shouldn't be used because Google Chrome manually checks for update and installs the update if available. Usually Google Chrome checks for update once in 5 hours. If your background page not persistent, then Google Chrome will automatically installs the extension when background page gets unloaded, otherwise only when Google Chrome gets restarted.

    chrome.runtime.onUpdateAvailable.addListener is an event listener that "fired when an update is available, but isn't installed immediately because the app is currently running". "App is currently running" means the background page is either "persistent" or is still running, some content or background scripts still running, etc.

    You can manually call chrome.runtime.reload when chrome.runtime.onUpdateAvailable.addListener fires, but it is not the best approach there. You should be 100% sure that it doesn't break app process and user will not lose some sensitive information. It is best to notify the user (with alert or chrome.notifications).

    // background.js
    chrome.runtime.onUpdateAvailable.addListener((details) => {
        if (details) {
            // if you 100% sure about this.
            // chrome.runtime.reload();
    
            // too distract.
            // alert(`New version (${details.version}) is available. Restart the browser to update.`);
    
            // requires `notifications` permission.
            // chrome.notifications.create({
            //     type: "basic",
            //     iconUrl: chrome.extension.getURL("logo-48.png"),
            //     title: "Update available",
            //     message: `New version (${details.version}) is available. Restart the browser to update.`
            // });
        }
    });
    

    However, nothing of this is best approach because we should rely (and try to don't do persisten background pages, because it breaks the update process) on automaticall update process of Google Chrome and don't do any additional job.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题