Harry Jiang
Harry Jiang

Reputation: 731

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

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 there a way for the extension to detect if there is a new update that hasn't been applied? The easiest way to do this would be to somehow query for the current version on the webstore and compare it to the installed version and I was wondering if there was anything in the API for this.

I know that chrome.app.getDetails().version gives me the current installed version but is there a way to get the current web store version? Thanks.

In the worst case, I could issue an http get for my extensions web store page and extract the version out from there but that seems to be super hacky so I'd prefer not to do it this way.

Upvotes: 14

Views: 5547

Answers (2)

Amaimersion
Amaimersion

Reputation: 1015

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.

Upvotes: 7

Xan
Xan

Reputation: 77591

You missed it, it's in the chrome.runtime API:

chrome.runtime.requestUpdateCheck(function callback)

Requests an update check for this app/extension.

Do note that the server may throttle your requests. You are supposed to check for its response in the callback and reduce your request frequency if that happens.

If you want to be even more proactive about that, you could implement push notifications for your update check. That's probably overkill, though - considering Web Store runs automated checks on updates that can last up to an hour anyway after you publish.

Upvotes: 10

Related Questions