0

I'm trying to make an autoclicker for a website but the auto clicker is only 400 cps I'm trying to see if it can go any faster

var event = new KeyboardEvent('keydown',{key:'g',
    ctrlKey:true
});

setInterval(function(){
   for(i=0; i< 100; i++){
     document.dispatchEvent(event);
   }
}, 0); 2256
256
Rifky Niyas
  • 1,737
  • 10
  • 25
Blue Duck
  • 56
  • 7

1 Answers1

1

I don't know what website is capturing and how, but here is a little bench you can run:

let sent = 0,
  received = 0,
  pack = 100,
  date = new Date().getTime(),
  stop = false,
  timer;

document.addEventListener("keydown", e => {
  if (++received > 1000000)
    stop = true;

  if (!(received % ((pack * 100 < 10000 ? pack *100 : 10000)))) {
    const time = (new Date().getTime() - date);
    console.log("sent:" + sent, "received:" + received, "Pack:" + pack, (pack * 100 < 10000 ? pack *100 : 10000), "Time:" + time, "CPS:" + ~~(received / time * 1000));
  }
});
var event = new KeyboardEvent('keydown', {
  key: 'g',
  ctrlKey: true
});

function init()
{
  document.querySelector("button").textContent=stop?'START':'STOP';
  document.querySelector("select").value = pack;
  received = sent = 0;
  date = new Date().getTime();
  clearInterval(timer);
  if (stop)
    return;

  timer = setInterval(function() {
    if (stop)
      return init();

    for (let i = 0; i < pack; i++) {
      sent++;
      document.dispatchEvent(event);
    }
  }, 0);
}
init();
<button onclick="stop=!stop;init()"></button>
<select oninput="pack=this.value;init()">
  <option value="1">1</option>
  <option value="10">10</option>
  <option value="100" selected>100</option>
  <option value="1000">1000</option>
  <option value="10000">10000</option>
  <option value="100000">100000</option>
</select>
vanowm
  • 9,466
  • 2
  • 21
  • 37
  • I got around 24000 on a pretty good machine. I guess this isn't really a worry, and very machine dependent. – somethinghere Aug 14 '21 at 06:53
  • 1
    That only measures at which frequency setInterval(fn, 0) fires (4ms after the 5th call). You can have far higher rate by using a MessageChannel as shown [here](https://stackoverflow.com/questions/61338780/is-there-a-faster-way-to-yield-to-javascript-event-loop-than-settimeout0). https://jsfiddle.net/bea61ypu/ But that misses the point. The events are fired synchronously, so if we remove the scheduling part, what this code would measure optimally is only the time taken to execute the callback. – Kaiido Aug 14 '21 at 07:03
  • Please read [answer] and always remember that you are not merely solving the problem at hand, but also educating the OP and any future readers of this question and answer. Thus, please [edit] the answer to include an explanation as to why it works. – Adriaan Mar 25 '22 at 08:27