Home > Enterprise >  Click A button faster
Click A button faster

Time:01-29

I'm new here and may sound dumb but I need help. we are playing a clicker game on a website where a button will be displayed at a random time, first 7 players to click it will be shown as the winners of that round. I wrote the clicker function bellow and always use it on my Chrome console.

(function () {
  setInterval(() => {
    const button = document.querySelector(“.click-button);
    if (!button) return;
button.click();
   }, 5);
})();

I was playing it with mobile network of 50 - 70 mbps of speed and was among the top 5 until when other players started using VPS Machine which have over 4gbps of internet speed. I now have a VPS running Windows server 2022 but cant still perform better. So My question is, Is it that my VPS speed is slower than their own or My Laptop have less specification than their own or I need a better javascript code than the one above? Someone should please help me. Thanks.

Running the above code on a VPS server with a download speed of 4.3gbps and upload speed of 4.1gbps through a browser console and click faster

CodePudding user response:

Not sure why your question was voted down. Perhaps if people feel that this is not a programming question but is about networking...

Your code can be a little faster by caching the function and the button outside the interval

(function() {
  const button = document.querySelector(".click-button");
  const buttonClick = () => button.click();
  if (button) setInterval(buttonClick, 5);
})();

CodePudding user response:

Instead of polling for the button, you could use a MutationObserver that will notify you when the button changes or is inserted into the document. Unless you can get to the event that triggers the button activation, this is probably the fastest you can do.

Assuming that the button is inserted into a <div id="button-container"></div>:

const callback = mutations => {mutations.forEach(m => m.addedNodes.forEach(node => node.tagName === 'BUTTON' && node.click()))}
const observer = new MutationObserver(callback)
const container = document.getElementById('button-container')
observer.observe(container, {childList: true})

Have a look at this codepen

  • Related