I am attempting to iron out a bug in a script which automatically logs in to https://account.booking.com/sign-in
function clickButton (){
const msg = document.querySelector(".bui-form__error");
const button = document.querySelector(".bui-button");
if (msg && msg.id !== "loginname-error" ) {
console.log(msg.textContent);
} else if (button) {
button.click();
} else {
console.log("Not yet...");
return;
}
}
setInterval(clickButton, 200);
Most of the time, this works fine. Despite having the username field filled out automatically by Chrome, it will sometimes display the loginname-error, which we can ignore safely for our purposes--however, this sometimes causes the page to become unresponsive to the script for some reason. Almost anything done on the page by the user will cause the script to continue--pressing F12 to open Dom inspector, clicking literally anywhere on the page, even running a command on the console (as simple as 'a = document.querySelector...
') all somehow kick the script into running as it should.
I have attempted to follow previous answers, implemented triggerMouseEvent
and run mouseover, mousedown, mouseup, and click. This behaves no differently from simply using click()
. As clicking elsewhere on the page can fix it, I have attempted to use such click events on other elements before using them on the button again; this also behaves no differently from simply using click()
. The only results I've seen for Tampermonkey script 'works after...' are AJAX issues with page reloading, not user interaction with the window.
Putting console.log("click")
following the click event shows that it is reaching the click event just fine. Using triggerMouseEvent
, I've logged dispatchEvent
as follows:
function triggerMouseEvent (node, eventType) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent (eventType, true, true);
console.log(node.dispatchEvent (clickEvent));
}
Which returns 'true
' as expected.
I have attempted to send a keyboard event as below between each click:
function pressf12(){
var event = new KeyboardEvent("pressF12");
event.keyCode = 123;
event.which = 123;
event.altKey = false;
event.ctrlKey = false;
event.shiftKey= false;
event.target = window;
window.dispatchEvent(event);
}
This has also not worked. I'm quite out of ideas for what is causing the issue and what can be done to get around it.
This is what is shown on the console after loading the page, if it may be relevant:
[Report Only] Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src saa.booking.com *.bstatic.com bstatic.com google-analytics.com 'self' 'nonce-6B8EKlfK9vqB8Uy'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.
content_script @ VM1347:61 (anonymous) @ VM1347:71
and
sign-in?op_token=...:38 POST https://account.booking.com/navigation_times?sid=&pid=...&nts=…=3&lang=en-us&ref_action=Signin_Index&aid=...&stype=&route=&ua=&ch=<= net::ERR_BLOCKED_BY_CLIENT callback @ sign-in?op_token=...:38 wrapper @ error_catcher:756 setTimeout (async)
win.setTimeout @ error_catcher:666 callback @ sign-in?op_token=...:38 load (async)
nav_timing @ sign-in?op_token=...:38 (anonymous) @ sign-in?op_token=...:38
Here is a screenshot:
No further messages are shown after the user clicks on the page. Even if I increase the interval to 5000, well after the page has loaded and both of these messages have displayed on their own, the clicks from the script do nothing.
The thought occurred to me that this may be caused by Chrome settings, addons or the like, but it occurs both on multiple of my own computers, as well as those of other users. Even were that the case, some workaround would be needed.
It also occurred to me that it may be somehow related to TM's sandbox. Trying @grant none
, @grant unsafeWindow
, and inserting the script inline as per this answer all have the same problem behavior. Inserting the script inline does give more of the unsafe-inline error in the console.