【发布时间】:2012-07-18 07:06:08
【问题描述】:
如何在 chrome 扩展程序自动更新时显示通知? 我的要求是在自动更新 chrome 扩展后显示一个弹出窗口。
【问题讨论】:
标签: google-chrome
如何在 chrome 扩展程序自动更新时显示通知? 我的要求是在自动更新 chrome 扩展后显示一个弹出窗口。
【问题讨论】:
标签: google-chrome
有一个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"}); } });
这是完整的答案,它对我有用。
//=============== 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"
]
}
]
}
希望对你有帮助。
【讨论】: