Skip to content

Instantly share code, notes, and snippets.

@n0sys
Created February 15, 2024 13:29
Show Gist options
  • Save n0sys/cd4d2516ca1716b7cf8fa9be243f84d9 to your computer and use it in GitHub Desktop.
Save n0sys/cd4d2516ca1716b7cf8fa9be243f84d9 to your computer and use it in GitHub Desktop.
// 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