Find all anchor URLs (including in web components' shadow root nodes)
This file contains 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
function recursiveFindAnchors(node) { | |
if (!node) return [] | |
// Find anchors URLs | |
const bareAnchorURLs = [...node.querySelectorAll('a')].map(anchor => formatHref(anchor.href)) | |
// Find all shadow roots | |
const allShadowRoots = [...node.querySelectorAll('*')].filter(node => node.shadowRoot).map(node => node.shadowRoot) | |
// No web components in here | |
if (allShadowRoots.length === 0) return bareAnchorURLs | |
// Accumulate and go a deeper level in each, you can use flatMap() if supported instead | |
return bareAnchorURLs.concat(allShadowRoots.map(recursiveFindAnchors).flat()) | |
} | |
function formatHref (href) { | |
if (!href) return '' | |
const {origin, pathname} = new URL(href) | |
return origin + pathname | |
} | |
console.log([...new Set(recursiveFindAnchors(document.body))]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment