Acting like a user?

25 views
Skip to first unread message

Robert

unread,
Jan 19, 2012, 3:23:09 PM1/19/12
to greasemon...@googlegroups.com
Hi all,

I am wondering if it could be possible that my script acts like the user itself? Well. It is one thing to modify the page. Attach handlers ecc. But what I'd like to do is something like pressing a button (so that the same happens like the user presses the button). So that I need not take care to find out what is done to do it myself.

Something like "GM_click(id)".

Or is there already a way to do this? (The site I want to interact with attaches onclick-handlers and it is not os easy for me to find out what they are really doing. So the easiest way would be to just hit that damned button by my userscript! :))) )


I know it is possible to act like the user, e.g. iMacros does do it:


Well, I do not need iMacro. But just a way to simulate a click.



Regards,
Robert

Anthony Lieuallen

unread,
Jan 19, 2012, 3:29:37 PM1/19/12
to greasemon...@googlegroups.com
http://wiki.greasespot.net/Generate_Click_Events

On Thu, Jan 19, 2012 at 3:23 PM, Robert <enk...@uni.de> wrote:
Something like "GM_click(id)".

Robert

unread,
Jan 19, 2012, 3:42:36 PM1/19/12
to greasemon...@googlegroups.com
Ah! Great! Tnx!

This seems to be what I want:
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, true);
el.dispatchEvent(evt);

Cheers,
Robert


-------- Original-Nachricht --------
Betreff: Re: [greasemonkey-users] Acting like a user?
Von: Anthony Lieuallen <aran...@gmail.com>
An: greasemon...@googlegroups.com
Datum: Thu Jan 19 2012 21:29:37 GMT+0100
http://wiki.greasespot.net/Generate_Click_Events

Robert

unread,
Jan 19, 2012, 4:24:41 PM1/19/12
to greasemon...@googlegroups.com
I have written a more generic function the perfectly fits my needs. So if someone is interessted, feel free to use it.


    // simulate mouse click on element
    function $click( elm, root ) {
        // create mouseclick element

        var evt = document.createEvent( 'MouseEvents' );
        evt.initEvent( 'click', true, true );
       
        // id, class, tag, name or node itself?
        var node;
        if( typeof elm == 'string' ) {
            if( elm[0] == '#' ) node = $( elm, root )
            else                node = $( elm, root )[0];
        } else                  node = elm;
       
        // click node
        node.dispatchEvent( evt );
    }
   
   
    // Get Elements
    function $( q, root, single ) {
        if( root && typeof root == 'string' ) {
            root = $( root, null, true );
            if( !root ) { return null; }
        }

        root = root || document;
        if( q[0] == '#' ) { return root.getElementById(q.substr(1)); }
        else if( q[0] == '/' || ( q[0] == '.' && q[1] == '/' ) ) {
            if( single ) { return document.evaluate(q, root, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; }
            return document.evaluate(q, root, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
        } else  if( q[0] == '.' ) { return root.getElementsByClassName(q.substr(1)); }
        else    if( q[0] == ':' ) { return root.getElementsByName(     q.substr(1)); }
        else    if( q[0] == '<' ) { return root.getElementsByTagName(  q.substr(1)); }

        return root.getElementsByTagName(q);
    }


Regards,

Robert

-------- Original-Nachricht --------
Betreff: Re: [greasemonkey-users] Acting like a user?
Von: Robert <enk...@uni.de>
An: greasemon...@googlegroups.com
Datum: Thu Jan 19 2012 21:42:36 GMT+0100
--
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.

--
  _ o    Robert H.       http://kurzlink.de/Enkidu70
 |<)_/#
 TT <T   enk...@uni.de

  "Du wirst da hinein geboren was Du nicht bist, um zu erfahren wer Du wirklich bist!"
Reply all
Reply to author
Forward
0 new messages