Home

Show Hidden Image Links

Source code for bookmarklet Show Hidden Image Links.



javascript:
(function(){
    
    function cE(a)
    {
        var a2 = [];
        for (var k = 0; k < a.length; ++k) {
            a2.push(a[k]);
            }
        return a2;
    }
    function linktext(node, ic)
    {
        function ns(nxi)
        {
            var str = "";

            var nx = nxi;
            while (nx != null)
            {
                if (nx.tagName == "IMG") {
                    if ( ic != null ) {++ic.count;}
                    str += (nx.hasAttribute("ALT") ? nx.alt : nx.src);
                }
                else if (nx.data != null
                    && nx.nodeType != 8 // COMMENT_NODE
                    && nx.parentNode.tagName != "SCRIPT") {
                    str += nx.data;
                }
                str += ns(nx.firstChild);
                nx = nx.nextSibling;
            }
            return str;
        }

        if (node.href == null || node.href.length == 0) {return null;}
        var nx = ns(node.firstChild);
        return nx.replace( /\s+/mg, " ").replace( /^\s/, "" ).replace( /\s+$/, "" );
    }
    var rp = 0;

    var tls = [];
    var hls = [];

    function eD(dt) {
        var c = cE( dt.getElementsByTagName( "A" ) );
        for (var k = 0; k < c.length; ++k) {
            var l = c[k];

            var ic = {count: 0};
            var lt = linktext(l, ic);
            if (lt != null)
            {
                if ( ic.count > 0 && lt.length == 0 ) {
                    hls.push( [l, dt] );
                }
                else
                {
                    if (lt.length > 0 && ! /#$/i.test(l.href))
                    {
                        tls[l.href] = l.href;
                    }
                }
            }
        }
    }


    // "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 redundant = 0;
    var hid_c = 0;

    for (var i = 0; i < hls.length; ++i )
    {
        var l = hls[i][0];

        var pf = "";
        var count;
        ++rp;
        if ( tls[l.href] && tls[l.href] == l.href )
        {
            ++redundant;
            pf = "R";
            count = redundant;
        }
        else
        {
            ++hid_c;
            count = hid_c;
        }

        var n = l.cloneNode( false );
        n.appendChild(
            hls[i][1].createTextNode(pf + count + ": [" + l.href + "]")
            );
        l.parentNode.replaceChild( n, l );
    }

    var st = "Hidden links: " + rp;
    if ( rp > 0 )
    {
        st += ". (Redundant: " + redundant
            + ")";

    }

    alert(st + ".");
}());