Skip to content

Instantly share code, notes, and snippets.

@blakek
Created October 18, 2023 18:32
Show Gist options
  • Save blakek/cf62508768fa6aa05ed38f742a00219c to your computer and use it in GitHub Desktop.
Save blakek/cf62508768fa6aa05ed38f742a00219c to your computer and use it in GitHub Desktop.
Find font usage on a page and what elements use it
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