User:AFigureOfBlue/toolkit.js

//This is an underconstruction JavaScript library. It is not yet documented or ready for use. //

//Constructor function; not much in it right now, but more to come. function Floatbox(boxname, color){ this.boxname = boxname;

if(!color){ this.color = "LightBlue"; } else { this.color = color; }

importStylesheetURI('//en.wikipedia.org/w/index.php?title=User:Drilnoth/toolkit.css&action=raw&ctype=text/css'); var floatbox = add_child(id("content"), "div"); floatbox.id = "floatbox-main"; if(this.color){ floatbox.style.borderColor = this.color; } var topdiv = add_child(floatbox, "div"); topdiv.id = "floatbox-topdiv"; if(this.color){ topdiv.style.backgroundColor = this.color; } var closebutton = add_child(topdiv, "a"); closebutton.id = "floatbox-close"; closebutton.href = "javascript:id('content').removeChild(id('floatbox-main')); void 0;"; closebutton.style.cssFloat = "right"; closebutton.style.styleFloat = "right"; var closeimage = add_child(closebutton, "img"); closeimage.src = "//en.wikipedia.org/w/thumb.php?f=Dazzle!%20black%20x.svg&width=12px"; closeimage.style.border = "none"; topdiv.appendChild(document.createTextNode(this.boxname)); var statustext = add_child(topdiv, "span"); statustext.id = "floatbox-status"; var statusinner = add_child(statustext, "span"); statusinner.id = "floatbox-status-inner"; floatbox.appendChild(topdiv); if(skin == "vector"){ floatbox.style.fontSize = "small"; } this.closebutton = id("floatbox-close"); this.topdiv = id("floatbox-topdiv"); this.mainbox = id("floatbox-main"); } Floatbox.prototype.create_form = function{ var floatform = add_child(id("floatbox-main"), "form"); floatform.id = "floatbox-floatform"; floatform.name = "floatform"; return floatform; }; Floatbox.prototype.form_append = function(type, txt){ return add_child(id('floatbox-floatform'), type, txt); }; Floatbox.prototype.form_input = function(type, name){ return add_input(id('floatbox-floatform'), type, name); };

function set_status(status, statuscolor){ var statustext = id("floatbox-status"); var statusinner = id("floatbox-status-inner"); var newstatus = document.createElement("span"); var newstatustext = document.createTextNode(": " + status); newstatus.appendChild(newstatustext); if(statuscolor){ newstatus.style.color = statuscolor; } newstatus.id = "floatbox-status-inner"; statustext.replaceChild(newstatus, statusinner); }

//A set of "ease of use" functions function enc(i){ return encodeURIComponent(i); } function id(i){ return document.getElementById(i); }

//Adds an element. function add_child(target, type, txt){ if (type) { var child_element = document.createElement(type); target.appendChild(child_element); if(txt){ var txtnode = document.createTextNode(txt); child_element.appendChild(txtnode); } return child_element; } else { var text_node = document.createTextNode(txt); target.appendChild(text_node); return text_node; } } //Adds an element which is IE-compatible. function add_input(target, type, name){ var child_element = document.createElement("input"); child_element.type = type; child_element.name = name; target.appendChild(child_element); return child_element; }

//Adds an invisible "note" to the document, which can be found by using document.getElementById. function add_note(i, txt){ var note = add_child(document.body, "span"); note.style.display = "none"; note.id = i; if (txt) { add_child(note, null, txt); } }

//Gets the content of the specified note. function get_note(i){ return id(i).firstChild.nodeValue; }

//Returns a boolean indicating whether or not the specified note exists. function has_note(i){ if (id(i)) { return true; } else { return false; } }

function ucfirst(oldString){ return oldString.charAt(0).toUpperCase + oldString.slice(1); }

function wikilinkstoanchors(str){ str = str.replace(/\[\[([^\|\]]*)\]\]/gi, "$1"); str = str.replace(/\[\[([^\|\]]*)\|([^\]]*)\]\]/gi, "$2"); str = str.replace(/\[https?\:\/\/([^\]\s]*)\]/gi, "$1"); str = str.replace(/\[https?\:\/\/([^\]\s]*)\s([^\]]*)\]/gi, "$2"); return str; }

function cleanwikitags(str){ str = str.replace(/<\/? ?nowiki ?\/?\>?/gi, ""); str = str.replace(/<(\/? ?)ref/gi, "&lt;$1ref"); str = str.replace(/<(\/? ?)ref\>/gi, "&lt;$1ref&gt;"); str = str.replace(/<(\/? ?)references( ?\/?)\>/gi, "&lt;$1references$2&gt;"); return str; }

if (typeof(query_api_url) === 'undefined') { var query_api_url = "//en.wikipedia.org/w/api.php"; }

function query(params, onsuccess, onerror){ var req = sajax_init_object; req.open("POST", "//en.wikipedia.org/w/api.php", true); req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.setRequestHeader("Content-length", params.length); req.setRequestHeader("Connection", "close"); if( typeof onerror == "function" ) req.onerror = onerror; if( typeof onsuccess == "function" ) req.onreadystatechange = function{ if (this.readyState == 4) { if (this.status == 200) { onsuccess; } } } req.send(params); return req; }

function squery(params){ var req = sajax_init_object; req.open("POST", "//en.wikipedia.org/w/api.php", false); req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.setRequestHeader("Content-length", params.length); req.setRequestHeader("Connection", "close"); req.send(params); return req; }

XMLHttpRequest.prototype.oncomplete = function(func){ this.onreadystatechange = function{ if (this.readyState == 4) { if (this.status == 200) { func; } } }; };

XMLHttpRequest.prototype.results = function(type){ var args = arguments; switch (type) { case "ContentOf": case "contentof": if (!args[1]) { alert("JavaScript error: XMLHttpRequest.results (type 'ContentOf') requires one or two additional arguments ('tag' and, optionally, 'num')."); return false; }  if (args[2]) { var num = args[2]; } else { var num = 0; }  return this.responseXML.getElementsByTagName(args[1])[num].firstChild.nodeValue; break; case "Error": case "error": if (this.responseXML.getElementsByTagName("error")[0]) { if (args[1] == "code" || args[1] == "Code") { return this.responseXML.getElementsByTagName("error")[0].getAttribute("code"); } else if (args[1] == "info" || args[1] == "Info") { return this.responseXML.getElementsByTagName("error")[0].getAttribute("info"); } else { return true; }  } else { return false; }  break; case "TagAttr": case "tagattr": if (!args[1] || !args[2]) { alert("JavaScript error: XMLHttpRequest.results (type 'TagAttr') requires two or three additional arguments ('tag', 'attr', and, optionally, 'num')."); return false; }  if (args[3]) { var num = args[3]; } else { var num = 0; }  return this.responseXML.getElementsByTagName(args[1])[num].getAttribute(args[2]); break; default: alert("JavaScript error: XMLHttpRequest.results had an unrecognized type. Valid types: 'ContentOf', 'Error', 'TagAttr'."); return false; } };

function userpage_of(pagename){ if(!pagename){ var pagename = wgTitle; } if (wgCanonicalSpecialPageName == "Contributions") { pagename = document.forms[0].target.value; } else if (wgCanonicalSpecialPageName == "Log") { pagename = id("mw-log-user").value; } return pagename.replace(/User(_talk)?\:/, '').replace(/([^\/]*).*/, '$1'); }

//