[Greasemonkey] Re: Clicking a Button Programmatically

A. Alfred Ayache alfred at lastbyte.ca
Wed May 24 17:54:10 EDT 2006


OK, here's where I'm at:

I've tried a couple of alternatives and neither seems to work for me...

I started with the example from chapter 4.22 of the DiveIntoGreasemonkey.org book:

 >>>
  	function newclick(event) {
		var target = (event ? event.target : this);
		
		target._click();
	}
	
	window.addEventListener('click', newclick, true);
	
	HTMLInputElement.prototype._click = HTMLInputElement.prototype.click;
	HTMLInputElement.prototype.click = newclick;

	refbtn.click();
<<<

The original example used the HTMLFormElement class.  Since the target page used 
a button input element, I switched to HTMLInputElement.  Sadly, not only did 
this not work, but I got the following error:

Error: HTMLInputElement.prototype has no properties

So, I moved on to a suggestion by Andre (?), which apparently was sent to him 
from Jeremy (Dunck?).

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

This seemed to work better, but still not 100%.  The reason I'm thinking it 
actually worked, is that the log shows the "evt" text (as well as others) coming 
up twice.  It's not 100% because the effect we're looking for here isn't 
happening.  That effect is as follows:

The form has a Rows dropdown and a Refresh button.  By default the form shows 10 
rows of data.  I need the script to detect whether the dropdown is set for All. 
  If it's not, it sets it to All and hits the click event on the Refresh button.

So, by the time this is all finished, the dropdown is set to All, and we've seen 
the log entries twice, but we still only have 10 rows showing in the page...  :(


A. Alfred Ayache
http://lphs76.ca             - Reunion community
http://www.rentersPlus.com   - Apartment Search
http://www.lastbyte.ca       - Web Design, eCommerce, PHP/MySQL, Java, Oracle



More information about the Greasemonkey mailing list