Chrome扩展更新通知

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

可能是Chrome扩展自动更新事件的重复问题。 - Rob W
2个回答

4

第二个链接已经过时了。你能给一个简单的例子吗:当扩展程序更新时如何打开弹出窗口? - sorin
好的,chrome.runtime.onInstalled 的链接就是你所需要的。我不建议在更新时打开弹出窗口,因为那真的很烦人,但它可能会像这样:`chrome.runtime.onInstalled.addListener(function(details) { if (details.reason == "update") { chrome.windows.create({url: "popup.html", type: "popup"}); } });` - gengkev
chrome.runtime.onInstalled的链接已经失效了。现在Google将其重定向到一个不可能的地方。(https://code.google.com/https://developer.chrome.com/extensions/dev/runtime.html) 这里是一个更新后的链接: https://developer.chrome.com/extensions/runtime#event-onInstalled - Justin Force

3

这里是完整的答案,对我很有效。

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

希望这能有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接