Last active
April 2, 2024 20:28
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 that match or are nested inside exclusion selectors and which are not internal | |
const filteredAnchorElements = Array.from(anchorElements).filter((el) => !isElementOrParentMatched(el, exclusionSelectors) && isInternalLink(el.href)); | |
// Extract the textContent of each anchor element instead of href | |
const anchorTexts = Array.from(filteredAnchorElements).map((el) => el.textContent); | |
return JSON.stringify(anchorTexts); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment