-1

I am working on a User script but it seems to work just for the first comparison:

(function() {
'use strict';

var noError = document.getElementsByClassName("noMistakeButton");
var next = document.getElementsByClassName("nextButton");
var wait = document.getElementsByClassName("understoodButton");
var okko = document.getElementsByClassName("buttonKo");
var exitOkko = document.getElementsByClassName("exitButton");

while(1)
{
    if( noError !== null)
    {
        noError.click();
    }

    if( next !== null)
    {
        exit.click();
    }

    if( wait !== null)
    {
        wait.click();
    }
    sleep(2);
    if( okko !== null)
    {
        exit.click();
    }

    if( exitOkko !== null)
    {
        exitOkko.click();
    }

} })();

My goal is the run this script freely while AFK. There are, as you can see, many buttons on the web page and each button cant be :visible or :hidden. My goal is just to click on them.

Here is the target page (static URL).

Some buttons only have class and no ID. Others have them both.

The console reports:

ERROR: Execution of script 'AutoVoltair' failed!   noError.click is not a function

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Maximilien Nowak
  • 113
  • 1
  • 1
  • 8

1 Answers1

0

It is unclear what you hope to accomplish. If you are trying to step through a sequence of controls, use the approach illustrated in Choosing and activating the right controls on an AJAX-driven site.

The kind of code shown in the question would just play "Whac-A-Mole" with whatever button "popped up" next. (And only if the preceding buttons had been deleted.)

Anyway, to answer the question: "why this code is only doing the first if?".

It's because userscripts (and javascript) stop running at the first critical error (with a few exceptions). Additionally:

  • noError.click is not a function because noError is a collection of elements, not a button.
  • All the getElementsByClassName calls are only done once. If it's going to continually loop, you need to recheck inside the loop.
  • There is no such thing as sleep().
  • while (1) is a very bad idea and can freeze your browser, or even help crash your PC. Use setInterval for polling the page.

Here's the "Whac-A-Mole" code with those errors corrected:

setInterval ( () => {
        var noError     = document.getElementsByClassName ("noMistakeButton");
        var next        = document.getElementsByClassName ("nextButton");
        var wait        = document.getElementsByClassName ("understoodButton");
        var okko        = document.getElementsByClassName ("buttonKo");
        var exitOkko    = document.getElementsByClassName ("exitButton");

        if (noError.length) {
            noError[0].click ();
        }
        if (next.length) {
            next[0].click ();
        }
        if (wait.length) {
            wait[0].click ();
        }
        if (okko.length) {
            okko[0].click ();
        }
        if (exitOkko.length) {
            exitOkko[0].click ();
        }
    },
    222  //  Almost 5 times per second, plenty fast enough
);


If you want to sequence clicks, use chained waitForKeyElements() calls as shown in this answer.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295