Created
February 16, 2017 04:01
-
-
Save ryanve/2150bcfe285ac9784d79fed9e8714d9c to your computer and use it in GitHub Desktop.
Mutation observer example listening to any attribute changes
This file contains 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