Last active
April 2, 2024 20:27
-
-
Save chris-castillo-dev/c5b2024370d268c4662c8c176205f1c1 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
const isElementOrParentMatched = (element, selectors) => { | |
let currentElement = element; | |
while(currentElement) { | |
for (let selector of selectors) { | |
if ($(currentElement).is(selector)) { | |
return true; | |
} | |
} | |
currentElement = currentElement.parentElement; | |
} | |
return false; | |
}; | |
// Function to check if a link is internal | |
const isInternalLink = (url) => { | |
const domain = window.location.hostname; | |
return url.startsWith('/') || url.startsWith('./') || url.startsWith('../') || url.includes(domain) || url.startsWith('#'); | |
} | |
// Get all anchor elements in the page | |
const anchorElements = document.querySelectorAll('a'); | |
// Array for user-defined jQuery selectors for exclusions | |
const exclusionSelectors = ['head', 'header', 'footer', '.skip-link', '.team-member-card', 'div[data-id="8294"]']; | |
// Filter out anchor elements match or are nested inside excluded selectors and which are not internal links | |
const filteredAnchorElements = Array.from(anchorElements).filter((el) => !isElementOrParentMatched(el, exclusionSelectors) && isInternalLink(el.href)); | |
// Extract the href of each anchor element instead of innerText | |
const anchorHrefs = Array.from(filteredAnchorElements).map((el) => el.href); | |
return JSON.stringify(anchorHrefs); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment