1

So I have seen multiple questions on this but i don't think i have seen one that has been solved. I am also super new to JS, jQuery and I have never made a userscript before so I still dont know a lot of words or concepts. So feel free to dumb it down for me :).

I want to autoclick a particular button on a site that pops up only for a split second.
Tons of people are trying to click on the same button so you have to be lightning fast.

I wanted to make a script to make this process easier.

I use an auto refresh extension to help refresh the page which i would like to use with the script as well.

What im struggling with:

1. Create a userscript with tampermonkey and javascript/jquery to locate and auto click the button.

2. The button is not on the page from the start and only pops up for a few seconds.

3. The page is constantly refreshing but I wanted to make sure the script will keep trying to click the button till it pops up and then stop once clicked.

My attempts so far:

Ive tried multiple site, extensions and questions here on stack:

  1. How to auto click an input button

  2. Automatic click on a pop up button

  3. Extension Autoclicker (really good btw): https://chrome.google.com/webstore/detail/auto-clicker-autofill/iapifmceeokikomajpccajhjpacjmibe?hl=en

  4. Extension imacros (also decent) : https://chrome.google.com/webstore/detail/imacros-for-chrome/cplklnmnlbnpmjogncfgfijoopmnlemp?hl=en#:~:text=You%20can%20combine%20iMacros%20with,iMacros%20for%20Firefox%20without%20changes!

MY ATTEMPT TO MAKE THE PROGRAM WITH TAMPERMONKEY AND jQUERY: I use a Tampermonkey extension that will allow jQuery on all pages just FYI

    // ==UserScript==
// @name         Autoclicker
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        Name
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const clickButton = () => {
        const button =
              document.querySelector("#queue-0-1394263709")
        if (button) button.click()
        else setTimeout(clickButton, 500)
    }
    window.addEventListener("DOMcontentloaded", clickButton)
})();

*This was just me trying something with the limited jQuery I know.

The Button details: Xpath of button: //*[@id="queue-0-1319022196"]

Selector of button: #queue-0-1394263709

Button: <a href="#" data="[object Object]" id="queue-0-1394263709" name="queue_item">Start</a>

PS: The button does not have a class to the best of my knowledge. I would really appreciate help from you guys thanks.

TL;DR i want to autoclick a button as soon as it pops up with a script

shemjay
  • 104
  • 1
  • 9

1 Answers1

-1

I ran your code through jsfiddle and noticed something. You had window.addEventListener("DOMcontentloaded", clickButton) instead of window.addEventListener("DOMcontentloaded", clickButton()). see the difference?

const clickButton = () => {
        const button =
              document.querySelector("#queue-0-1394263709")
        if (button) button.click()
        else setTimeout(clickButton, 500)
    }
    window.addEventListener("DOMcontentloaded", clickButton())
L8R
  • 401
  • 5
  • 21
  • Hey bloke thanks for helping me out. I tried the solution you gave and it said undefined when i tried running it on chrome dev tools. When i turned it into a script on Tampermonkey it didnt run either. – shemjay Nov 10 '20 at 15:54
  • My fault, I didn't realize there was no class. Put `#queue-0-1394263709` in place of `.some class`. – L8R Nov 10 '20 at 16:33
  • oh i did do that part and there was a small typo when you wrote document so i wrote it properly also but sadly still no dice. – shemjay Nov 11 '20 at 20:07
  • Are there any other elements with the same Id? – L8R Nov 12 '20 at 14:56
  • No I dont think there are maybe i can use something else apart from id? – shemjay Nov 13 '20 at 11:50
  • You could use `document.getElementsByTagName('a')`. – L8R Nov 13 '20 at 14:44
  • how do i get an elements tag name btw – shemjay Nov 15 '20 at 21:55
  • You could use `Elem.tagName`. [This doc](https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName) explains more – L8R Nov 16 '20 at 15:50