-
-
Save drejohnson/1b5251adec8cc2e70b2499522f25410c to your computer and use it in GitHub Desktop.
Mutation observer example listening to any attribute changes
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
!function() { | |
var emitter = { | |
emit: console.dir.bind(console) | |
} | |
function emit(mutation) { | |
var target = mutation.target | |
var name = mutation.attributeName | |
var value = target.getAttribute(name) | |
emitter.emit({ | |
mutation: mutation, | |
target: target, | |
name: name, | |
value: value, | |
state: value != null | |
}) | |
} | |
var observer = new MutationObserver(function(mutations) { | |
mutations.forEach(emit) | |
}); | |
observer.observe(document.body, { | |
subtree: true, | |
attributes: true | |
}); | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment