Note that mutation events are deprecated, in modern browsers you'd do this with mutation observers:
var onAppend = function(elem, f) {
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(m) {
if (m.addedNodes.length) {
f(m.addedNodes)
}
})
})
observer.observe(elem, {childList: true})
}
onAppend(document.body, function(added) {
console.log(added) // [p]
})
var p = document.createElement('p')
p.textContent = 'Hello World'
document.body.appendChild(p) // will trigger onAppend callback