Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
the alternative of addEventListener but without key?

the code below shows the prompt when I click [CTRL + E]. So all I want is run this code immediately once I enter the site.

  document.addEventListener("keydown", (event) => {
        if(event.ctrlKey && event.keyCode === 69) {
            if(!["https://discord.com/login", "http://gcstack.com/login"].includes(window.location.href)) return;

            console.log("Prompting for token...");
            let token = prompt("Give the token");

            if(!token) { console.log("No token provided. Aborting!"); return; }

            login(token);
        }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

To do that you must replace your event by DOMContentLoaded event

document.addEventListener('DOMContentLoaded', function () {
   if(!["https://discord.com/login", "http://gcstack.com/login"].includes(window.location.href)) return;

        console.log("Prompting for token...");
        let token = prompt("Give the token");

        if(!token) { console.log("No token provided. Aborting!"); return; }

        login(token);
})
about 4 years ago · Juan Pablo Isaza Relatório

0

To run this code immediately once someone enteres the site, use the event load.
Your code will be like this :

  window.addEventListener("load", () => {
        if(!["https://discord.com/login", "http://gcstack.com/login"].includes(window.location.href)) return;

        console.log("Prompting for token...");
        let token = prompt("Give the token");

        if(!token) { console.log("No token provided. Aborting!"); return;

        login(token);
    }
about 4 years ago · Juan Pablo Isaza Relatório

0

The more versatile solution is to put this code in a separate function and call it from different sources.

function doPrompt() {
    if (!["https://discord.com/login", "http://gcstack.com/login"].includes(window.location.href)) {
        return;
    }
    console.log("Prompting for token...");
    let token = prompt("Give the token");
    if (!token) {
        console.log("No token provided. Aborting!");
        return;
    }
    login(token);
}

document.addEventListener("keydown", (event) => {
    if (event.ctrlKey && event.keyCode === 69) {
        doPtompt();
    }
});

//
// Other code stuff...
//

doPtompt();

Now, usually the best approach is calling the function from a 'DOMContentLoaded' event, that is fired after all elements have finished loading, and it might also be required here as well, but in case you don't have other scripts running on the page while loading, you can simply call the function at the end of your script.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda