Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to find all fonts used on a page

Is there easy way to parse a HTML page to find all the fonts used (or all the font stacks used)?

Or similarly, is there a script that parses a page and returns which CSS rules are included and used or included and are not used?

Examples:

If I parse index.html, I want to know that 2 font stacks are used: font-family: Georgia, serif and font-family: Arial, sans-serif.

Or, if I parse index.html, I want to know that lines 10, 12, and 15 of style.css are used.

I imagine somewhere someone has created an app for this? Anyone know of anything?

like image 600
christina Avatar asked Jan 09 '12 16:01

christina


People also ask

How do you tell what fonts are used on a website?

Add (download) Google chrome extension or Safari extension on your choice of web browser. Go to webpage where you want to find out the font and click on WhatFont extension. Hover over the webpage. You will find a floating box containing font ,you want to find out.

How do I copy a font family from a website?

Go to the Web page with the font you like and highlight the first line of text on the page styled with that font. Press "Ctrl-C" to copy the line of text.


1 Answers

ES2015 way of doing it, also supporting ::before and ::after pseudo-selectors.

function listFonts () {

  let fonts = []

  for (let node of document.querySelectorAll('*')) {

    if (!node.style) continue

    for (let pseudo of ['', ':before', ':after']) {

      let fontFamily = getComputedStyle(node, pseudo).fontFamily

      fonts = fonts.concat(fontFamily.split(/\n*,\n*/g))

    }

  }

  // Remove duplicate elements from fonts array
  // and remove the surrounding quotes around elements
  return [...new Set(fonts)]
    .map(font => font.replace(/^\s*['"]([^'"]*)['"]\s*$/, '$1').trim())

}

When running this on StackOverflow, will return ["Times", "Arial", "Helvetica Neue", "Helvetica", "sans-serif", "BlinkMacSystemFont", "Consolas", "Menlo", "Monaco", "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", "monospace"]

like image 175
Christophe Marois Avatar answered Sep 28 '22 16:09

Christophe Marois