Google Chrome Extension: Inject contentscript in a webpage with js errors

Because the webpage contains errors, we can't inject directly the content script using this code in the manifest.json file
"content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["jquery.js", "content_script.js"]
    }
  ]
But we can inject the code each time the page is changed using an url change event listener, inserting the following code in the background.html page:
chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){
	if(changeInfo.status == "complete" && (tab.url.substr(0,25) == "http://www.mypage.com/" || tab.url.substr(0,26) == "https://www.mypage.com/") ){
		chrome.tabs.executeScript(null, {file: "jquery.js"});
		chrome.tabs.executeScript(null, {file: "content_script_injected.js"});
	}
  });
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Comments are closed.