Jump to content

User:Proteins/checkALTtext.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Proteins (talk | contribs) at 16:26, 5 December 2008 (clean up a few typos). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<pre>
// Check whether alt text is specified for all images on a page
//
// To use this script, add "importScript('User:Proteins/checkALTtext.js');" to your monobook.js subpage 
// under your user page, as you can see at User:Proteins/monobook.js

function checkAltText() { 
    var alt_text = "";
    var alert_string = "";
    var error_string = "";

    var temp_image;
    var parent_node;
    var grandparent_node;

    var num_pixels = 0;
    var image_index = 0;
    var num_raw_images = 0;

    var num_icon_images = 0;
    var num_icon_images_missing_alt_text = 0;
    var num_nonicon_images = 0;
    var num_nonicon_images_missing_alt_text = 0;

    var icon_percentage = 0.0;
    var nonicon_percentage = 0.0;


// Loop over the images
    num_icon_images = 0;
    num_icon_images_missing_alt_text = 0;
    num_nonicon_images = 0;
    num_nonicon_images_missing_alt_text = 0;

    num_raw_images = document.images.length;
    alert_string = "This document has " + num_raw_images + " images.\n";
    window.alert(alert_string);

    for (image_index=0; image_index<num_raw_images; image_index++) {
        alt_text = "";
        temp_image = document.images[image_index];
        if (!temp_image) { continue; }
        alt_text = temp_image.alt;
 
        num_pixels = temp_image.width * temp_image.height;
        if (temp_image.src.match(/Replace_this_image_male\.svg/)) { continue; }
        if (temp_image.src.match(/Replace_this_image_female\.svg/)) { continue; }
        if (num_pixels > 5000) { 
                num_nonicon_images++; 
                if (!alt_text) { num_nonicon_images_missing_alt_text++; }
                continue;
        }
        num_icon_images++; 
        if (!alt_text) { num_icon_images_missing_alt_text++; }
    } // closes loop over the images
        icon_percentage = 0.0;
        nonicon_percentage = 0.0;

        if (num_icon_images > 0) { 
               icon_percentage = (100.0 * num_icon_images_missing_alt_text) / num_icon_images;
                alert_string += icon_percentage + "% of " + num_icon_images + " small images (<=5000 pixels) have no alt text.";
        }
      if (num_nonicon_images > 0) { 
              nonicon_percentage = (100.0 * num_nonicon_images_missing_alt_text) / num_nonicon_images;
               alert_string += nonicon_percentage + "% of " + num_nonicon_images + " normal images (>5000 pixels) have no alt text.";
      }
      window.alert(alert_string);

} // closes function checkAltText() 

addOnloadHook(function () {
            addPortletLink('p-cactions', 'javascript:checkAltText()', 'alt', 'ca-alttext', 'Check images for alt text', '', '');
});

//</pre>