[Greasemonkey] Re: Problems with 0.5.1

Nikolas Coukouma lists at atrus.org
Thu Sep 1 12:24:10 EDT 2005


Joe la Poutre wrote:
> Confirmed for PasswordComposer, with the following code:
>
> window.addEventListener("load", function(e) { mpwd_launcher(); }, true);
> window.addEventListener("resize", function(e) { mpwd_launcher(); }, true);
>
> The "onresize" event works all the time, the "onload" event does not
> (most of the time?).
Thought: Greasemonkey does not obtain a lock to prevent onload from 
firing before injection is complete. This is a separate issue from GM 
injecting scripts. Which one are we discussing here?

If the concern is the reliability of the load event, GM could add an 
event listener for load, record that it occurred, and make this 
information available to the script. So, you'd write:
function myLoadHandler() {
  // ...
}
if (GM_docLoaded) {
  myloadHandler();
} else {
  document.addEventListener("load", myLoadHandler, true);
}

Alternatively, GM could provide a special API for adding event handlers 
that would handle this for the author.

There's a small race condition here; the document may load between the 
time that the if statement is evaluated and before the listener is 
added. This is unavoidable because JavaScript lacks synchronization. I 
would suggest firing a load event, but then in-page scripts would see 
two load events and that might screw with things.

-Nikolas


More information about the Greasemonkey mailing list