Skip to content

Instantly share code, notes, and snippets.

@maximtop
Last active March 18, 2022 06:50
Show Gist options
  • Save maximtop/e94426a613559399b115e097970151ac to your computer and use it in GitHub Desktop.
Save maximtop/e94426a613559399b115e097970151ac to your computer and use it in GitHub Desktop.
Example of script, which can be used to click on elements
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