Created
February 15, 2024 13:29
-
-
Save n0sys/cd4d2516ca1716b7cf8fa9be243f84d9 to your computer and use it in GitHub Desktop.
This file contains 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
// wait for element to load | |
// This is NOT my code, source : https://stackoverflow.com/a/61511955 | |
function waitForElm(selector) { | |
return new Promise(resolve => { | |
if (document.querySelector(selector)) { | |
return resolve(document.querySelector(selector)); | |
} | |
const observer = new MutationObserver(mutations => { | |
if (document.querySelector(selector)) { | |
observer.disconnect(); | |
resolve(document.querySelector(selector)); | |
} | |
}); | |
// If you get "parameter 1 is not of type 'Node'" error, see https://stackoverflow.com/a/77855838/492336 | |
observer.observe(document.body, { | |
childList: true, | |
subtree: true | |
}); | |
}); | |
} | |
// Usage | |
waitForElm("body").then((element) => {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment