Skip to content

Instantly share code, notes, and snippets.

@henno
Created May 24, 2024 11:24
Show Gist options
  • Save henno/acd04715191e68bcf0b7eaf9859f3376 to your computer and use it in GitHub Desktop.
Save henno/acd04715191e68bcf0b7eaf9859f3376 to your computer and use it in GitHub Desktop.
Observe mutations
new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length > 0) {
const addedNode = mutation.addedNodes[0] as HTMLElement;
if (addedNode.tagName) {
console.log(`${addedNode.tagName}, id='${addedNode.id}', class='${addedNode.className}', innerText='${addedNode.innerText}'`);
console.log()
// Console log node's attribute names and values
for (let i = 0; i < addedNode.attributes.length; i++) {
console.log(`${addedNode.attributes[i].name}: ${addedNode.attributes[i].value}`);
}
console.log('----');
}
}
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment