Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!