问题描述
这是我的background.js文件
this is my background.js file
chrome.tabs.onUpdated.addListener(function(tabId,info, tab) {
var sites =new Array('site2','site1');
var url=tab.url;
var siteFlag=0;
for(var i in sites) {
var regexp = new RegExp('.*' + sites[i] + '.*','i');
if (regexp.test(url)) {siteFlag=1;}
};
if(siteFlag==1){
chrome.tabs.executeScript(tabId, {file:"contentscript.js"});
chrome.tabs.executeScript(tabId, {file:"jquery.js"});
chrome.tabs.insertCSS(tabId,{file:"box.css"});
}
});
在contentscript.js中,我只是运行一个弹出框.
In the contentscript.js I simply run a popup box.
$(document).ready(function () {
function popup() {...}
if (window.addEventListener)
{
window.addEventListener('load', popup(), false);
}
else if (window.attachEvent)
{
window.attachEvent('onload', popup());
}
});
有些页面有一个弹出框,有些页面有两个甚至更多
There are some pages that there are one popup-box and there are pages that two or even more
出什么问题了?
=============编辑=================
=============EDIT==================
这些页面包含iframe
those pages contain Iframes
推荐答案
chrome.tabs.onUpdated.addListener
,这与iframe无关.您的脚本被多次注入同一帧,因为您为每个状态更改注入了脚本,并且只想在状态更改为完成"时才注入脚本.通过添加if (changeInfo.status == "complete") {
来修改代码:
chrome.tabs.onUpdated.addListener
is fired when tab status changes, this has nothing to do with iframes. Your script is injected multiple times to the same frame because you inject it for each status change, and you want to inject it only when status changes to "complete". Modify your code by adding if (changeInfo.status == "complete") {
:
chrome.tabs.onUpdated.addListener(function(tabId,info, tab) {
if (info.status == "complete") {
/* checking & injecting stuff */
}
});
这篇关于chrome扩展脚本在某些页面上甚至加载了两倍甚至更多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!