Skip to content

Instantly share code, notes, and snippets.

@scytalezero
Created September 30, 2019 16:36
Show Gist options
  • Save scytalezero/b8d01189b177bc538db1495ebcf193ec to your computer and use it in GitHub Desktop.
Save scytalezero/b8d01189b177bc538db1495ebcf193ec to your computer and use it in GitHub Desktop.
waitFor MutationObserver function
function waitFor(selector, cb) {
// set up the mutation observer
const observer = new MutationObserver(function (mutations, me) {
// `mutations` is an array of mutations that occurred
// `me` is the MutationObserver instance
var element = document.querySelector(selector)
if (element) {
cb(element)
me.disconnect() // stop observing
return
}
});
// start observing
observer.observe(document, {
childList: true,
subtree: true
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment