KKSB
KKSB

Reputation: 131

Chrome extension update notification

How to show notification when chrome extension updates automatically? My requirement is to show a popup after update the chrome extension automatically.

Upvotes: 8

Views: 4269

Answers (2)

gengkev
gengkev

Reputation: 1959

There is a chrome.runtime.onInstalled event you can listen to that fires when the extension is installed or updated (so says the new packaged app documentation), but it's only available in the dev channel right now.

2019 Update: Since this answer was provided in 2012, this event has been moved from the dev to the stable channel.

Upvotes: 4

Veeresh Devireddy
Veeresh Devireddy

Reputation: 1145

Here is the complete answer and it is working for me.

//=============== background.js =================
chrome.runtime.onInstalled.addListener(function (details) {
  try {
    var thisVersion = chrome.runtime.getManifest().version;
    if (details.reason == "install") {
      console.info("First version installed");
      //Send message to popup.html and notify/alert user("Welcome")
    } else if (details.reason == "update") {
      console.info("Updated version: " + thisVersion);
      //Send message to popup.html and notify/alert user

      chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
        for( var i = 0; i < tabs.length; i++ ) {
            chrome.tabs.sendMessage(tabs[i].id, {name: "showPopupOnUpdated", version: thisVersion});
        }
        });
    }
  } catch(e) {
    console.info("OnInstall Error - " + e);
  }
});


//=============== popup.js =================
//Note: this has to be injected as content script from manifest.json
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    switch (request.name) {
        case "showPopupOnUpdated":
            alert("Extension got updated to latest version: " + request.version);
            break;
    }
});


//=============== manifest.js =================
//Note: background.html needs to import background.js
{
  "background": {
    "page": "background.html"
  },
  "content_scripts": [
    {
      "js": [
        "js/popup.js"
      ]
    }
  ]
}

Hope it helps.

Upvotes: 3

Related Questions