Jump to content

User:Proteins/checkALTtext.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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\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++; 
                     temp_image.src = "";
                }
                continue;
        }
        num_icon_images++; 
        if (!alt_text) { 
              num_icon_images_missing_alt_text++; 
              temp_image.src = "";
        }
    } // closes loop over the images
        icon_percentage = 0.0;
        nonicon_percentage = 0.0;

      if (num_nonicon_images > 0) { 
              nonicon_percentage = (100.0 * num_nonicon_images_missing_alt_text) / num_nonicon_images;
              nonicon_percentage = Math.round(nonicon_percentage);
               alert_string += num_nonicon_images_missing_alt_text + " of " + num_nonicon_images + " normal images ";
               if (num_nonicon_images_missing_alt_text == 1) { alert_string += "is"; } else { alert_string += "are";}
               alert_string += " missing alt text (" + nonicon_percentage + "%).\n\n";
      }
        if (num_icon_images > 0) { 
               icon_percentage = (100.0 * num_icon_images_missing_alt_text) / num_icon_images;
               icon_percentage = Math.round(icon_percentage);
                alert_string += num_icon_images_missing_alt_text + " of " + num_icon_images + " small images ";
               if (num_icon_images_missing_alt_text == 1) { alert_string += "is"; } else { alert_string += "are";}
                alert_string += " missing alt text (" + icon_percentage + "%).\n";
                alert_string += "Small images are defined as those having fewer than 5000 pixels.\n\n";
        }
      window.alert(alert_string);

} // closes function checkAltText() 

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

//</pre>