Last active
January 4, 2019 01:56
-
-
Save Yuyz0112/005e11735056f0bc992ce821e48647e1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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