chrome extension script is loading twice even more on some pages
google-chrome-extension, javascript
Solution
`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 */
}
});
Problem
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"}); } }); ``` 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 what is the problem? =============EDIT================== those pages contain Iframes