Skip to content

Instantly share code, notes, and snippets.

@kerminz
Created August 23, 2018 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerminz/d93a1de36586cc99ad22976f53059c20 to your computer and use it in GitHub Desktop.
Save kerminz/d93a1de36586cc99ad22976f53059c20 to your computer and use it in GitHub Desktop.
function waitForElement(selector, target, callback) {
// observer target node
var target = document.querySelector(target);
// create new observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var addedNodes = mutation.addedNodes;
for (var i=0; i < addedNodes.length; i++) {
var node = addedNodes[i];
if (node.matches && node.matches(selector)) {
callback();
observer.disconnect();
}
}
});
});
// observer config
var config = { childList: true, subtree: true };
// start with observervation
if (target) {
observer.observe(target, config);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment