[Greasemonkey] Re: Clicking a Button Programmatically

Andre gm at andrecgn.de
Wed May 24 11:07:46 EDT 2006


Hi Alfred,

if 'elem' is your button, you need to do the following:

evt=document.createEvent("MouseEvents");
if(evt && evt.initMouseEvent) {
       evt.initMouseEvent("click",true,true,document.defaultView,
                          1,0,0,0,0,false,false,false,false,0,null);
       elem.dispatchEvent(evt);
}

(Code received from Jeremy (I think) some months ago. Thanks!!)

Beware that somewhere on Mozilla.org the documentation on clicking 
buttons is (or was) just wrong. I forgot the link to the good 
documentation :-(

The "onclick" attribute is not directly accessible due to the nature of 
the wrappers around all page objects (see Mark's wonderful book "Dive 
Into Greasemonkey book" about details on wrappers).

Calling (or evaluating) the contents of the onclick attribute is a 
security risk, because the called (evaluated) code would be executed in 
GM context with raised privileges.

Firing the click event as described above should be safe though.

Cheers, Andre

A. Alfred Ayache wrote:
> I've read section 4.22 of the Dive Into Greasemonkey book.  But I'm 
> looking at a form with a regular button, rather than a submit.  So how 
> do I fire an onClick event?
> 
> If it helps, here's the HTML:
> 
> <input value="Refresh" onclick="airtimeDetailForm.submit()" type="button">
> 
> Thanks for your help,



More information about the Greasemonkey mailing list