Enumerate font urls using javascript / jquery

Is there a way to list all @ font-face URLs (web font URLs) of a given HTML page using JavaScript or JQuery?

+5
source share
3 answers

Yes, assuming you want to find all the @ font-faces specified in the stylesheet, unlike the ones actually used in the HTML document itself.

Given a reasonably standard stylesheet that looks like this:

@font-face {
    font-family: 'Lobster12Regular';
    src: url('fonts/lobster_1.2-webfont.eot');
    src: url('fonts/lobster_1.2-webfont.eot?#iefix') format('embedded-opentype'),
         url('fonts/lobster_1.2-webfont.woff') format('woff'),
         url('fonts/lobster_1.2-webfont.ttf') format('truetype'),
         url('fonts/lobster_1.2-webfont.svg#Lobster12Regular') format('svg');
    font-weight: normal;
    font-style: normal;

}

@font-face {
    font-family: 'AllerRegular';
    src: url('fonts/aller_std_rg-webfont.eot');
    src: url('fonts/aller_std_rg-webfont.eot?#iefix') format('embedded-opentype'),
         url('fonts/aller_std_rg-webfont.woff') format('woff'),
         url('fonts/aller_std_rg-webfont.ttf') format('truetype'),
         url('fonts/aller_std_rg-webfont.svg#AllerRegular') format('svg');
    font-weight: normal;
    font-style: normal;

}

@font-face {
    font-family: 'AllerBold';
    src: url('fonts/aller_std_bd-webfont.eot');
    src: url('fonts/aller_std_bd-webfont.eot?#iefix') format('embedded-opentype'),
         url('fonts/aller_std_bd-webfont.woff') format('woff'),
         url('fonts/aller_std_bd-webfont.ttf') format('truetype'),
         url('fonts/aller_std_bd-webfont.svg#AllerBold') format('svg');
    font-weight: normal;
    font-style: normal;

}

@font-face {
    font-family: 'AllerLight';
    src: url('fonts/aller_std_lt-webfont.eot');
    src: url('fonts/aller_std_lt-webfont.eot?#iefix') format('embedded-opentype'),
         url('fonts/aller_std_lt-webfont.woff') format('woff'),
         url('fonts/aller_std_lt-webfont.ttf') format('truetype'),
         url('fonts/aller_std_lt-webfont.svg#AllerLight') format('svg');
    font-weight: normal;
    font-style: normal;

}

h1 {
    font-family: 'Lobster12Regular';
}

h2 {
    font-family: 'AllerRegular';
}

Then the following HTML (with built-in Javascript) will more or less do the trick:

<html>

<head>

<link rel="stylesheet" href="test.css">

</head>

<body>

<h1>Hello</h1>
<h2>Hello</h2>

<script type="text/javascript">

var pattern=/url\(.*?\)/g;
for (var i=0;i<document.styleSheets[0].cssRules.length;i++)
{
    var urls=document.styleSheets[0].cssRules[i].cssText.match(pattern);
    if (urls)
    {
        for (var j=0;j<urls.length;j++)
        {
            alert(urls[j]);
        }
    }
}

</script>

</body>

</html>

With some reservations:

Firstly, one of the main ways to specify @ font-face relies on specifying src twice, this method will only raise the second src.

-, .

[0] ( ). , , , .

+3

, . , : http://paulirish.com/2009/font-face-feature-detection/, , @font-face, , . CSS- (, , ), , , JS jQuery. .

0

, , Chrome, , .

jquery ( webfonts).

, ( "" ), (: font-family: fonta, fontb, fontc).

jQuery.fn.elementText = function() {
    return $(this)  
            .clone()
            .children()
            .remove()
            .end()
            .text()
            .replace(/^\s\s*/, '').replace(/\s\s*$/, '')
            ;
};

Font = function(family, src) {
    // In order to properly categorise complex font setups, weight and style need to be 
    // considered as part of a unique font. (TODO)
    this.family = family;
    this.src = src;
    this.weight = null;
    this.style = null;
};

Font.prototype.toString = function() {
    return '[Font ' + this.family + ': ' + this.src + ']';
};


var fontNames = {};
var fontObjects = [];

$(':visible').each(function (k, v) {
    var $v = $(v);
    var font;
    if ($v.elementText().length) {
        font = $v.css('font-family');
        // TODO: seperate by comma, remove quotes
        fontNames[font] = font;
    }
});

for (var sheet=0; sheet < document.styleSheets.length; sheet++)
for (var i=0; i<document.styleSheets[sheet].cssRules.length; i++)
{
    var rule = document.styleSheets[sheet].cssRules[i];
    if (rule instanceof CSSFontFaceRule) {
        if (fontNames[rule.style.fontFamily])
            fontObjects.push(
                    new Font(rule.style.fontFamily, rule.style.src) );
    }
}

fontObjects.forEach( function(v) { console.log(v.toString()); });

Example output (here you can see an example of what happens when you specify different fonts 'bold', 'italic', etc.):

[Font 'ITC Legacy Sans Std': url(http://localhost/~admin/tmp/fonts/LegacySansStd-BookItalic.otf)]
[Font 'ITC Legacy Sans Std': url(http://localhost/~admin/tmp/fonts/LegacySansStd-Book.otf)]
[Font 'ITC Legacy Sans Std': url(http://localhost/~admin/tmp/fonts/LegacySansStd-MediumItalic.otf)]
[Font 'ITC Legacy Sans Std': url(http://localhost/~admin/tmp/fonts/LegacySansStd-Medium.otf)]
[Font 'ITC Legacy Sans Std': url(http://localhost/~admin/tmp/fonts/LegacySansStd-BoldItalic.otf)]
[Font 'ITC Legacy Sans Std': url(http://localhost/~admin/tmp/fonts/LegacySansStd-Bold.otf)]
[Font 'ITC Legacy Sans Std': url(http://localhost/~admin/tmp/fonts/LegacySansStd-Ultra.otf)]
[Font 'Ocean Sans Std': url(http://localhost/~admin/tmp/fonts/OceanSansStd-LightIta.otf)]
[Font 'Ocean Sans Std': url(http://localhost/~admin/tmp/fonts/OceanSansStd-Light.otf)]
[Font 'Ocean Sans Std': url(http://localhost/~admin/tmp/fonts/OceanSansStd-Book.otf)]
[Font 'Ocean Sans Std': url(http://localhost/~admin/tmp/fonts/OceanSansStd-SemiboldIta.otf)]
[Font 'Ocean Sans Std': url(http://localhost/~admin/tmp/fonts/OceanSansStd-Semibold.otf)]
[Font 'Ocean Sans Std': url(http://localhost/~admin/tmp/fonts/OceanSansStd-Bold.otf)]
0
source

All Articles