I feel that this will probably immediately become apparent to any non-amateur, but I have been at a standstill for several days.
I am writing a Chrome extension that runs a script at the start of a document. I want to rewrite the HTML code of specific DIVs as they are downloaded, but before they are displayed to the user (so that the user does not see the HTML code of the site before it is unceremoniously replaced by my custom HTML). The method I'm trying to use is as follows.
addEventListener('DOMNodeInserted', function(event){ if(event.relatedNode.innerHTML.indexOf("contentArea")>-1){ writeContentArea(); } }, false); function writeContentArea(){ var divtowrite = document.getElementById('contentArea'); include(divtowrite,"contentArea.html");
Now the problem is that when the page loads and the JS is executed, the div will load before it is replaced. It's strange when I try this with another div, like a sidebar, it works as expected; that is, the div is replaced before it is displayed to the user. I cannot understand why it works for some divs and not for others.
I donβt know if this is really relevant, but on the Chrome side I have:
chrome.tabs.getSelected(null, function(tab) { if(tab.url.indexOf("search.php") > -1){ chrome.tabs.executeScript(null, {file: "fhrun.js", allFrames: false, runAt: "document_start"} ); } });
Any ideas why this doesn't work, or the best method I should use? Thanks!!
source share