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?
1 Answer 1
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);
answered Jul 14, 2012 at 14:09
PAEz
8,5422 gold badges37 silver badges28 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Rob W
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 default