Last active
March 18, 2022 06:50
-
-
Save maximtop/e94426a613559399b115e097970151ac to your computer and use it in GitHub Desktop.
Example of script, which can be used to click on elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AG_click = (selector, timeoutMs = 10000) => { | |
const callback = () => { | |
try { | |
const el = document.querySelector(selector); | |
if (el) { | |
el.click(); | |
return true; | |
} | |
} catch (e) { | |
// log error and stop execusion if selector is invalid | |
console.log(e.message); | |
return true; | |
} | |
return false; | |
}; | |
// do not append mutation observer if element is already on the page | |
if (callback()) { | |
return; | |
} | |
const observer = new MutationObserver(() => { | |
if (callback()) { | |
observer.disconnect(); | |
} | |
}); | |
observer.observe(document, { childList: true, subtree: true }); | |
// stop observing if timeout reached | |
setTimeout(() => { | |
observer.disconnect(); | |
}, timeoutMs); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment