[ACCEPTED]-How can I get greasemonkey to call a function on a page after it loads-greasemonkey

Accepted answer
Score: 27

Try using: unsafeWindow.myFunction();

More 1 details and info - http://wiki.greasespot.net/UnsafeWindow

Score: 17

One way to call a function in the original 22 page is like this:

location.href = "javascript:void(myFunction());";

It is a bit ugly. There 21 is also the unsafeWindow provided by GreaseMonkey too, but 20 the authors advise against using it.


Looks 19 neater but make sure you understand the 18 ramifications. From the manual:

unsafeWindow 17 bypasses Greasemonkey's XPCNativeWrapper-based 16 security model, which exists to make sure 15 that malicious web pages cannot alter objects 14 in such a way as to make greasemonkey 13 scripts (which execute with more privileges 12 than ordinary Javascript running in a 11 web page) do things that their authors 10 or users did not intend. User scripts 9 should therefore avoid calling or in any other 8 way depending on any properties on unsafeWindow 7 - especally if if they are executed for 6 arbitrary web pages, such as those with 5 @include *, where the page authors may 4 have subverted the environment in this 3 way.

In other words, your script elevates 2 the privileges available to the original 1 page script if you use unsafeWindow.

Score: 7

You could try using javascript event listeners.

These 3 execute code on response to object events 2 occurring (such as page load)

For example, to 1 execute code on page load:

window.addEventListener('load', function () 
    /* code goes here */

More Related questions