【发布时间】:2013-08-28 18:31:47
【问题描述】:
我有一个 chrome 扩展程序,它会在每个打开的选项卡中注入一个 iframe。我的 background.js 中有一个 chrome.runtime.onInstalled 侦听器,它手动注入所需的脚本,如下所示(此处 API 的详细信息:http://developer.chrome.com/extensions/runtime.html#event-onInstalled):
background.js
var injectIframeInAllTabs = function(){
console.log("reinject content scripts into all tabs");
var manifest = chrome.app.getDetails();
chrome.windows.getAll({},function(windows){
for( var win in windows ){
chrome.tabs.getAllInWindow(win.id, function reloadTabs(tabs) {
for (var i in tabs) {
var scripts = manifest.content_scripts[0].js;
console.log("content scripts ", scripts);
var k = 0, s = scripts.length;
for( ; k < s; k++ ) {
chrome.tabs.executeScript(tabs[i].id, {
file: scripts[k]
});
}
}
});
}
});
};
当我第一次安装扩展时,这很好用。当我的扩展更新时,我想做同样的事情。如果我在更新时也运行相同的脚本,我看不到注入新的 iframe。不仅如此,如果我尝试在更新后向我的内容脚本发送消息,则不会有任何消息通过内容脚本。我看到其他人在 SO (Chrome: message content-script on runtime.onInstalled) 上也遇到了同样的问题。 chrome 扩展更新后删除旧内容脚本并注入新内容的正确方法是什么?
【问题讨论】: