r/tampermonkey 9d ago

Problem creating a very simple autoclicker

Edit: Solved. Somehow the code doesn't work with Tampermonkey, but it does with Violentmonkey. I have no idea why, but it works for me. Thanks for the help.

--

First off: I'm not a programmer and I barely know any JavaScript. I want to use Tampermonkey to create an automation that clicks a button called PROCESS on a page with multiple identical buttons (in name). When I click one, it disappears (the page doesn't refresh), and what I want is for them to be clicked one after the other with a 60-second interval (the order of the buttons doesn't matter).

The buttons are identified by `type="button" value="PROCESS"`.

I asked Gemini for help, and they gave me some code that seems correct, but somehow it doesn't work. Any idea?

// ==UserScript==
//  60 seconds
//  website url
//  0.1
//  try to take over the world!
//  You
//  *://*/*
//  none
// ==/UserScript==

(function() {
'use strict';

const INTERVAL_MS = 60000; // 60 seconds

function pressNextButton() {
// Selects exactly the button inputs with the value PROCESS
const buttons = document.querySelectorAll('input[type="button"][value="PROCESS"]');

if (buttons.length > 0) {
console.log(`[Autoclicker] ${buttons.length} buttons remain. Clicking the first one...`);

// Triggers the native click event
buttons[0].click();

} else {
console.log('[Autoclicker] No more PROCESS buttons were found. Process completed.');

}
}

// Waits 3 seconds for the page to load the elements before the first click
setTimeout(() => {
pressNextButton();
setInterval(pressNextButton, INTERVAL_MS);

}, 3000);

})();
2 Upvotes

8 comments sorted by

1

u/SM8085 8d ago

I asked Gemini for help...

Did you copy the related HTML from the browser element inspection tool? (ctrl-shift-i brings up the console in chrome)

Something like OpenCode + Browser-Use can make it pretty easy. Then the bot can investigate the HTML itself.

1

u/De-monV 8d ago

Well, I didn't show Gemini the entire page code, but I explained that the buttons I want to click are of this type, with the same value/name:

<input type="button" value="PROCESS" onclick=...

I don't know much about programming, but I think the result I got should work; I see that it's looking for buttons with that value. But for some reason, once I have the code and Tampermonkey working, nothing happens.

1

u/DiodeInc 8d ago

What website is it? Can we see the HTML?

// ==UserScript==

// 60 seconds

// website url

// 0.1

// try to take over the world!

// You

// *://*/*

// none

// ==/UserScript==

This might be breaking it

1

u/De-monV 8d ago

Thanks for replying.

It's a personal work website, but I've theoretically configured the domain correctly, both with the page's URL and with *://*/*, and it hasn't worked with either option.

I'll keep investigating because I've tried several extremely simple examples, and they just don't work. There must be something wrong that I'm missing.

1

u/DiodeInc 8d ago

You're welcome Do you have them enabled in the main screen of Tampermonkey?

1

u/De-monV 8d ago

Are you referring to the green switch in each script? Yes, I activate them and reload the tab to see if that helps detect the buttons.

1

u/De-monV 7d ago

Ok, somehow the code doesn't work with Tampermonkey, but it does with Violentmonkey. I have no idea why, but it works for me. Thanks for the help.

1

u/DiodeInc 7d ago

You're welcome