Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find what code changed the DOM

Have you ever tried to track down which piece of javascript modified the DOM? Use a mutationObserver to monitor the DOM for changes. Then run console.trace() inside the callback. This will log a stack trace all the way back to the code that did the DOM modification. Basically copy and paste this code.

(function () {
var targetNode = document.querySelector('.my-selector'); // Set your selector for the parent container.
var config = { attributes: true, childList: true, subtree: true };
// Callback function to execute when mutations are observed
var callback = function(mutationsList, observer) {
console.log('mutationsList', mutationsList);
console.trace(); // This is the magic
};
var observer = new MutationObserver(callback);
observer.observe(targetNode, config);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment