Skip to content

Instantly share code, notes, and snippets.

@tomhodgins
Created January 24, 2020 21:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tomhodgins/002348e81e2ee9b769ae9a8bd4414e7e to your computer and use it in GitHub Desktop.
const MO = new MutationObserver(processNodes)
function processNodes(entries) {
entries.forEach(({addedNodes}) =>
addedNodes.forEach(node => {
if (node.matches('a[href^="https://catch.me"]')) {
console.log('caught one!', node)
}
})
)
}
// Process all nodes in document right away
processNodes([{addedNodes: document.querySelectorAll('*')}])
// And being watching <html> for any future nodes added
MO.observe(document.documentElement, {childList: true, subtree: true})
// Add demo links
setTimeout(
() => {
const tag = document.createElement('a')
tag.href = 'https://skip.me'
document.body.append(tag)
},
500
)
setTimeout(
() => {
const tag = document.createElement('a')
tag.href = 'https://catch.me'
document.body.append(tag)
},
1000
)
setTimeout(
() => {
const tag = document.createElement('a')
tag.href = 'https://skip.me'
document.body.append(tag)
},
1500
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment