Created
October 21, 2021 15:49
-
-
Save DevinClark/c4acfd90d2689041a21e8c9fba5413e6 to your computer and use it in GitHub Desktop.
Get unique links in viewport of a page
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
function isAnyPartOfElementInViewport(el) { | |
const rect = el.getBoundingClientRect(); | |
const windowHeight = (window.innerHeight || document.documentElement.clientHeight); | |
const windowWidth = (window.innerWidth || document.documentElement.clientWidth); | |
const vertInView = (rect.top <= windowHeight) && ((rect.top + rect.height) >= 0); | |
const horInView = (rect.left <= windowWidth) && ((rect.left + rect.width) >= 0); | |
return (vertInView && horInView); | |
} | |
function unique(propertyName) { | |
return ((e, i, array) => array.findIndex(a => a[propertyName] === e[propertyName]) === i); | |
} | |
console.log(Array.from(document.querySelectorAll('a[href]')).filter(isAnyPartOfElementInViewport).filter(unique('href')).length) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment