Skip to content

Instantly share code, notes, and snippets.

@telefonosuci
Last active September 8, 2023 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telefonosuci/76ad7fdda2b2c294cc31cf84ae524ea5 to your computer and use it in GitHub Desktop.
Save telefonosuci/76ad7fdda2b2c294cc31cf84ae524ea5 to your computer and use it in GitHub Desktop.
Print trace when a node of a specific type is appended to the dom
<script>
const checkNodeType = Node.COMMENT_NODE;
const observer = new MutationObserver((mutationsList, observer) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
for (const addedNode of mutation.addedNodes) {
if (addedNode.nodeType === checkNodeType) {
console.log('Comment added to DOM:', addedNode.textContent);
console.trace();
}
}
}
}
});
// Here we are watching the head of the dom... We can specific any other node to watch...
observer.observe(document.head, { childList: true, subtree: true });
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment