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

183
Visualizações
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
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