Sometimes the mutation observer callback doesn't fire when I expect it to.
If I run this code in developer tools console:
// callback for mutations observer
callbackForAllChanges = function (mutationsList, observer) {
console.log("mutations: ", mutationsList);
};
// create mutation observer
allChanges = new MutationObserver(callbackForAllChanges);
// attach mutation observer to document
allChanges.observe(document, {
childList: true,
subtree:true
});
// create new child
document.body.appendChild(document.createElement("div"));
I would expect callback to fire when I create a new child. But sometimes it does and sometimes it doesn't.
When I run the code in the devtools console while on stackoverflow it works: I see mutations: [MutationRecord] logged to the console.
When I go on twitter and run the above code in the devtools console, it doesn't seem to work: mutations: [MutationRecord] is not logged to the console.
What could cause Mutation Observer to not work on twitter?
mutations: [MutationRecord] does not logconst newDiv = document.createElement('div')
newDiv.setAttribute('id','newDiv')
// callback for mutations observer
callbackForAllChanges = function (mutationsList, observer) {
console.log("mutations: ", mutationsList);
};
// create mutation observer
allChanges = new MutationObserver(callbackForAllChanges);
// attach mutation observer to document
allChanges.observe(newDiv, {
childList: true,
subtree:true
});
// create new child
newDiv.appendChild(document.createElement("div"));