Created
January 22, 2023 21:55
-
-
Save hylophile/93337c910559a7a3162af7f51efd279f to your computer and use it in GitHub Desktop.
wait for an element to appear in the DOM (useful for SPA)
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
| 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