Skip to content

Instantly share code, notes, and snippets.

@pgchamberlin
Created December 5, 2012 17:47
Show Gist options
  • Save pgchamberlin/4217842 to your computer and use it in GitHub Desktop.
Save pgchamberlin/4217842 to your computer and use it in GitHub Desktop.
Snippet that logs DOM mutations using the MutationObserver API
<script type="text/javascript">
// See MDN: https://developer.mozilla.org/en-US/docs/DOM/MutationObserver?redirectlocale=en-US&redirectslug=DOM%2FDOM_Mutation_Observers
(function(){
// select the target node
var target = document.querySelector('body');
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
var i=0;
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(++i + ': ' + mutation.type);
});
});
// configuration of the observer:
var config = {
childList: true,
attributes: true,
characterData: true,
subtree: true,
attributeOldValue: true,
characterDataOldValue: true,
attributeFilter: true
};
// pass in the target node, as well as the observer options
observer.observe(target, config);
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment