It seems that it is not possible to prevent a piece of javascript from
running if it is put in a <body onload=" ">. A helpdesk website I'm
using every now and then is using this to set the focus after loading
the page.
The things I tried:
1.
var all;
all = document.evaluate("//body", document, null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < all.snapshotLength; i++) {
var t = all.snapshotItem(i);
t.onload = null;
}
2.
document.body.onload = null;
By putting an alert() call in the script I could see that my GM script
was executed before the body onload, but I could not prevent that
piece of Javascript from running at all...
I have used the first loop to prevent an onunload from running but I
think this event is already ready to be delivered and it is too late
for GM to prevent it.
In the mean time I'm just modifying the tag it is looking for, but I
find it a bit annoying that I can't prevent the code from running at
all.
Regards,
Berend.
First, this is a good site to read for new user script authors:
http://wiki.greasespot.net/Main_Page
Then, specifically, from the links:
http://www.oreillynet.com/pub/a/network/2005/11/01/avoid-common-greasemonkey-pitfalls.html
Pitfall #2
To solve, you need this, but note the warning at the top:
http://wiki.greasespot.net/UnsafeWindow
Problem:
http://wiki.greasespot.net/XPCNativeWrapper#Expando_Properties
(Ugly) solution:
location.href = 'javascript:void(document.body.onload = null);';
I've tried the suggestions in the followups but up to now nothing worked.
As I said before I did successfully use a similar GM script to prevent
a body.onunload from running. I don't know why it does not work for
the body.onload
Attached is a small html file that mimics the body.onload usage from
the mentioned website. I would be grateful if somebody can come up
with a working userscript that prevents the alert () call in this html
file from being called.
I have tested Firefox 2.0.0.1 on Windows XP and Firefox 1.5.0.7 on Debian Linux.
Greasemonkey version 0.6.7.20070131.0
These are the UserScript constructs I've tried:
// ==UserScript==
// @name Remove onload
// @namespace none
// @description Remove onload
// @include */body_onload.html
// ==/UserScript==
alert('Greasemonkey called');
location.href = 'javascript:void(document.body.onload = null);';
unsafeWindow.document.body.onload = null;
unsafeWindow.document.body.load = null;
function my_func(evt) {
alert('my_func called');
evt.preventDefault();
}
document.body.addEventListener("load", my_func, true);
document.body.addEventListener("onload", my_func, true);
unsafeWindow.document.body.addEventListener("load", my_func, true);
unsafeWindow.document.body.addEventListener("onload", my_func, true);
Regards,
Berend.
document.body.setAttribute('onload','');
Yes, this works fine. Am I correct to conclude that the 'onload' is
not an event? That seems strange...
Anyway, thanks a lot for your help.
Regards,
Berend.
Actually, onload is a custom property (supported by Mozilla, sometimes
referred to as 'expando' see [3]) It is custom because it does not
form part of the interface HTMLBodyElement [4] which is the base of
the HTML BODY tag.
In this case, the DOM property 'onload' is not available to GM for the
reasons pointed out by Lenny [2]. However, the standard HTML
attribute is always available.
Or something like that ;-)
[1] http://xulplanet.com/ndeakin/archive/2004/12/22/
[2] http://wiki.greasespot.net/XPCNativeWrapper#Expando_Properties
[3] http://www.xulplanet.com/ndeakin/article/278/
[4] http://www.xulplanet.com/references/objref/HTMLBodyElement.html
2007/2/28, brei...@gmail.com <brei...@gmail.com>: