The button won't be clicked after timeout with the click() method

A beginner’s question:

I avoid detailing why and where I run the following script from security consideration but I do wonder why it doesn’t work on the end webpage. Does something seem problematic to you?

setTimeout (function doPurity() {
let myButton = document.querySelector("[type=submit]");
window.onload = myButton.click();
}, 1000);

doPurity();

This code doesn’t work in my browser though — The button won’t be clicked after one second, the moment I land on the relevant webpage / the moment I load it.

  1. I run the script with Tampermonkey, in which other scripts work fine.
  2. The script is turned on, at the relevant webpage.

Any ideas why it won’t work?

A typo? i.e. "setTimout

Thank you. My bad not to go through the code again before posting. I fixed the typo (edited the question) and now I encounter the main problem I had before I added the setTimeout as well (and that I thought the setTimeout will fix):

It seems the button does indeed clicked (this time, after 1 second) but it seems the webpage loads itself time and again. Like it’s being loaded in a loop.

I think it’s some kind of “defense mechanism” that the webpage I try to click a button in, blocks me when it recognizes the click is being done with the click() method.

I don’t am not a security specialist and don’t the slightest clue to explain this behavior.

Original problem (reported in comment 3 solved).

The loop behavior caused due to a wrong match in the Tampermonkey script. Instead of a match to a specific webpage, the match was global (*).

It seemed natural to me that if there is a refresh loop, no other webpage is involved but I was wrong — Even though I stayed in the same webpage, the browser only tried to load others.

Now, after I changed the match from https://example.com to https://example.com/xyz the function worked properly.

Thus, refresh loop behavior is most likely due a global match in Greasemonkey/Tampermonkey.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.