You can use Mutation Observers for this purpose - at least if you do not need to support IE/Opera.
Here's a short example (taken from html5rocks.com) on how they are used:
var insertedNodes = [];
var observer = new WebKitMutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
for(var i = 0; i < mutation.addedNodes.length; i++)
insertedNodes.push(mutation.addedNodes[i]);
})
});
observer.observe(document, {
childList: true
});
console.log(insertedNodes);
Note the Webkit
prefix. You need to use the browser-specific prefix. In Firefox it would be Moz
instead.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…