[Greasemonkey] adding a "global" javascript function

David Kaspar dkaspars at asite.com
Thu Oct 13 11:12:17 EDT 2005


This was the very same reason I dived into Greasemonkey, to fix an IE
only function. In my case it was showModalDialog.

unsafeWindow.showModalDialog = function() {
            return window.confirm("Are you sure?");
}

Watch out if the site is using frames you may have to navigate the DOM
to insert the function into the correct frame:

if (unsafeWindow.frames.frameNameHere) {
        unsafeWindow.frames.frameNameHere.showModalDialog = function() {
            return unsafeWindow.confirm("Are you sure?");
        }
}

If you want to be compatible with older GM, I think this trick will help
if you use it at the beginning of your script:

unsafeWindow = unsafeWindow ? unsafeWindow : window;

(Test for unsafeWindow. If null use standard window)

David Kaspar



-----Original Message-----
From: greasemonkey-bounces at mozdev.org
[mailto:greasemonkey-bounces at mozdev.org] On Behalf Of Lenny Domnitser
Sent: Wednesday, October 12, 2005 10:07 PM
To: greasemonkey at mozdev.org
Subject: Re: [Greasemonkey] adding a "global" javascript function

On 10/12/05, Ilia K. <mail4ilia at gmail.com> wrote:
> window.helloworld = function() {
> ...
> So, I guess something has changed in the new Greasemonkey.

Yep. window is now an XPCNativeWrapper (it's a security thing) and
unsafeWindow is a new object that is the regular page window.

unsafeWindow.helloworld = function() {
  alert('Hello world!');
}

Make sure not to use unsafeWindow on scripts that execute on unlimited
sites, because a malicious page can take advantage of unsafeWindow
(hence "unsafe") to do all sorts of bad things.
_______________________________________________
Greasemonkey mailing list
Greasemonkey at mozdev.org
http://mozdev.org/mailman/listinfo/greasemonkey


More information about the Greasemonkey mailing list