0

Basically I am using the Firefox plugin called "greasemonkey" and I am attempting to write a script who's purpose is to find a specific class specified image on each page and click on it. Provided the item to be "clicked" has an href, this is accomplished fairly easily due to several other posts here, some using anchors and such, but assuming there is no href to follow, is there a way to "click" on an image or button?

For example, say I want to make my way through a survey and they have several check boxes or radio buttons. Is there a way to select the button using a script in greasemonkey?

For reference, this is what I have done in the past to "click" on a button that had a href attribute.

$("a.tip").each(function(){
if($(this).attr("caption")=="captionName"){
$.get("http://www.website.com" +$(this).attr("href"),window.location.reload());
return;
    }
});

*As far as the capabilities of greasemonkey, I am not certain what it can and can't do. diveintogreasemonkey.org appears it is no longer being hosted, therefore making the documentation fairly hard to come by.

Also, this would be different than a click listener because I am wanting the script to do the clicking. Any reference material you might have that could point me in the right direction would also be appreciated.

One last note, I am fairly new to this field of "web code" so if you would be so kind to explain it like I don't know anything, I won't be offended at all.

5
  • greasemonkey's homepage: greasespot.net Commented Apr 13, 2012 at 20:15
  • thank you very much, the firefox addon link only referenced my aforementioned site. I'll make sure I bookmark this. Commented Apr 13, 2012 at 20:20
  • 1
    What's an example of the html used to make the button? Commented Apr 13, 2012 at 20:21
  • You may want to take a look at this, as it seems very close to your own question: stackoverflow.com/questions/6466856/… Commented Apr 13, 2012 at 20:42
  • cool, I didn't see this one during my search. I'll mess around with it a little more with this info. As far as some sample html, it might take me a bit seeing as I am currently on my smart phone and pulling source html isn't the easiest thing to do. Commented Apr 13, 2012 at 21:04

2 Answers 2

0

You can click on just about anything by generating an actual click event. No href is required and this technique is great for activating modern, javascript-powered controls on "web 2.0" sites.

This method works even when .click() or jQuery's `.click() don't. (But, no method works on every control.)

Plain JavaScript (Works on Firefox, Chrome, Greasemonkey,and others):

var clickTarget = document.querySelector ("Any valid CSS selector");
var clickEvent  = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
clickTarget.dispatchEvent (clickEvent);


jQuery version:

var clickTarget = $ ("Any valid jQuery selector");
var clickEvent  = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
clickTarget[0].dispatchEvent (clickEvent);


Note that both of these snippets are for one link/button/etc. at a time.
On AJAX-driven pages, be sure you check that the target node exists first, and wait if necessary.

Sign up to request clarification or add additional context in comments.

Comments

0

Since you're using JQuery, i assume there is a @require is your script.
// @require http://code.jquery.com/jquery.min.js

Now, why isn't click() working for you?
Do you get any errors in the console?

I tried this code

setTimeout(function(){
    alert('about to click/select a radio button')
    $('#radio2').prop('checked', true);
    alert('ready to click images with class .imagem ?')
    $('.imagem').click()
}, 2000)

here: http://jsfiddle.net/QkCs4/5/

worked fine for me.
see if it works for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.