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?

asked Jul 13, 2012 at 14:18

1 Answer 1

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

answered Jul 14, 2012 at 14:09
Sign up to request clarification or add additional context in comments.

1 Comment

Make sure that the Content script is executed at document_start, via run_at. For documentation of the beforeload extension event, see stackoverflow.com/q/4395525

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.