Skip to content

Instantly share code, notes, and snippets.

@Yuyz0112
Last active January 4, 2019 01:56
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 Yuyz0112/005e11735056f0bc992ce821e48647e1 to your computer and use it in GitHub Desktop.
Save Yuyz0112/005e11735056f0bc992ce821e48647e1 to your computer and use it in GitHub Desktop.
>>> callback here
added element type node <body>​…​</body>​
[found with selector "div p"]
added element type node <div>​…​</div>​
[found with selector "div p"]
added element type node <p>​</p>​
added element type node <script>​</script>​
>>> callback here
added element type node <a>​</a>​
<!DOCTYPE html>
<head>
<script>
const callback = mttns => {
console.log('>>> callback here')
mttns.forEach(mttn => {
[...mttn.addedNodes].forEach(node => {
if (node.nodeType !== 1)
return;
console.log('added element type node', node)
if (node.querySelector('div p')) {
console.log('[found with selector "div p"]');
}
if (node.querySelector('div a')) {
console.log('[found with selector "div a"]');
}
});
});
};
const opts = { childList: true, subtree: true };
new MutationObserver(callback).observe(document, opts);
</script>
</head>
<body>
<div>
<p></p>
<script></script>
<a></a>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment