How can I run Greasemonkey before * anything else on the page?

Is it possible for Greasemonkey scripts to run earlier on the page?

I know @run-at document-start , but it seems to run right after the <HTML> . This is usually not a problem, but if the page is not correctly defined, as in the example below, it seems I can do nothing.

I would be grateful for any suggestions or ideas. Thanks!

 <script>alert('This is an annoying message.');</script> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> ...etc... 
+4
source share
3 answers

This is not possible because HTML requires that scripts be executed during parsing or not executed at all, for example. It is allowed:

 <script> document.write('<!'+'--'); </script> 

If the browser does not pass this script without executing it, it will see a completely different document, so you will not be able to parse the DOM of the HTML document before running the scripts.

Opera solves this problem in UserJS by BeforeScript events, allowing UserJS to modify / delete scripts at the very last moment.

+2
source

Actually, @run-at document-start does pretty much run the GM script before anything from the page.

In your case, the following code will work along with @run-at document-start :

 _oldAlert = alert; unsafeWindow.alert = function () {}; /*-- Optionally, wait for the DOM, then set an onload handler to restore alert(). */ 
+3
source

You tried:

 (function() { var yourFunction = function() { // ... } window.addEventListener("load", yourFunction, false); })(); 
-1
source

All Articles