User:Proteins/checkALTtext.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Proteins/checkALTtext. |
//<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>