Created
September 30, 2019 16:36
-
-
Save scytalezero/b8d01189b177bc538db1495ebcf193ec to your computer and use it in GitHub Desktop.
waitFor MutationObserver function
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 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