如何在Chrome扩展程序自动更新时显示通知? 我的要求是在自动更新chrome扩展程序后显示弹出窗口。
答案 0 :(得分:3)
当安装或更新扩展程序(chrome.runtime.onInstalled
event),时,有一个so says the new packaged app documentation你可以听到这些内容,但它现在只在开发者频道中可用。
2019更新:由于此答案于2012年提供,因此此事件已从开发者移至稳定渠道。
答案 1 :(得分:0)
这是完整的答案,它对我有用。
//=============== 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"
]
}
]
}
希望它有所帮助。