Greasemonkey: stop running JS
The html page has the following:
<script type="text/javascript"> // some code </script> My greasemonkey script should prevent the script from starting . How can i do this?
Update: I understand that in the general case this is not possible. However, in my particular case, may I 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 any way to define window.devicePixelRatio before running the inline script?
This is now possible with @run-at document-start and HTML5 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); Re:
Is there a way to define
window.devicePixelRatiobefore running the inline script?
Now. For instance:
// ==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 general:
Since Firefox is version 4, now it is only possible for Firefox. Use the checkForBadJavascripts utility to use the power of beforescriptexecute . For instance:
// ==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 completely blocks the first inline script containing window.devicePixelRatio . If you want to selectively modify parts of this script, see this answer and / or this answer .