Is there a way I can use Greasemonkey to selectively remove scripts on a site?
Disclaimer: I know JS but don’t have much (read: none) direct experience with GM.
I just need to stop external scripts from loading (can’t use NoScript because I want to load other less annoying scripts).
I tried doing this:
// ==UserScript==
// @name turn_shit_off
// @namespace http://www.google.com
// @include http://www.xyz.com/*
// ==/UserScript==
window.onload = function() {
var d = document; // shorthand
var scripts = d.getElementsByTagName('script');
for(var i = 0; i < scripts.count; i++) {
if(scripts[i].src.indexOf('foobar.js') != -1) {
scripts[i].src = '';
}
}
}
But it doesn’t work for me.
Yes, Adblock Plus is the best way to go, if applicable.
The GM code may not fire in time to stop all the damage, but — for giggles — a working version of your code would be something like so:
You will see that it actually removes the script elements.
But, alas, altering or removing the
scriptelements, by itself, will have no effect most of the time. Except, maybe, in delayed-load/run code (things that fireonloador that have thedeferorasyncattributes set).You won’t have any effect until explicitly countering the handlers and timers that the bogus JS sets — which is highly page-specific and not always possible.
Run this code to see for yourself.