Click DOM not working function

I’m doing userscript and the click function seems to be blocked, is there any way to automatically click on the element I want?

Please provide a bit more context for your question.


You can select the element and call .click() on it.

<button>Click</button>
const btn = document.querySelector("button");
btn.addEventListener("click", () => console.log("button was clicked"));
btn.click();

So I’m automating the equipping and unequipping of a vest without me needing to go to the vest and keep clicking, you know?
But in this case, any click event I place only works if I have the mouse over the element, I wanted everything to be automatic, you know?

1 Like
window.addEventListener("load", function() {
    // Seu código aqui
    (function() {
        'use strict';

        // Adiciona o ícone do status do colete
        var thfofoColete = document.createElement("div");
        thfofoColete.id = "status-colete-thfofo";
        thfofoColete.style.content = "url(/static/img/item/colete.png)";
        thfofoColete.style.filter = "drop-shadow(1px 1px 0 white) drop-shadow(-1px 1px 0 white) drop-shadow(1px -1px 0 white) drop-shadow(-1px -1px 0 white)";
        thfofoColete.style.position = "absolute";
        thfofoColete.style.bottom = "85px";
        thfofoColete.style.right = "7px";
        thfofoColete.style.cursor = "pointer";
        document.body.appendChild(thfofoColete);

        // Variável para controlar se a verificação está ativada
        var thfofoReparar = false;

        // Função para alternar a verificação quando o ícone do colete é clicado
        thfofoColete.addEventListener("click", function() {
            thfofoReparar = !thfofoReparar;
            if (thfofoReparar) {
                alert("ativado");
                thfofoColete.style.filter = "drop-shadow(1px 1px 0 green) drop-shadow(-1px 1px 0 green) drop-shadow(1px -1px 0 green) drop-shadow(-1px -1px 0 green)";
            } else {
                alert("desativado");
                thfofoColete.style.filter = "drop-shadow(1px 1px 0 white) drop-shadow(-1px 1px 0 white) drop-shadow(1px -1px 0 white) drop-shadow(-1px -1px 0 white)";
            }
        });

        // Função para verificar periodicamente se o colete precisa ser reparado
        var thfofoIntervalo = setInterval(function() {
            if (thfofoReparar) {
                var thfofoUsandoAgora = window.getComputedStyle(document.getElementById("eslot2")).content;
                if (thfofoUsandoAgora.includes("colete")) {
                    var thfofoAlturaColete = parseInt(document.getElementById("eh2").style.height);
                    if (!isNaN(thfofoAlturaColete) && thfofoAlturaColete > 60) {
                        var clicarEquipad = document.getElementById("eslot2");
                        clicarEquipad.addEventListener("click", () => alert('cliquei no colete equipado'));
                        clicarEquipad.click();
                    }
                    for (var id = 1; id < 13; id++) {
                        var thfofoTemItem = document.getElementById("whp" + id).style.display;
                        if (thfofoTemItem === "block") {
                            var thfofoEquipamento = window.getComputedStyle(document.getElementById("slot" + id)).content;
                            if (thfofoEquipamento.includes("colete")) {
                                var thfofoAlturaItem = parseInt(document.getElementById("wh" + id).style.height);
                                if (!isNaN(thfofoAlturaItem) && thfofoAlturaItem >= 95) {
                                    // alert("Equipando colete [" + id + "]...");
                                    document.getElementById("slot" + id).click();
                                }
                            }
                        } else {
                            console.log("[" + id + "] não possui item");
                        }
                    }
                }
            }
        }, 10000);
    })();
});

Do you see any errors in the browser console related to your code?

I don’t know the page this is running on, so I can’t say much.

Right-click the element and use Inspect. In the “Event Listeners” tab make sure you are selecting the correct element in your code. It should have a click event attached to it. Remove your own event listeners you add in the script (or disable the script) first so you don’t see your own code.

You can also use getEventListeners in the console and pass it the element (docs).


Post the full UserScript and what you are using to run it (i.e. Tampermonkey).

I posted the entire code above, I use violentmonkey

I tried using this geteventlistener and it always returns an empty array in any element
“{}”

I meant including the metadata at the top of the script.

Also, do you see any errors in the browser console when the script runs?

Then the element you passed into getEventListeners does not have any event listeners on it. You can use $0 for the element as long as the node is selected inside Inspect

Example for the forum reply button

getEventListeners($0)
{keydown: Array(1), click: Array(1), mousedown: Array(1)}