Skip to content

Instantly share code, notes, and snippets.

@hylophile
Created January 22, 2023 21:55
Show Gist options
  • Select an option

  • Save hylophile/93337c910559a7a3162af7f51efd279f to your computer and use it in GitHub Desktop.

Select an option

Save hylophile/93337c910559a7a3162af7f51efd279f to your computer and use it in GitHub Desktop.
wait for an element to appear in the DOM (useful for SPA)
function waitForSelector(s:string) {
const d = Date.now();
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
clearInterval(interval);
reject(new Error(`never found {s}`));
}, 2000);
const interval = setInterval(() => {
const el = document.querySelector(s);
if (el) {
clearTimeout(timeout);
clearInterval(interval);
resolve(Date.now() - d);
}
}, 100);
});
}
$0.click();
waitForSelector("#selector on new page")
.then((v) => console.error(v))
.catch((e) => console.error(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment