1

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?

Jonathan
  • 738
  • 8
  • 22

1 Answers1

4

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

PAEz
  • 8,366
  • 2
  • 34
  • 27
  • Make sure that the [Content script](http://code.google.com/chrome/extensions/content_scripts.html) is executed at `document_start`, via `run_at`. For documentation of the `beforeload` extension event, see http://stackoverflow.com/q/4395525 – Rob W Jul 14 '12 at 14:16