通过javascript检测正在使用的@font-face

通过javascript检测正在使用的@font-face,javascript,jquery,css,Javascript,Jquery,Css,我需要嗅探的格式,以便将信息传递给并匹配格式 我可以使用jQuery获得字体 var font = $("p").css('font-family'); var fsiz = $("p").css('font-size'); 但是,如果字体系列是web字体-例如“Open Sans”,我还需要找到加载它的@font-face规则的src 我不知道怎么做。找到所有css定义的字体,演示(控制台) 函数getFonts(obj){ var o=obj |{}, sheet=document.sty

我需要嗅探
的格式,以便将信息传递给
并匹配格式

我可以使用jQuery获得字体

var font = $("p").css('font-family');
var fsiz = $("p").css('font-size');
但是,如果字体系列是web字体-例如“Open Sans”,我还需要找到加载它的
@font-face
规则的src


我不知道怎么做。

找到所有css定义的字体,演示(控制台)

函数getFonts(obj){ var o=obj |{}, sheet=document.styleSheets, 规则=null, i=板材长度,j;
虽然(0我调整了所选的解决方案,使其在IE和FF中也能工作,在iOs上也能工作。所选的解决方案只在Chrome中工作。(唉,我还不能为该解决方案添加注释,这就是为什么我将其放在一个单独的解决方案中。)

函数getFonts(obj){ var o=obj |{}, sheets=document.styleSheets, 规则=null, i=板材长度,j; 虽然(0是对的答案的一个小改进,但我已经包含了以字体大小和样式为键的嵌套对象,以检索字体的所有版本

function getFonts (obj) {
    var o = obj || {},
        sheet = document.styleSheets,
        rule = null,
        i = sheet.length, j;
    while( 0 <= --i ){
        rule = sheet[i].rules || sheet[i].cssRules || [];
        j = rule.length;
        while( 0 <= --j ){
            if( rule[j].constructor.name === 'CSSFontFaceRule' ){
                if (!(rule[j].style.fontFamily in o)) o[ rule[j].style.fontFamily ] = Object.create(null);
                o[ rule[j].style.fontFamily ][ rule[j].style.fontWeight + '-' + rule[j].style.fontStyle ] = rule[j].style.src;
            };
        }
    }
    return o;
}
函数getFonts(obj){ var o=obj |{}, sheet=document.styleSheets, 规则=null, i=板材长度,j;
while(0)您需要“查找src”做什么?我需要在iframe中重新创建@font-face以使其中的文本与父页面上的文本匹配。这在中不起作用FF@KyleMit,在FireFox中,您可以通过
rule[j]找到字体规则CSSFontFaceRule的instanceof CSSFontFaceRule
,但是从中提取实际数据看起来并不整洁,看起来您必须从规则文本中解析它。这个fiddle输出:{foo:“url(“bar.otf”)”}对我来说。你的代码中有一个错误。你正在使用未声明的变量“rule”。我修复了它,但Chrome最新的Chrome抛出了一个DOM异常。你似乎再也不能访问这些规则了。哦,好吧。谢谢,我修复了规则/规则错误。DOM异常超出了我的考虑范围,目前没有时间进行研究。Chrome根本不会让你这么做你再也无法访问这些属性了。对此你无能为力。如果有人找到解决方案/解决方法,我很乐意听到。
function getFonts (obj) {
    var o = obj || {},
        sheets = document.styleSheets,
        rules = null,
        i = sheets.length, j;
    while( 0 <= --i ){
        rules =  sheets[i].cssRules || sheets[i].rules || []; // I swapped those two, IE would go wrong
        j = rules.length;
        while( 0 <= --j ){
            if( rules[j].toString() == "[object CSSFontFaceRule]" ){  // This works in IE and FF too
                o[ rules[j].style.fontFamily ] = rules[j].style.src;
            };
        }
    }
    return o;
}
function getFonts (obj) {
    var o = obj || {},
        sheet = document.styleSheets,
        rule = null,
        i = sheet.length, j;
    while( 0 <= --i ){
        rule = sheet[i].rules || sheet[i].cssRules || [];
        j = rule.length;
        while( 0 <= --j ){
            if( rule[j].constructor.name === 'CSSFontFaceRule' ){
                if (!(rule[j].style.fontFamily in o)) o[ rule[j].style.fontFamily ] = Object.create(null);
                o[ rule[j].style.fontFamily ][ rule[j].style.fontWeight + '-' + rule[j].style.fontStyle ] = rule[j].style.src;
            };
        }
    }
    return o;
}