User:Proteins/hotsections.js

// // Inspired by HotCat, this script tries to make table editing easy. // After clicking once, it adds a "(+)" symbol before every paragraph and "(±)(-)" symbols to each existing image

// This script should have two states: add table symbols, or remove them. Clicking twice should not add the table symbols twice. // Global variables

var show_HotSections = false; var hot_section_string = "§";

function hotSectionPopup(arg_anchor_level, arg_section_heading) { var alert_string; var prompt_string;

//	alert_string = "Debugging string"; alert_string = "Once this function is written, you'll enter an H" + arg_anchor_level + " section heading \"" + arg_section_heading + "\" here\n"; window.alert(alert_string); } // closes hotSectionPopup

function hotSections { var alert_string = ""; var on_main_page = false; var using_Internet_Explorer = false;

var hot_node; var hot_link; var hot_text; var hot_typeface; var num_hot_links_added = 0;

var anchor_index = 0; var num_H2_anchors = 0;

var anchor_level = 0; var prev_anchor_level = 1; //begin at the H1 heading var read_entire_article = true; var anchors; var temp_anchor; var num_anchors = 0; var temp_anchor_name = ""; var section_name = "lead section";

var parent_node; var grandparent_node;

var sibling_node; var next_sibling_node;

var cutoff_anchor_index = 0; var cutoff_H2_anchor_index = 0;

var new_section_level = 0; var max_section_level = 0; var new_section_heading = ""; var section_level_string = "";

// Check whether we're on the Main Page on_main_page = false; if (document.getElementById("mp-topbanner")) { on_main_page = true; window.alert("Sorry, this script is intended for regular articles, not the Main Page."); return; } // closes check whether we're on the Main Page

// check for Internet Explorer browser using_Internet_Explorer = false; if (navigator.userAgent.indexOf("MSIE") > -1) { using_Internet_Explorer = true; }

// Insert hot-section tags up to the closing sections alert_string = ""; num_H2_anchors = 0; num_hot_links_added = 0; section_name = "lead section"; prev_anchor_level = 1; //begin at the H1 heading read_entire_article = true; anchors = document.anchors; num_anchors = anchors.length; for (anchor_index=1; anchor_index<num_anchors; anchor_index++) { temp_anchor = anchors[anchor_index]; parent_node = temp_anchor.parentNode; // should be a "P" node if (!parent_node) { continue; } sibling_node = parent_node.nextSibling; if (!sibling_node) { continue; } grandparent_node = parent_node.parentNode; if (!grandparent_node) { continue; }

// Check headings for jumps upwards in heading level anchor_level = 0; if (sibling_node.nodeName == "H1") { alert_string += " WARNING: Illegal H1 heading in this section\n"; } else if (sibling_node.nodeName == "H2") { anchor_level = 2; } else if (sibling_node.nodeName == "H3") { anchor_level = 3; } else if (sibling_node.nodeName == "H4") { anchor_level = 4; } else if (sibling_node.nodeName == "H5") { anchor_level = 5; } else { next_sibling_node = sibling_node.nextSibling; if (!next_sibling_node) { continue; } sibling_node = next_sibling_node;

// Check headings for jumps upwards in heading level if (next_sibling_node.nodeName == "H1") { alert_string += " WARNING: Illegal H1 heading in this section\n"; } else if (next_sibling_node.nodeName == "H2") { anchor_level = 2; } else if (next_sibling_node.nodeName == "H3") { anchor_level = 3; } else if (next_sibling_node.nodeName == "H4") { anchor_level = 4; } else if (next_sibling_node.nodeName == "H5") { anchor_level = 5; } else { continue; } } // closes assignment of the anchor level, if any

//Check major section headings for closing sections if (anchor_level == 2) { num_H2_anchors++; section_name = temp_anchor.name; temp_anchor_name = temp_anchor.name; alert_string += "Section " + num_H2_anchors + " : " + section_name.replace(/(_+)/ig, " ") + "\n"; temp_anchor_name = temp_anchor_name.replace(/:$/ig,""); // eliminate colons at end temp_anchor_name = temp_anchor_name.replace(/s$/ig,""); // eliminate plurals at end temp_anchor_name = temp_anchor_name.replace(/See_also/ig,""); temp_anchor_name = temp_anchor_name.replace(/Related_topic/ig,""); temp_anchor_name = temp_anchor_name.replace(/Related_article/ig,""); temp_anchor_name = temp_anchor_name.replace(/Further_reading/ig,""); temp_anchor_name = temp_anchor_name.replace(/External_link/ig,""); temp_anchor_name = temp_anchor_name.replace(/Footnote/ig,""); temp_anchor_name = temp_anchor_name.replace(/Note/ig,""); temp_anchor_name = temp_anchor_name.replace(/Reference/ig,""); temp_anchor_name = temp_anchor_name.replace(/Citation/ig,""); temp_anchor_name = temp_anchor_name.replace(/Source/ig,""); temp_anchor_name = temp_anchor_name.replace(/Link/ig,""); temp_anchor_name = temp_anchor_name.replace(/s([_\s]+)and([_\s]+)/ig,""); temp_anchor_name = temp_anchor_name.replace(/([_\s]+)and([_\s]+)/ig,""); temp_anchor_name = temp_anchor_name.replace(/([_\s]+)/ig,""); if (temp_anchor_name == "") { break; }

} // closes check for H2 anchor

if (anchor_level > 0) { hot_text = document.createTextNode("(+§)");

hot_typeface = document.createElement("B"); hot_typeface.appendChild(hot_text);

hot_link = document.createElement("A"); hot_link.className = "hotsection"; max_section_level = prev_anchor_level+1; section_level_string = "(2–" + max_section_level + ")"; hot_link.title = "Add a section here, level " + section_level_string; hot_link.appendChild(hot_typeface); hot_link.onclick = function { // I'd like to pass max_section_level to this function, but how? prompt_string = "Section level:"; new_section_level = window.prompt(prompt_string, ""); if (!new_section_level) { return; } while ((new_section_level < 2) || (new_section_level > 5)) { prompt_string = "Please limit your choice to the range from 2 to 5:"; new_section_level = window.prompt(prompt_string, ""); if (!new_section_level) { return; } }				new_section_heading = window.prompt("Section heading:",""); if (!new_section_heading) {return;} hotSectionPopup(new_section_level, new_section_heading); return; }			grandparent_node.insertBefore(hot_link, parent_node); num_hot_links_added++;

} // closes addition of the HotSection symbol

if (anchor_level > 0) { prev_anchor_level = anchor_level; } } // closes loop over the anchors

cutoff_anchor_index = anchor_index; cutoff_H2_anchor_index = num_H2_anchors; if (cutoff_anchor_index < num_anchors) { read_entire_article = false; alert_string += "\nHot-section insertion will stop before the \"" + temp_anchor.name.replace(/(_+)/ig, " ") + "\" section.\n"; } else { read_entire_article = true; alert_string += "\nHot-section insertion will cover the entire article.\n"; } //	window.alert(alert_string);

// Print combined alert string alert_string = "Added symbols for " + num_hot_links_added + " new sections.\n"; window.alert(alert_string); } // closes function hotSections $(function {            mw.util.addPortletLink('p-cactions', 'javascript:hotSections', hot_section_string, 'ca-hotsections', 'Symbols for adding sections', , ); }); //