Skip to content

Instantly share code, notes, and snippets.

@culttm
Last active May 27, 2017 07:42
Show Gist options
  • Select an option

  • Save culttm/ec7a384e5661f3fcf7e29cd98c5010d3 to your computer and use it in GitHub Desktop.

Select an option

Save culttm/ec7a384e5661f3fcf7e29cd98c5010d3 to your computer and use it in GitHub Desktop.
observeDOM.js
observeDOM(document.getElementsByTagName('head')[0], () => {
console.log('head is changed');
});
const observeDOM = (function(){
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver,
eventListenerSupported = window.addEventListener;
return function(obj, callback){
if( MutationObserver ){
// define a new observer
let obs = new MutationObserver(function(mutations, observer){
if( mutations[0].addedNodes.length || mutations[0].removedNodes.length )
callback();
});
// have the observer observe foo for changes in children
obs.observe( obj, { childList:true, subtree:true });
}
else if( eventListenerSupported ){
obj.addEventListener('DOMNodeInserted', callback, false);
obj.addEventListener('DOMNodeRemoved', callback, false);
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment