Jonathan
Jonathan

Reputation: 773

Chrome extension inject javascript before body finish loading

I'm triyng to create a Chrome extension that block a script before it is executed.

The script tag is in body tag and not in head.

Is it possible to do so?

in manifest.json I have set content_scripts like this:

"content_scripts": [
{
    "run_at": "document_start",
  "matches": ["http://website.it/*"],
  "js": ["dojob.js"]

}]

And my script is this one:

var cont = 0;

document.addEventListener("DOMNodeInserted", function(event){
        if(cont==0){
            alert(document.getElementsByTagName("script")[0].src);
            document.getElementsByTagName("script")[0].src = "";
            cont++;
        }

});

But the script still runs...

How cant I make it work?

Upvotes: 1

Views: 2347

Answers (1)

PAEz
PAEz

Reputation: 8542

Since your making src="" I take it that the js is external. If thats the case then you could use the beforeload event or webrequest api to block the loading of the script.

function doBeforeLoad(event){
    if (event.srcElement.tagName=="SCRIPT" && event.srcElement.src=='test.js') {
        event.preventDefault();
    }
}

document.addEventListener('beforeload', doBeforeLoad , true);

http://code.google.com/chrome/extensions/webRequest.html

Upvotes: 4

Related Questions