0

In order to block some JavaScript that I don't want to execute when I load a page, I would like to have a Greasemonkey script that deletes everything between two HTML comment tags. For example:

<!-- sub script -->
 Lines containing script elements to be removed
<!-- end sub script -->

I have previously tried removing the script elements using a sample script that was suggested to me, but it isn't working as expected. That script was this:

var script = document.querySelectorAll('script[src*="example.com"]');
for (var i = 0, len = script.length; i < len; i++) {
   script[i].parentNode.removeChild(scriptscript[i]);
}

Simply put, it doesn't remove the script elements as I had hoped it would. I know that the comment elements are consistent between pages and if I could use those as the markers to begin and end a search and simply delete everything in between, I believe it would solve the problem.

Any guidance would be appreciated. To say I'm new to JavaScript would be an understatement, but I'm a quick learner.

user247702
  • 23,641
  • 15
  • 110
  • 157
B.Rossow
  • 136
  • 1
  • 8

1 Answers1

1

You can't remove a JavaScript script by just removing the <script> which included it.

That's because once browser parses the <script>, the script runs, and there's no way to undo that.

But on some browsers you can block the script just before it runs, using beforescriptexecute event:

window.addEventListener('beforescriptexecute', function(e) {
    if (~e.target.src.indexOf('example.com')) {
        e.preventDefault();
    }
}, true);

Note your GM script must run before the script you want to block, to be sure, use

@run-at document-start
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • So I've tried this with no success. Should I be able to use just what you provided, substituting the relevant domain from which I want to block scripts, as the entire body of the GM script? When I do that, the scripts still load. Will this catch all instances of scripts from this domain rather than just the first? There are multiple scripts on the page. – B.Rossow May 01 '14 at 18:32
  • Yes, no more code is needed to make it work. And it should block all scripts loaded from URLs which contain `'example.com'`. See a [demo](http://jsfiddle.net/7VeBL/) – Oriol May 01 '14 at 18:36
  • Okay, thanks. Somehow those scripts are still getting executed. Maybe they're getting injected after the page loads by another script I haven't blocked? If necessary I can block the offending domain using AdBlockPlus, which I know works, but I was hoping to use this as a learning exercise. – B.Rossow May 01 '14 at 18:39
  • @B.Rossow In which browser and version are you trying it? Check `'onbeforescriptexecute' in window` to know if it's supported. – Oriol May 01 '14 at 18:50
  • +1, but for any ` – Brock Adams May 02 '14 at 02:24
  • @BrockAdams I also think Adblock is useful. But faster and less resource intensive? Last time I tried it, it slowed down Firefox startup by 15 seconds and made it eat 300MB RAM more. I prefer the script above :) – Oriol May 02 '14 at 17:27
  • @Oriol, You're kidding, right? I haven't noticed *Adblock* slowing startup and Firefox memory use is notoriously hard to pin down, but so what? AdBlock saves me *minutes* each and every day by blocking gigabytes of crap from ever downloading. As a bonus, I don't even see all kinds of annoying, blinking, chattering garbage that seems to spam websites. Also, third-party cruft and ads are now the largest source of hack attempts on websites. Adblock stops a lot of that cold. – Brock Adams May 03 '14 at 00:48