// some code My greasemonkey script ...">

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?

+4
source share
4 answers

Custom scripts run after the page loads, so you cannot.

Otherwise, the code uses the "onload" event.

Custom scripts are executed after the DOM is fully loaded, but before loading takes place. This means that your scripts can start immediately and do not need to wait for downloads.

+2
source

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); 
+12
source

Another option is to use Privoxy instead of GreaseMonkey. You simply use Privoxy as a proxy server (on the local host) and search / replace strings that you don't like.

+1
source

Re:

Is there a way to define window.devicePixelRatio before 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 .

+1
source

Source: https://habr.com/ru/post/1315752/


All Articles