Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Chrome extension first run / update

How can an extension find out that it is being run for the first time or has just been updated, so that the extension can perform some specific actions? (e.g. open a help page or update settings)

like image 689
rack1 Avatar asked Mar 08 '10 05:03

rack1


People also ask

Can I see my Chrome extension history?

You can navigate directly and just scroll down to load your browsing history day by day How to use: • Click this extension's icon • Click menu item from Chrome -> History -> Show Full History • Use Chrome keyboard shortcuts Permissions Needed - This extension requires access to your browsing history so that our ...

Do Chrome extensions get update automatically?

Chrome extentions are automatically updated even if Chrome autoupdate is disabled.

How do I know if my Chrome extensions are up to date?

Just go to chrome://settings/ click on Extensions, and select the 'Developer mode' button. Once done, this will enable the button “Update”. Click on it, and you are done. You can also access it via 3-dots Settings > More Tools > Extensions.


2 Answers

In newer versions of Chrome (since Chrome 22), you can use the chrome.runtime.onInstalled event, which is much cleaner.

Example:

// Check whether new version is installed chrome.runtime.onInstalled.addListener(function(details){     if(details.reason == "install"){         console.log("This is a first install!");     }else if(details.reason == "update"){         var thisVersion = chrome.runtime.getManifest().version;         console.log("Updated from " + details.previousVersion + " to " + thisVersion + "!");     } }); 
like image 96
Alvin Wong Avatar answered Oct 21 '22 02:10

Alvin Wong


Updated answer to reflect v3 of manifest:

Chromium now has a chrome.runtime set of APIs, which allow you to fetch the version of the extension.

To get the current version:

chrome.runtime.getManifest().version 

To listen when the extension has been first installed, when the extension is updated to a new version, and when Chromium is updated to a new version, you can use the onInstalled event.

chrome.runtime.onInstalled.addListener((details) => {    const currentVersion = chrome.runtime.getManifest().version    const previousVersion = details.previousVersion    const reason = details.reason        console.log('Previous Version: ${previousVersion }')    console.log('Current Version: ${currentVersion }')     switch (reason) {       case 'install':          console.log('New User installed the extension.')          break;       case 'update':          console.log('User has updated their extension.')          break;       case 'chrome_update':       case 'shared_module_update':       default:          console.log('Other install events within the browser')          break;    }  }) 

Thats all!


Old answer, prior to 2011

If you want to check if the extension has been installed or updated, you can do something like this:

function onInstall() {     console.log("Extension Installed");   }    function onUpdate() {     console.log("Extension Updated");   }    function getVersion() {     var details = chrome.app.getDetails();     return details.version;   }    // Check if the version has changed.   var currVersion = getVersion();   var prevVersion = localStorage['version']   if (currVersion != prevVersion) {     // Check if we just installed this extension.     if (typeof prevVersion == 'undefined') {       onInstall();     } else {       onUpdate();     }     localStorage['version'] = currVersion;   } 
like image 31
Mohamed Mansour Avatar answered Oct 21 '22 03:10

Mohamed Mansour