Building an in-browser Auto-clicker with JavaScript

Date: 2024-11-08 | create | tech | javascript |

I recently stumbled upon Neal.fun's Sun vs Moon clicker. I noticed that sun was losing to moon by a large margin which is a travesty as we all need the sun to survive. So I decided to do smth about it.

Here we build an auto-clicker bot that runs directly in our browser so we can automate clicks (and points!) for the team of our choice - here sun.

Sun vs Moon Clicker

The clicker game itself is pretty simple:

  • Two buttons - one for sun, one for moon
  • When you click a button it adds a point
  • Under the hood this sends a POST request to the backend

Thus to increase points for our team we just need to automate button clicks.

Creating an in-browser Auto-clicker with JavaScript

Most browsers allow you to run arbitrary javascript in them which is great for customizability. We'll leverage that to build ourselves a little autoclicker.

First we need to be able to find the button we need to click. By inspecting the HTML we can see that the sun button has an id - sun-btn. We'll use that to find it on the page.

Then we build a little script we can paste into the browser's console which will tell the browswer to click the button on an interval:

  • Create an interval which runs our code on a timer - here 200ms (~5 clicks / second)
  • Find the button using the id - sun-btn
  • Click the button
  • Every ~100 clicks print to console so we know it's alive

Code:

const clickInterval = setInterval(() => {
    const button = document.getElementById('sun-btn');
    if (button) {
        button.click();
        if(Math.random() <= 0.01) {
	        console.log('Button clicked ~100 times');
        }
    } else {
        console.log('Button not found');
        clearInterval(clickInterval); // Stop if button disappears
    }
}, 200);

Next

I tried increasing the click rate to 10 / second but quickly ran into rate limits so I think 5 / second is around the most we can do for a long time.

If you liked this post you might also like:

Want more like this?

The best / easiest way to support my work is by subscribing for future updates and sharing with your network.