Skip to content

Instantly share code, notes, and snippets.

@smhigley
Last active December 5, 2020 01:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smhigley/75fa44df8218d62714ea6df5bab353b5 to your computer and use it in GitHub Desktop.
Save smhigley/75fa44df8218d62714ea6df5bab353b5 to your computer and use it in GitHub Desktop.
MutationObserver quick test
function observeElement(element) {
const config = { attributes: true, childList: true, subtree: true };
const callback = function(mutationsList, observer) {
for(let mutation of mutationsList) {
if (mutation.type === 'childList') {
const additions = mutation.addedNodes;
const removals = mutation.removedNodes;
if (removals.length) {
console.log('Child nodes were removed:', ...removals);
}
if (additions.length) {
console.log('Child nodes were added:', ...additions);
}
}
else if (mutation.type === 'attributes') {
console.log('The ' + mutation.attributeName + ' attribute was modified.');
}
}
};
const observer = new MutationObserver(callback);
observer.observe(element, config);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment