Quantcast

Getting greasemonkey to click a button with onclick code

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Getting greasemonkey to click a button with onclick code

Kip-12
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.

--

You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/greasemonkey-users?hl=en.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Getting greasemonkey to click a button with onclick code

cc-21
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.

On 2009-12-30 15:47, Kip wrote:

> 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.
>
> --
>
> You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to [hidden email].
> 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/>


--

You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/greasemonkey-users?hl=en.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Getting greasemonkey to click a button with onclick code

Kip-12
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 <[hidden email]> 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) ===== */
> 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.
>
> On 2009-12-30 15:47, Kip wrote:
>
>
>
> > 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.
>
> > --
>
> > You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
> > To post to this group, send email to [hidden email].
> > To unsubscribe from this group, send email to [hidden email].
> > For more options, visit this group athttp://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/>

--

You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/greasemonkey-users?hl=en.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re: Getting greasemonkey to click a button with onclick code

cc-21
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 ;)

On 2009-12-30 23:59, Kip wrote:
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 [hidden email] 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) ===== */
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.

On 2009-12-30 15:47, Kip wrote:
    
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/>

--

You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/greasemonkey-users?hl=en.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re: Getting greasemonkey to click a button with onclick code

esquifit-2
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 <[hidden email]> wrote:

>...
> 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.

--

You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/greasemonkey-users?hl=en.


Loading...