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);
}
}
}