Page 1 of 1

Block a single embedded script

Posted: Sun Mar 21, 2010 9:04 pm
by Nighthawk
Hi I've been testing something on my own webspace.

To prevent any other manipulating of the page content on the client side I could stop events from bubbling up using the stopPropagation function, by embedding this piece of javascript in the page:

Code: Select all

if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", function(ev) {ev.stopPropagation();}, false);
}else if(document.attachEvent){
document.attachEvent("DOMContentLoaded", function(ev) {ev.stopPropagation();});
}else{
document.DOMContentLoaded = function(ev){ev.stopPropagation();};
}
This would stop extentions like greasemonkey for example too as they only get fired after the DOM was loaded but at that point stopPropagation prevents it from running.

My question is, can I reverse this and prevent this specific embedded script from running, using the surrogate replacement method?

Re: Block a single embedded script

Posted: Mon Mar 22, 2010 10:38 am
by Giorgio Maone
Nighthawk wrote: My question is, can I reverse this and prevent this specific embedded script from running, using the surrogate replacement method?
Yes you can:

Code: Select all

(function() {
var stopPropagation = Event.prototype.stopPropagation;
Event.prototype.stopPropagation = function() {
  if (this.type != "DOMContentLoaded") stopPropagation.apply(this);
}
})();

Re: Block a single embedded script

Posted: Mon Mar 22, 2010 7:12 pm
by Nighthawk
Thanks a lot, this feature is a real nice addition, props!