Created
February 5, 2014 17:02
-
-
Save botimer/8828339 to your computer and use it in GitHub Desktop.
Index and count all font variations on an HTML page
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 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