Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda