Created
October 18, 2023 18:32
-
-
Save blakek/cf62508768fa6aa05ed38f742a00219c to your computer and use it in GitHub Desktop.
Find font usage on a page and what elements use it
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 fontElementMap(selector = "*", onlyVisible = true) { | |
const allElements = document.querySelectorAll(selector); | |
const fontElements = {}; | |
for (element of allElements) { | |
const style = window.getComputedStyle(element); | |
const fontFamily = style.getPropertyValue("font-family"); | |
if (!fontFamily) { | |
continue; | |
} | |
if (onlyVisible && style.getPropertyValue("display") === "none") { | |
continue; | |
} | |
fontElements[fontFamily] = fontElements[fontFamily] || []; | |
fontElements[fontFamily].push(element); | |
} | |
return fontElements; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment