Last active
May 27, 2017 07:42
-
-
Save culttm/ec7a384e5661f3fcf7e29cd98c5010d3 to your computer and use it in GitHub Desktop.
observeDOM.js
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
| observeDOM(document.getElementsByTagName('head')[0], () => { | |
| console.log('head is changed'); | |
| }); |
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
| 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