Skip to content

Instantly share code, notes, and snippets.

@rdhyee
Created March 12, 2014 14:33
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 rdhyee/9508158 to your computer and use it in GitHub Desktop.
Save rdhyee/9508158 to your computer and use it in GitHub Desktop.
code bit to observe changes in DOM. Based on http://stackoverflow.com/a/18970264
var whatToObserve = {childList: true, attributes: true, subtree: true, attributeOldValue: true, attributeFilter: ['class', 'style']};
var mutationObserver = new MutationObserver(function(mutationRecords) {
$.each(mutationRecords, function(index, mutationRecord) {
if (mutationRecord.type === 'childList') {
if (mutationRecord.addedNodes.length > 0) {
//DOM node added, do something
console.log("DOM node added");
}
else if (mutationRecord.removedNodes.length > 0) {
//DOM node removed, do something
console.log("DOM node removed");
}
}
else if (mutationRecord.type === 'attributes') {
if (mutationRecord.attributeName === 'class') {
//class changed, do something
console.log("class changed");
}
}
});
});
mutationObserver.observe(document.body, whatToObserve);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment