User:Proteins/hotcite.js

// // Inspired by HotCat, this script tries to make adding citations easy. // After clicking once, it adds a "(+)" symbol after every sentence // This script should have two states: add citation symbols, or remove them. Clicking twice should not add the citation symbols twice. // Global variables var show_HotCite = false; var hot_cite_string = "cite"; function hotCitePopup { window.alert("Once this function is written, you'll enter new citation here\n"); } // closes hotCitePopup function hotCite { var alert_string = ""; var on_main_page = false; var using_Internet_Explorer = false; var paragraphs; var temp_paragraph; var num_paragraphs = 0; var paragraph_index = 0;

var sentences; var temp_sentence; var num_sentences = 0; var sentence_index = 0; var child_nodes; var temp_child_node; var num_child_nodes = 0; var child_node_index = 0;

var temp_text; var sentences; var temp_sentence; var num_sentences = 0; var sentence_index = 0;

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

// 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; } // add the citation symbols num_hot_links_added = 0; paragraphs = document.getElementById('bodyContent').getElementsByTagName("P"); num_paragraphs = paragraphs.length; for (paragraph_index=0; paragraph_index<num_paragraphs; paragraph_index++) { temp_paragraph = paragraphs[paragraph_index]; temp_child_node = temp_paragraph.firstChild; if ((temp_child_node.nodeName == "A") && (temp_child_node.name)) { continue; } // skip anchors

child_nodes = temp_paragraph.childNodes; num_child_nodes = child_nodes.length; for (child_node_index=0; child_node_index<num_child_nodes; child_node_index++) { temp_child_node = temp_paragraph.childNodes[child_node_index];

if (temp_child_node.nodeType != 3) { continue; } // consider only text nodes

temp_text = temp_child_node.data;

sentences = temp_text.split(/\.\s+/); num_sentences = sentences.length; if (num_sentences < 2) { continue; } // skip text nodes that don't split num_sentences--; for (sentence_index=0; sentence_index 0) { temp_sentence = " " + temp_sentence; }

sub_text = document.createTextNode(temp_sentence); temp_paragraph.insertBefore(sub_text, temp_child_node);

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

hot_typeposition = document.createElement("SUP"); hot_typeposition.appendChild(hot_typeface); hot_link = document.createElement("A"); hot_link.className = "hotcite"; hot_link.title = "Click to add a citation here."; hot_link.appendChild(hot_typeposition); hot_link.onclick = function { hotCitePopup; return; }				temp_paragraph.insertBefore(hot_link, temp_child_node); num_hot_links_added++; } // closes loop over sentences temp_paragraph.removeChild(temp_child_node);

} // closes loop over the child nodes of this paragraph hot_text = document.createTextNode("(cite)"); hot_typeface = document.createElement("B"); hot_typeface.appendChild(hot_text);

hot_typeposition = document.createElement("SUP"); hot_typeposition.appendChild(hot_typeface); hot_link = document.createElement("A"); hot_link.className = "hotcite"; hot_link.title = "Click to add a citation here."; hot_link.appendChild(hot_typeposition); hot_link.onclick = function { hotCitePopup; return; }		temp_paragraph.appendChild(hot_link); num_hot_links_added++;

} // closes loop over paragraphs // Acknowledgment alert_string = "Added " + num_hot_links_added + " symbols for new citations."; window.alert(alert_string); } // closes function hotCite $(function {            mw.util.addPortletLink('p-cactions', 'javascript:hotCite', hot_cite_string, 'ca-hotcite', 'Symbols for adding inline citations', , ); }); //