In the end I used MutationObserver
:
// select the target node for mutation observation
var target = document.body;
// create an observer instance
var observer = new MutationObserver(function(mutations) { // mutations: an array of MutationRecord objects
mutations.forEach(function(mutation) {
var addedList = mutation.addedNodes;
for (var i = 0; i < addedList.length; ++i) {
// do something with addedList[i]
}
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true, subtree: true };
// pass in the target node, as well as the observer options
observer.observe(target, config);