Home

Show ALT as TITLE

Source code for bookmarklet Show ALT as TITLE.



javascript:
(function(){
    
    function cE(a)
    {
        var a2 = [];
        for (var k = 0; k < a.length; ++k) {
            a2.push(a[k]);
            }
        return a2;
    }

    function hasAttribute(e, a)
    {
        return e.hasAttribute(a);
    }
    var rp = 0; // replacement count
    var ic = 0; // item count
    function eI(dt) {
        var c = cE( dt.images );
        for (var k = 0; k < c.length; ++k) {
            var img = c[k];
            var alt = img.alt;
            var noalt = !hasAttribute(img, "ALT");

            rp += (noalt ? 1 : 0);
            ++ic;
            img.title = "[" + (noalt ? "[" + img.src + "]" : alt) + "]";
            }
    }

    function eA(dt) {
        var c = cE( dt.getElementsByTagName("area") );
        for (var k = 0; k < c.length; ++k) {
            var img = c[k];
            var alt = img.alt;
            var noalt = !hasAttribute(img, "ALT");

            rp += (noalt ? 1 : 0);
            ++ic;
            img.title = "[" + (noalt ? "[" + img.href + "]" : alt) + "]";
            }
    }

    function eD(dt) {
        eI(dt);
        eA(dt);
        alert("ALT-less items: " + rp + "/" + ic);
    }


    // "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);
}());