User:Proteins/unindent.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/unindent. |
//<pre>
// Unindent discusions for accessibility
function unindent() {
var alert_string = "";
var diagnostic_string = "";
var indent_level = 0;
var DL_elements;
var num_DL_elements = 0;
var DT_elements;
var num_DT_elements = 0;
var DD_elements;
var temp_DD_element;
var num_DD_elements = 0;
var DD_element_index = 0;
var num_unindented_DD_elements = 0;
var top_node;
var parent_node;
var top_DL_node;
// Colors to help sighted people after the unindenting
var DD_background_colors = ["black", "red", "blue", "green", "magenta", "cyan", "orange", "purple", "darkgreen", "brown"];
top_node = document.getElementById('bodyContent');
DD_elements = top_node.getElementsByTagName("DD");
diagnostic_string = "";
num_DD_elements = DD_elements.length;
for (DD_element_index=0; DD_element_index<num_DD_elements; DD_element_index++) {
temp_DD_element = DD_elements[DD_element_index];
indent_level = 0;
// Find the topmost DL element for this DD node
top_DL_node = null;
parent_node = temp_DD_element.parentNode;
while ((parent_node) && (parent_node != top_node)) {
if (parent_node.nodeType != 1) {
parent_node = parent_node.parentNode;
continue;
} // examine only Element nodes
if (parent_node.nodeName == "DL") {
// Check whether the parent DL element has any DT elements
num_DT_elements = parent_node.getElementsByTagName("DT").length;
if (num_DT_elements > 0) { break; } // if so, stop unindenting...
// ...else make this the new indent level
indent_level++;
top_DL_node = parent_node;
} // closes check for a parental DL element
parent_node = parent_node.parentNode;
} // closes loop climbing up the document tree
if (indent_level > 0) {
num_unindented_DD_elements++;
temp_DD_element.style.cssText = "background-color:" + DD_background_colors[indent_level];
diagnostic_string += "DD element " + DD_element_index + " is indented to level " + indent_level + ".\n";
}
} // closes loop over the DD elements of the document
window.alert(diagnostic_string);
//Acknowledgment
alert_string = "Unindented " + num_unindented_DD_elements + " paragraphs of " + num_DD_elements + " possible.";
window.alert(alert_string);
} // closes unindent() function
addOnloadHook(function () {
addPortletLink('p-cactions', 'javascript:unindent()', 'unindent', 'ca-unindent', 'Unindent discussions', '', '');
});
//</pre>