7

Looking for a Javascript that do a left-mouse click on a image/button identified by ID or CLASS name, wait x seconds and repeat. And able to run in developer tools Console tap, in chrome and firefox.

Tried to write it myself, cause i figured it would be a simple code, but after 2 hours of trial and error with no luck, i am starting to run out of options.

Hopefully a Javascript pro out there got time to help out a very novice user ;)

Thanks

zyl1647
  • 153
  • 1
  • 3
  • 6

3 Answers3

2

If you're willing to use jQuery, you can use this simple function to do it:

window.setInterval(function(){
    $("#divToClick").trigger("click");
}, 1000);

That will call it every 1000 milliseconds, or 1 second

For a pure Javascript solution, you should take a look at How to trigger event in Javascript

or, if you're not interested in supporting IE, you can do it the simple way, with an Event() constructor, and event.dispatch()

Community
  • 1
  • 1
scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
2

What's wrong with

document.getElementById(id).click()
Diego Carrion
  • 513
  • 5
  • 8
2

You'd use the event constructor and dispatchEvent for that :

var support = true; // check if event constructor is supported

try {
    if (new MouseEvent('click', {bubbles: false}).bubbles !== false) {
        support = false;
    } else if (new MouseEvent('click', {bubbles: true}).bubbles !== true) {
        support = false;
    }
} catch (e) {
    support = false;
}

setInterval(function() {
    if (support) {
        var event = new MouseEvent('click');
    }else{
        var event = document.createEvent('Event');
        event.initEvent('click', true, true);
    }
    elem.dispatchEvent(event);
},1000);

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Tried the Javascript code you wrote on Fiddle, but does not click the image and returns no error. just output 44 and clicked. – zyl1647 Dec 16 '13 at 02:37
  • Did of course replace test with actual ID. – zyl1647 Dec 16 '13 at 02:43
  • @zyl1647 - That's what the console does when the same value is logged over and over, it just adds a number at the front for the number of times it was logged to the console, try this and see -> http://jsfiddle.net/hLnTy/4/ – adeneo Dec 16 '13 at 02:46
  • Sadly the same, does however output [18, 18, 4, 3, 200, Array[3], "DBBBACCDECCD"] when i manually click the image while script runs – zyl1647 Dec 16 '13 at 02:57
  • @zyl1647 - does the fiddle posted above actually output that, and in what browser – adeneo Dec 16 '13 at 03:00
  • @zyl1647 - I've just tested this in IE9 (It won't work without attachEvent in older versions), Chrome, Firefox and Opera, and it works everywhere ? – adeneo Dec 16 '13 at 03:02
  • Does output that yeh, and in Chrome. – zyl1647 Dec 16 '13 at 03:13
  • @zyl1647 - It works just fine here? What does this do -> http://jsfiddle.net/hLnTy/5/ – adeneo Dec 16 '13 at 03:27
  • v5 on Fiddle outputs "Uncaught TypeError: Cannot call method 'appendChild' of null" tried to test with http://www.brainbashers.com/10seconds.asp. v4 on said 10seconds site still dont click the image, however it does return next clicked x in line whenever i click the image manually. – zyl1647 Dec 16 '13 at 03:42