Skip to content

Instantly share code, notes, and snippets.

@shahzaibalikhan
Created April 16, 2016 17:01
Show Gist options
  • Save shahzaibalikhan/76c181a62e504459308479144378c626 to your computer and use it in GitHub Desktop.
Save shahzaibalikhan/76c181a62e504459308479144378c626 to your computer and use it in GitHub Desktop.
HTML nodes mutation observer
var container = document.querySelector('body');
var observer = new MutationObserver(function(mutations){
// Do something here
mutations.forEach(function(mutation) {
console.log('mutation.type = ' + mutation.type);
for (var i = 0; i < mutation.addedNodes.length; i++) {
console.log(' "' + mutation.addedNodes[i].textContent + '" added');
}
for (var i = 0; i < mutation.removedNodes.length; i++) {
console.log(' "' + mutation.removedNodes[i].textContent + '" removed');
}
});
});
observer.observe(container, {
childList: true,
attributes: true,
characterData: true,
subtree: true,
attributeOldValue: true,
characterDataOldValue: true
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment