Is it possible to have Greasemonkey scripts run before anything else on the page?
I'm aware of @run-at document-start
, but this appears to run immediately after the <HTML>
tag. Normally this isn't a problem, but if the page is misformatted as in the example below, there doesn't seem to be anything I can do.
I'd appreciate 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...
Is it possible to have Greasemonkey scripts run before anything else on the page?
I'm aware of @run-at document-start
, but this appears to run immediately after the <HTML>
tag. Normally this isn't a problem, but if the page is misformatted as in the example below, there doesn't seem to be anything I can do.
I'd appreciate 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...
Share
Improve this question
asked Apr 2, 2009 at 3:07
anschauunganschauung
3,7683 gold badges27 silver badges35 bronze badges
3 Answers
Reset to default 4Actually, @run-at document-start
does pretty much run the GM script before anything from the page.
In your case, the following code will work in conjunction with @run-at document-start
:
_oldAlert = alert;
unsafeWindow.alert = function () {};
/*-- Optionally, wait for the DOM, then set an onload handler to restore
alert().
*/
It's not possible, because HTML requres that scripts are executed during parsing or not at all, e.g. this is allowed:
<script> document.write('<!'+'--'); </script>
If browser goes past this script without executing it, it will see pletely different document, therefore you can't analyze DOM of HTML document before scripts run.
Opera solves this problem in UserJS by firing BeforeScript
events, allowing UserJS to change/remove scripts at the very last moment.
Have you tried:
(function() {
var yourFunction = function()
{
// ...
}
window.addEventListener("load", yourFunction, false);
})();