Skip to content

Instantly share code, notes, and snippets.

@botimer
Created February 5, 2014 17:02
Show Gist options
  • Save botimer/8828339 to your computer and use it in GitHub Desktop.
Save botimer/8828339 to your computer and use it in GitHub Desktop.
Index and count all font variations on an HTML page
function indexFonts() {
var fonts = {};
var props = ['font-family', 'font-size', 'font-weight', 'font-style'];
var nodes = document.body.getElementsByTagName('*');
var L = nodes.length;
for (var i = 0; i < L; i++) {
var node = nodes[i];
if (node.style) {
var style = node.style;
var cs = getComputedStyle(node, '');
var font = {};
for (j in props) {
var prop = props[j];
var val = (style[prop] || cs[prop]);
font[prop] = val;
}
var key = JSON.stringify(font);
fonts[key] = (fonts[key] || 0) + 1;
}
}
return fonts;
}
console.log(indexFonts());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment