Home

Image Stats

Source code for bookmarklet Image Stats.



javascript:
(function(){
        function travTree(node, ifun)
    {
        var counter = 0;
        function ns(nxi, level)
        {
            var nx = nxi;
            while (nx)
            {
                ++counter;
                ifun(nx);
                ns(nx.firstChild, level + 1);
                nx = level > 0 ? nx.nextSibling : null;
            }
        }

        ns( node, 0 );
        return counter;
    }

    function countItems(a)
    {
        return a.length;
    }

    function hasAttribute(e, a)
    {
        return e.hasAttribute(a);
    }
    var rp = 0;

    var hcRE = /\S+/;

    var text_count = 0;
    var all_img_count = 0;

    var imgs = [];
    var img_count = 0;
    var noalt_count = 0;
    var invisible_count = 0;
    var bkg_count = 0;

    var area_count = 0;
    var area_noalt_count = 0;
    var area_hidden_count = 0;

    var submit_count = 0;
    var submit_noalt_count = 0;
    var submit_hidden_count = 0;

    function eN(n){
        var tg = n.tagName;
        switch (tg) {
            case "IMG":
            {
                imgs[n.src] = n.src;
                ++img_count;
                ++all_img_count;
                if ( hasAttribute( n, "ALT" ) ) {
                    invisible_count += (hcRE.test( n.alt )) ? 0 : 1;
                }
                else {
                    ++noalt_count;
                }
            }
            break;

            case "AREA":
            {
                ++area_count;
                if ( hasAttribute( n, "ALT" ) ) {
                    area_hidden_count += (hcRE.test( n.alt )) ? 0 : 1;
                }
                else {
                    ++area_noalt_count;
                }
            }
            break;

            case "BODY":
            case "TR":
            case "TD":
            case "TABLE":
            {
                var bkg = n.getAttribute("BACKGROUND");
                if (bkg != null) {
                    imgs[bkg] = bkg;
                    ++bkg_count;
                    ++all_img_count;
                }
            }
            break;

            case "INPUT":
            {
                if ( n.type.toUpperCase() == "IMAGE" ) {
                    imgs[n.src] = n.src;
                    ++all_img_count;
                    ++submit_count;
                    if ( hasAttribute( n, "ALT" ) ) {
                        submit_hidden_count += (hcRE.test( n.alt )) ? 0 : 1;
                    }
                    else {
                        ++submit_noalt_count;
                    }
                }
            }
            break;

            }
        if (n.data != null
            && n.nodeType != 8 // COMMENT_NODE
            && n.parentNode.tagName != "STYLE"
            && n.parentNode.tagName != "SCRIPT" && hcRE.test(n.data) ) {
            ++text_count;
        }
    }

    function eD(dt) {
        var bod = dt.getElementsByTagName( "BODY" )[0];
        travTree(bod, eN);
    }


    // "dIr" == "documentIterator"
    function dIr(dt, f) {
        function ifsr( fs )
        {
            for (var i = 0; i < fs.length; ++i)
            {
                try {
                    dIr(fs[i].contentDocument, fs[i]); // recursive call
                }
                catch (e) {
                    alert(e || "Unspecified error");
                }
            }
        }

        if (dt == null) {return;}
        eD(dt, f);
        ifsr( dt.getElementsByTagName("FRAME") );
        ifsr( dt.getElementsByTagName("IFRAME") );
    }

    // iterate through all documents
    dIr(document, null);
    var st = "Image/text ratio: " + (all_img_count) + "/" + text_count
        + " (unique: " + countItems(imgs) + ")"
        + ". | IMG: " + img_count
        + " (ALT: missing: " + noalt_count
        + ", empty: " + invisible_count
        + ", text: " + (img_count - noalt_count - invisible_count)
        + "). | Background: " + bkg_count
        ;
    st += ". | Image button: " + submit_count;
    if ( submit_count ) {
        st += " (Hidden: " + submit_hidden_count
            + ", ALT missing: " + submit_noalt_count
            + ")"
            ;
    }
    st += ". | Area: " + area_count;
    if ( area_count ) {
        st += " (Hidden: " + area_hidden_count
            + ", ALT missing: " + area_noalt_count
            + ")"
            ;
    }
    alert(st + ".");
    // if  (all_img_count != (img_count + bkg_count +  submit_count) ) { alert( "fel" ); }
}());