5

chrome扩展自动更新时如何显示通知?我的要求是在自动更新 chrome 扩展后显示一个弹出窗口。

4

2 回答 2

4

安装或更新扩展程序时,您可以收听一个chrome.runtime.onInstalled事件(新打包的应用程序文档如此说),但它现在仅在开发通道中可用。

2019 年更新:自 2012 年提供此答案以来,此事件已从 dev 移至 stable 频道。

于 2012-07-22T19:09:45.433 回答
1

这是完整的答案,它对我有用。

//=============== 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"
      ]
    }
  ]
}

希望能帮助到你。

于 2016-12-09T16:20:57.320 回答