Skip to content

Instantly share code, notes, and snippets.

@rogierslag
Last active January 14, 2020 10:43
Show Gist options
  • Save rogierslag/5cddfe07bf4aaea0e0b17758f1b7d7ba to your computer and use it in GitHub Desktop.
Save rogierslag/5cddfe07bf4aaea0e0b17758f1b7d7ba to your computer and use it in GitHub Desktop.
Get fonts from current web page
// Open the inspector of a page you want to check and run the following script.
const foundVariants = [];
function variation(family, weight, style) {
return {key : `${family}-${weight}-${style}`, family, style, weight};
}
// Loop over all selectors
document.querySelectorAll('*').forEach(e => {
// Get computed styles by the browser
const css = window.getComputedStyle(e);
// Get the font family
const families = (css.fontFamily || '').split(',')
.map(e => e.trim())
.map(e => e.replace(/"/g, ''))
// Filter these defaults out
.filter(e => e !== 'sans-serif' && e !== 'serif');
const style = css.fontStyle;
const weight = css.fontWeight;
const results = families.map(family => variation(family, weight, style));
// For each family, if it was unknown, add it to the list
results.forEach(result => {
if (!foundVariants.map(f => f.key).includes(result.key)) {
foundVariants.push(result);
}
});
});
console.log('Found the following variations of fonts', foundVariants.map(({family, weight, style}) => ({family, style, weight})));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment