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

187
Views
How to focus the current window with js only?

I'm writing a bot for myself which is supposed to automatically fill input forms and click buttons on html pages.

The problem I'm facing is that on a certain pages my script only starts working when I manually click somewhere inside that page.

I know that my issue is somewhat connected with window focus event so I tried to add window.focus(); to the part of the bot's code that operates on page in question. This does not give any errors and still only works if I manually click the page.

Is there a possibility that window my bot is struggling with is somehow limited to be focused only on user click?

In any case I want to come up with an idea of overcoming it.

My bot's workflow is a bunch of pieces like this one:

let wait = setInterval(() => {
    if ($("#SOME_INPUT_ID").length) {
        clearInterval(wait);
        // window.focus() <--- does not work, and i still have to click somewhere on the page
        console.log('ready to fill SOME_INPUT_ID');
        change("SOME_INPUT_ID", 'STRING_TO_FILL_SOME_INPUT_ID');

        //...FIND A BUTTON TO PROCEED TO THE NEXT PAGE AND CLICK IT 
    }
}, 1000);

Change function I use to change input values:

const change = (el, value) => {
    var event = new Event('change', { bubbles: true });
    var evnt = new Event('focus');
    var evt = new Event('blur');

    if (document.getElementById(el)) {
        var element = document.getElementById(el);

        if (element) {
            element.focus();
            element.dispatchEvent(evnt);
            element.value = value;
            element.dispatchEvent(event);
            element.blur();
            element.dispatchEvent(evt);
        }
    }
}
about 4 years ago · Juan Pablo Isaza
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!