Compatibility of Greasemonkey scripts with Firefox, Safari, Opera and Chrome (onload event)


Say you wanted a Greasemonkey script to be compatible with Safari and Chrome.

Whereas in Firefox the Greasemonkey scripts only run when the DOM is ready. Chrome, Safari and Opera seem to run the Greasemonkey scripts before the DOM is ready.

So what I am looking for is a solution that will only run a listener on the page, waiting for the DOM loaded/ready event, and on receiving that event, then run the rest of the script. If the browser they are using is Chrome, Safari or Opera. If they are using Firefox, just run the script straight away and not bother with the event listener.

How to code this?


Thanks NV, do you know if that script is also Google Chrome compatible?

Will the code


    // Code here run after 'load' event in all browsers.


also run on DOM ready in Google Chrome?

First of all, take a look at Nice alert UserJS. It works on Greasemonkey, Opera, and Safari Greasekit.

If you want to run some code after page load, event listeners not necessary at all.

... safari + opera, seem to run the gm scripts before the dom is ready.

No. Actually, it's more complicated. Opera has greasemokey compatible mode. All scripts named yada-yada.user.js run in compatible mode, and run after DOM is ready.

NOTE: wrap your code into anonymous function to prevent variable leakage into global window object (this happens in Opera).


  // Some code


Safari Greasekit works like Firefox Greasemonkey in this case.