【问题标题】:Chrome extension update notificationChrome 扩展更新通知
【发布时间】:2012-07-18 07:06:08
【问题描述】:

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

【问题讨论】:

标签: google-chrome


【解决方案1】:

有一个chrome.runtime.onInstalled event 可以在安装或更新扩展程序时触发 (so says the new packaged app documentation),但它目前仅在开发频道中可用。

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

【讨论】:

  • 第二个链接已过时。能否举个简单的例子:扩展更新时如何打开弹窗?
  • 嗯,chrome.runtime.onInstalled 的链接真的是你所需要的。我不建议在更新时打开弹出窗口,因为这真的很烦人,但它会是这样的:chrome.runtime.onInstalled.addListener(function(details) { if (details.reason == "update") { chrome.windows.create({url: "popup.html", type: "popup"}); } });
  • chrome.runtime.onInstalled 的链接已损坏。谷歌现在将其重定向到不可能的事情。 (code.google.com/https://developer.chrome.com/extensions/dev/…) 这是一个更新的链接:developer.chrome.com/extensions/runtime#event-onInstalled
【解决方案2】:

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

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2011-06-26
    • 2022-07-08
    • 2022-07-16
    • 1970-01-01
    • 2014-09-17
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多