User:Proteins/Vicime.js

// // Stop-gap substitute // // To use this script, add "importScript('User:Proteins/Vicime.js');" to your monobook.js subpage // under your user page, as you can see at User:Proteins/monobook.js

function highlight_references(search_node, parent_node) { // For testing purposes, begin by searching for instances of the username // Then graduate to indexOf(/]*>.*<\/ref>/)

var alert_string = ""; var num_instances = 0;

while (search_node != null) { if(search_node.nodeType==3) { //text node // Find position of first reference var ix=search_node.data.search(/]*>.*<\/ref>/);

// Added for debugging //       		var ix=search_node.data.toLowerCase.indexOf(wgUserName.toLowerCase); //			match_length = wgUserName.length;

if(ix!=-1) { num_instances++;

// Get the length of the match string var ref_matches = search_node.data.match(/]*>.*<\/ref>/); var match_string = ref_matches[0]; var match_length = match_string.length; //				var match_length = 10;

//Split the string into three substrings var t1=ix?document.createTextNode(search_node.data.substr(0,ix)):null; var t2=document.createTextNode(search_node.data.substr(ix,match_length)); var t3=ix+match_length==search_node.data.length?null:document.createTextNode(search_node.data.substr(ix+match_length));

// Create/add the SPAN elements and conjoin the substrings var s1=document.createElement("SPAN"); s1.style.backgroundColor="#FF0000"; s1.appendChild(t2); var s2=document.createElement("SPAN"); if(t1!=null) s2.appendChild(t1); s2.appendChild(s1); if(t3!=null) s2.appendChild(t3); parent_node.replaceChild(s2,search_node); if(t3!=null) highlight_references(t3,s2); //find remaining occurences in the new nodes search_node=s2.nextSibling; }     			else { // if there is no instance of a reference, go to next sibling node search_node=search_node.nextSibling; }		}   		else { // if the node is not a text node, search its children, then go on to siblings if(search_node.firstChild!=null) { num_instances += highlight_references(search_node.firstChild, search_node); }		     			search_node=search_node.nextSibling; }	} // closes check for null search_node

return num_instances;

} // closes function highlight_references

function Vicime { var alert_string = ""; var editbox_text = "";

var wpTextbox; var body_content;

var num_instances = 0;

// Get the bodyContent node body_content = document.getElementById('bodyContent'); if (!body_content) { alert_string = "ERROR: There is no bodyContent node in this article."; window.alert(alert_string); return; }

// For testing purposes only num_instances = highlight_references(body_content.firstChild, body_content); alert_string = "Colored " + num_instances + " instances of references."; window.alert(alert_string); return;

// Check that we're on an editing page if (!document.URL.match("\&action=edit")) { alert_string = "This function only works on editing pages. Sorry!\n"; window.alert(alert_string); return; }

// Get the textbox and its contents wpTextbox = document.getElementById("wpTextbox1"); if (!wpTextbox) { alert_string = "Error: Unable to identify editing box."; window.alert(alert_string); return; }	editbox_text = wpTextbox.value; alert_string = "The edit box begins with:\n\n\t" + editbox_text.substr(0, 20); window.alert(alert_string);

// Identify the references highlight_references(wpTextbox, wpTextbox.parentNode);

// Create placeholders for the references; this could be hard in a textarea element return; } // closes function Vicime

$(function {            mw.util.addPortletLink('p-cactions', 'javascript:Vicime', 'Vicime', 'ca-Vicime', 'Vicime formatting', 'v', ''); });