Created
October 21, 2021 23:18
-
-
Save olets/3d04ce5a481c98cba061b30dd5c01039 to your computer and use it in GitHub Desktop.
find on a page all links with a pathname you care about
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
const pathnamesOfInterest = ['of-interest', 'also-of-interest']; | |
/* | |
// if IE11 didn't need to be supported | |
elsOfInterest = [...document.querySelectorAll('a')]; | |
.filter(el => { | |
let pathname = ''; | |
if (el.href) { | |
const url = new URL(el.href); | |
pathname = url.pathname; | |
} | |
const ofInterest = pathnamesOfInterest.includes(pathname); | |
return ofInterest; | |
}); | |
*/ | |
elsOfInterest = [...document.querySelectorAll('a[href]')]; | |
.filter(el => { | |
const pathname = el.href.split('?')[0].replace(/\/$/, '').split('/').pop(); | |
const ofInterest = pathnamesOfInterest.includes(pathname); | |
return ofInterest; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment