Skip to content

Instantly share code, notes, and snippets.

@autotrof
Created July 22, 2023 09:55
Show Gist options
  • Save autotrof/b088ca0000a0b089f07926cdde8acd68 to your computer and use it in GitHub Desktop.
Save autotrof/b088ca0000a0b089f07926cdde8acd68 to your computer and use it in GitHub Desktop.
javascript wait for elm
// 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)) {
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment