I want to perform a mouse click 5 seconds after a particular web page is in the foreground tab of the Chrome browser. Code for the mouse click is document.querySelector('[value="submit"]').click() and Web link is www.website.com
Please guide me if this arrangement will be possible. Regards, Vicky.
@Maik Lowrey, The page is already opened in the chrome browser, say in the second tab. Now when I move to that second tab (bring it to the foreground) , I want to wait for 5 seconds and emulate a mouse click on the search button there. This is what I want to achieve. (I can't reply to your comment, so editing here)
Finally, I got this correct with this code:
if(document.referrer=="w-w-w.website.url") {
function fireTheSubmitEvent() {
setTimeout(fireTheSubmitEvent, 2000)
}
document.addEventListener('visibilitychange', function() {
if (document.visibilityState === 'visible') {
document.querySelector('[value="Search"]').click()
}
})
}
Thank you all.
To detect either the tab have become visible or hidden, you can use the visibilitychange event and use the visibilityState to access the state itself. Then you can use the setTimeout function to run the submit event and set the time out to 5000 miliseconds.
function fireTheSubmitEvent() {
document.querySelector('[value="submit"]').click()
}
document.addEventListener('visibilitychange', function() {
if (document.visibilityState === 'hidden') {
setTimeout(fireTheSubmitEvent, 5000)
}
})