Search code examples
htmlcssfont-facewebfonts

How to get the list of all font-families used on the site?


My main goal is to remove unused font-faces (links to fonts). I must know all font-families that are actually used on the website and then I will remove all unused. Bingo ;)


Solution

  • Simply paste that in console and press enter , you can get a alert popup with list of fonts ..

    function styleInPage(css, verbose){
        if(typeof getComputedStyle== "undefined")
        getComputedStyle= function(elem){
            return elem.currentStyle;
        }
        var who, hoo, values= [], val,
        nodes= document.body.getElementsByTagName('*'),
        L= nodes.length;
        for(var i= 0; i<L; i++){
            who= nodes[i];
            if(who.style){
                hoo= '#'+(who.id || who.nodeName+'('+i+')');
                val= who.style.fontFamily || getComputedStyle(who, '')[css];
                if(val){
                    if(verbose) values.push([hoo, val]);
                    else if(values.indexOf(val)== -1) values.push(val);
                }
                val_before = getComputedStyle(who, ':before')[css];
                if(val_before){
                    if(verbose) values.push([hoo, val_before]);
                    else if(values.indexOf(val_before)== -1) values.push(val_before);
                }
                val_after= getComputedStyle(who, ':after')[css];
                if(val_after){
                    if(verbose) values.push([hoo, val_after]);
                    else if(values.indexOf(val_after)== -1) values.push(val_after);
                }
            }
        }
        return values;
    }
    
    alert(styleInPage('fontFamily'));// returns array:
    

    Screenshot