JavaScript Auto Clicker

An “auto clicker” is a type of software or macro that automatically clicks a mouse button for the user.

The easiest way to use a JavaScript auto clicker would be to use our bookmarklet that clicks for you when you click and hold your mouse.

To use the bookmarklet:

  1. Drag the button below into your browser’s Bookmark Bar
  2. When you are on a page where you want to use the auto clicker, you can click the bookmarklet to activate it
  3. You need to hold down the left mouse button for the auto clicker to start clicking

Or, if you prefer you can manually create the bookmark and use the following code in the URL field:

javascript:(function(){let button=0,intervalId;document.body.addEventListener("mousedown",t=>{t.button===button&&(intervalId=intervalId||setInterval(()=>t.target.click(),1))}),document.body.addEventListener("mouseup",t=>{t.button===button&&(clearInterval(intervalId),intervalId=0)});})();
Code language: JavaScript (javascript)

Click Counter

You can test if the bookmarklet is working using the click counter below.

Simply add the bookmarklet above, click the bookmarklet to run it, then hold down your left mouse button on the button below!

Number of clicks: 0

Using a Different Mouse Button

If you want to use a different mouse button, you can change button = 0 in the code to the appropriate number below:

  • 0: Main button pressed, usually the left button or the un-initialized state
  • 1: Auxiliary button pressed, usually the wheel button or the middle button (if present)
  • 2: Secondary button pressed, usually the right button
  • 3: Fourth button, typically the Browser Back button
  • 4: Fifth button, typically the Browser Forward button

Source: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button

Auto Clicker Bookmarklet Source Code

The bookmarklet code is below:

let button = 0; let intervalId; document.body.addEventListener('mousedown', (e) => { if (e.button === button) { intervalId = intervalId || setInterval(() => e.target.click(), 1) } }); document.body.addEventListener('mouseup', (e) => { if (e.button === button) { clearInterval(intervalId); intervalId = 0; } });
Code language: JavaScript (javascript)

Alternative Method

Alternatively, if you would like to call the auto clicker from your browser console and specify the element that should be clicked, you can use the code below:

function autoClicker(elementSelector, interval) { var element = document.querySelector(elementSelector); setInterval(() => { // Dispatch a mouse event to the element element.dispatchEvent(new MouseEvent('click')); }, interval); } // Call the auto clicker for the #my-button element // with a 100 millisecond interval // (will be clicked 10 times per second) autoClicker('#my-button', 100);
Code language: JavaScript (javascript)

On Chrome you could open the Console (Ctrl + Shift + J) and then paste the code and press enter to run it.

Make sure to change the #my-button and 100 values first, representing the selector for the element to click and the interval between clicks.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *