Merging getComputedStyle and evaluate in Greasemonkey
        Posted  
        
            by 
                Keheliya Gallaba
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Keheliya Gallaba
        
        
        
        Published on 2011-01-03T03:25:19Z
        Indexed on 
            2011/01/03
            5:53 UTC
        
        
        Read the original article
        Hit count: 245
        
I need to get all the text nodes with a certain font-face in a page to an array. I tried..
textnodes = document.evaluate("//* [@style='font-family: foo;']//text()["
            + "not(ancestor::script) and not(ancestor::style)]", document,
            null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
and
textnodes = document.evaluate("//* [@face='foo']//text()["
            + "not(ancestor::script) and not(ancestor::style)]", document,
            null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
But these does not work with pages that is styled by external CSS files. Seems getComputedStyle() is the way to go. I think what I need is something like..
var tags = document.getElementsByTagName('*');
for (var i in tags) {
    var style = getComputedStyle(tags[i], '');
    if (style.fontFamily.match(/foo/i)) {
        textnodes.push(tags[i]);
        }
    }
But text nodes were not returned in this method. Is there anyway I can use a hybrid of xpath evaluate() and getComputedStyle() or any other way to achieve this?
© Stack Overflow or respective owner