6

A page has the following in the html:

<script type="text/javascript">
  // some code
</script>

My greasemonkey script needs to prevent that script from running. How can I do this?


Update: I understand that in the general case this is impossible. However, in my specific case, I may have a loophole?

<script type="text/javascript">
  if (!window.devicePixelRatio) {
    // some code that I -don't- want to be run, regardless of the browser
  }
</script>

Is there some way I can define window.devicePixelRatio before the embedded script runs?

Mala
  • 14,178
  • 25
  • 88
  • 119

4 Answers4

14

This is now possible with @run-at document-start and Firefox's beforescriptexecute. Tested only in FF24.

// ==UserScript==
...
// @run-at         document-start
// ==/UserScript==

//a search string to uniquely identify the script
//for example, an anti-adblock script
var re = /adblock/i;

window.addEventListener('beforescriptexecute', function(e) {

    if(re.test(e.target.text)){

        e.stopPropagation();
        e.preventDefault();
    }

}, true);

beforescriptexecute was rejected for HTML 5 in 2016, and Chrome is unlikely to implement it.

It does not run for <script> nodes inserted by other scripts.

leewz
  • 3,201
  • 1
  • 18
  • 38
aekotra
  • 141
  • 1
  • 4
  • +1 but note that this will work in FF only and has been supported since FF version 4. Also, it might be better to use `.textContent` instead `.text` in this case. – Brock Adams Sep 29 '13 at 02:06
3

Re:

Is there some way I can define window.devicePixelRatio before the embedded script runs?

There is now. Like so:

// ==UserScript==
// @name     _Pre set devicePixelRatio
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @run-at   document-start
// @grant    none
// ==/UserScript==
//-- @grant none is imporatant in this case.

window.devicePixelRatio = "Unimportant string or function or whatever";


In the general case:

Since Firefox version 4, this is now possible on Firefox only. Use the checkForBadJavascripts utility to leverage the power of beforescriptexecute. Like so:

// ==UserScript==
// @name     _Block select inline JS
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  https://gist.github.com/raw/2620135/checkForBadJavascripts.js
// @run-at   document-start
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

checkForBadJavascripts ( [
    [false, /window\.devicePixelRatio/, null]
] );


This blocks the first inline script, that contains window.devicePixelRatio, entirely. If you want to selective modify parts of that script, see this answer and/or this answer.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
2

User scripts run after the page is loaded, so you aren't able to.

Unless, the code uses the "onload" event.

User scripts are executed after the DOM is fully loaded, but before onload occurs. This means that your scripts can begin immediately and don't need to wait for onload.

Aaron Butacov
  • 32,415
  • 8
  • 47
  • 61
  • Thanks. I guess I'll do my best to try and undo what the script does. I've updated the question with something that -may- make it possible... or is it the case that by the time my GM script runs, it's too late anyway? – Mala Jul 15 '10 at 05:16
  • Not with a user script. The embedded JavaScript runs as soon as it is loaded by the browser. User scripts run after the entire page is loaded. – Aaron Butacov Jul 15 '10 at 05:41
1

Another option is to use Privoxy instead of GreaseMonkey. You just use Privoxy as your proxy (on localhost) and search/replace the strings you don't like.

johndodo
  • 17,247
  • 15
  • 96
  • 113