Getting greasemonkey to click a button with onclick code

4,595 views
Skip to first unread message

Kip

unread,
Dec 30, 2009, 6:47:08 PM12/30/09
to greasemonkey-users
Hello everyone,
I just started using grease monkeys and am attempting to code a
greasemonkey script. This script is supposed to click this button
automatically when it goes to this website.

===website source code===
<form method="post" name="form1" id="s2t">

// Omitted info not related to button

<p style="display:none;"><input type="submit" name="save"
value="OK" id="fbbtn" class="btn" /></p>
<p><input type="button" value="OK" disabled=true id="fbsb"
class="btn" onclick="document.form1.action='/
s2t';document.getElementById('fbbtn').click(); this.disabled=true;" /
></p>
</form>
====================

Right now I am running this code:

var allInput, thisInput, allImg, thisImg;
allInput = document.evaluate(
'//input[@value]',
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
for (var i = 0; i < allInput.snapshotLength; i++) {
thisInput = allInput.snapshotItem(i);
if (thisInput.value == 'OK'){
thisInput.click();
}
}

It clicks the button but the webpage just refreshes itself instead of
doing what the button does when manually clicked.

Any suggestions to fix this so the button clicks as if manually done
is welcomed! Thanks in advanced.

cc

unread,
Dec 31, 2009, 1:06:56 AM12/31/09
to greasemon...@googlegroups.com, Kip
Personally, I use a utility function I borrowed from a popular GM script
on userscripts.org:

/* ===== Click on an element (borrowed from Facebook Fixer, @namespace http://userscripts.org/people/14536) ===== */
function click(elm) {
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, window, 0, 1, 1, 1, 1, false, false, false, false, 0, null);
elm.dispatchEvent(evt);
}

It works very nicely for everything I've tried it on so far. The basic
mechanism is to simulate a mouse click by dispatching a click event to
the element in question, which seems to do just about everything you
could wish (it does in fact trigger Firefox's built-in clicking
mechanisms, too).

HTH.

> --
>
> You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
> To post to this group, send email to greasemon...@googlegroups.com.
> To unsubscribe from this group, send email to greasemonkey-us...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/greasemonkey-users?hl=en.
>
>
>
> ____________________________________________________________________________________
> Download thousands of great movies, for free!
> http://click.lavabit.com/?r=MTc0
> ____________________________________________________________________________________
>

--
cc | pseudonymous |<http://carlclark.mp/>


Kip

unread,
Dec 31, 2009, 2:59:13 AM12/31/09
to greasemonkey-users
Thanks for the reply. This is a very nifty function I'll have to play
around with and see if I can get it to work. In my case there seems to
be a of elements that could be the one needing to be clicked. I was
looking around at the initMouseEvent parameters and it seems you can
offer a clientX, clientY, screenX, screenY to be clicked. Do you know
if I can have the mouse click somewhere on the screen without passing
an element?

On Dec 30, 10:06 pm, cc <carlcl...@lavabit.com> wrote:
> Personally, I use a utility function I borrowed from a popular GM script
> on userscripts.org:
>

> /* ===== Click on an element (borrowed from Facebook Fixer, @namespacehttp://userscripts.org/people/14536) ===== */

> > For more options, visit this group athttp://groups.google.com/group/greasemonkey-users?hl=en.

cc

unread,
Dec 31, 2009, 4:02:31 AM12/31/09
to greasemon...@googlegroups.com, Kip
You do have to pass an element, I think, but in this case, I suspect you could simply send the event to a parent node that's certain to contain the node to target (e.g. <body>, if nothing better), but I've never tried it.
That is, determine where the "click" should happen, x and y, and dispatch the corresponding coordinates (perhaps translated for the parent node's offsets) in the event to the parent node.

I strongly suggest taking a good look at the DOM2 event model spec at http://www.w3.org/TR/DOM-Level-2-Events/ — it's a reasonably readable description of what exactly can be done with events. Also, https://developer.mozilla.org/en/DOM_Events and https://developer.mozilla.org/en/Gecko-Specific_DOM_Events are good MDC resources for more tutorial/light-reference style purposes; read those closely too, perhaps before you read the spec.
Don't worry too much about reading the whole spec at once, of course; I'm still skimming through it, and re-reading bits here and there, and I'm a fast reader too.

The things to look for in particular in the spec are the bubbling and capture phases of event dispatch; try to understand as much of that as possible, and try different things out, too ;)
Hello everyone,
   I just started using grease monkeys and am attempting to code a
greasemonkey script. This script is supposed to click this button
automatically when it goes to this website.

(snip)

Any suggestions to fix this so the button clicks as if manually done
is welcomed! Thanks in advanced.
-- 
cc | pseudonymous | <http://carlclark.mp/>

esquifit

unread,
Dec 31, 2009, 5:28:21 AM12/31/09
to greasemon...@googlegroups.com
You'll find some more readable, high quality materials under:

http://www.quirksmode.org/js/contents.html#events

On Thu, Dec 31, 2009 at 10:02 AM, cc <carl...@lavabit.com> wrote:
>...

Reply all
Reply to author
Forward
0 new messages