User:Dinnerbone/Vandal box/beta.js

//

// Variables are fun! var params; var cBox; var http_request = false; var temp_summary = false; vbWarnReasons = new Array; // I'm bored of variables now, let's try something else.

// How about some DOM functions? document.getElementsByClassName = function(clsName,tag){ var retVal = new Array; if (tag == null) { tag="*"; }	var elements = document.getElementsByTagName(tag); for (var i = 0;i < elements.length;i++) { var classes = elements[i].className.split(" "); for (var j = 0;j < classes.length;j++) { if (classes[j] == clsName) { retVal.push(elements[i]); }		}	}	return retVal; }

function addLink(url, text) { var newLink = document.createElement("a"); newLink.href = url; newLink.appendChild(text || addText(url)); return newLink; }

function addText(text) { return document.createTextNode(text); }

function getBody(content) { var x = content.indexOf("", x); var y = content.lastIndexOf(" "); return content.slice(x + 1, y); }

function showWarnOptions(user) { var contentBox = document.getElementById("content"); var optionsBox = document.createElement("div"); var optionsList = document.createElement("ul"); for (var i=0; i<vbWarnReasons.length; i++) { var listOption = document.createElement("li"); listOption.appendChild(addLink("javascript:showWarnOptionsExt('" + escape(user) + "', " + escape(i) + ")", addText(vbWarnReasons[i].reason))); optionsList.appendChild(listOption); }   contentBox.removeChild(document.getElementById("bodyContent")); optionsBox.appendChild(optionsList); contentBox.appendChild(optionsBox);

document.getElementsByClassName("firstHeading")[0].textContent = "Giving warning to user " + user;

optionsBox.id = "bodyContent"; }

function showWarnOptionsExt(user, reason) { var contentBox = document.getElementById("content"); var optionsBox = document.createElement("div"); var contentBack = document.createElement("span"); var contentTop = document.createElement("div"); var optionsList = document.createElement("ul");

for (var i=1; i<=vbWarnReasons[reason].levels; i++) { var listOption = document.createElement("li"); var desc = ""; if (i == 1) { desc = "Assumes good faith, generally includes a 'welcome to wikipedia' notice, very light warning."; } else if (i == 2) { desc = "Makes no faith assumptions, light warning."; } else if (i == 3) { desc = "Assumes bad faith, moderate warning."; } else { desc = "Assumes bad faith, strong and final warning."; }       listOption.appendChild(addLink("javascript:giveWarning('" + escape(user) + "', '" + escape(wgTitle) + "')", addText("Warn level " + i + ": " + desc + " - " + wgTitle+ ""))); optionsList.appendChild(listOption); }   if (vbWarnReasons[reason].im) { var listOption = document.createElement("li"); var desc = "Assumes bad faith, first and last warning.";

listOption.appendChild(addLink("javascript:giveWarning('" + escape(user) + "', '" + escape(wgTitle) + "')", addText("Warn level 4im: " + desc + " - " + wgTitle + ""))); optionsList.appendChild(listOption); }   contentTop.appendChild(contentBack); contentBack.appendChild(addText("< ")); contentBack.appendChild(addLink("javascript:showWarnOptions('" + escape(user) + "')", addText("List of reasons")));

contentBox.removeChild(document.getElementById("bodyContent"));

optionsBox.id = "bodyContent"; contentTop.id = "contentSub"; contentBack.className = "subpages";

optionsBox.appendChild(contentTop); optionsBox.appendChild(optionsList); contentBox.appendChild(optionsBox); } // Well, that got boring quick.

// I hear AJAX is rather fun this time of year. function makePOSTRequest(url, parameters, func) { http_request = false; if (window.XMLHttpRequest) { // Mozilla, Safari,... http_request = new XMLHttpRequest; if (http_request.overrideMimeType) { // set type accordingly to anticipated content type //http_request.overrideMimeType('text/xml'); http_request.overrideMimeType('text/html'); }	} else if (window.ActiveXObject) { // IE		try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} }	}	if (!http_request) { alert('Cannot create XMLHTTP instance'); return false; }	http_request.onreadystatechange = func; http_request.open('POST', url, true); http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http_request.setRequestHeader("Content-length", parameters.length); http_request.setRequestHeader("Connection", "close"); http_request.send(parameters); }

function makeGETRequest(url, func) { http_request = false; if (window.XMLHttpRequest) { // Mozilla, Safari,... http_request = new XMLHttpRequest; if (http_request.overrideMimeType) { // set type accordingly to anticipated content type //http_request.overrideMimeType('text/xml'); http_request.overrideMimeType('text/html'); }	} else if (window.ActiveXObject) { // IE		try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} }	}	if (!http_request) { alert('Cannot create XMLHTTP instance'); return false; }	http_request.onreadystatechange = func; http_request.open('GET', url, true); http_request.send(null); }

function reloadPage { if (http_request.readyState == 4) { if (http_request.status == 200) { result = http_request.responseText; document.location = document.location; } else { alert('There was a problem with the request.'); }	} }

function addPOSTVar(name, variable) { return name + "=" + encodeURIComponent(variable); } // Hmm, that was over-rated.

// Let's try some general functions function onLoadVB { loadVBDefaults; if (wgAction == "history") { doHistory; } }

function addWarnReason(reason, levels, template, im) { var newReason = new Array; newReason.reason = reason; newReason.levels = levels; newReason.template = template; newReason.im = im; vbWarnReasons.push(newReason); } // So boring.

// Ok then, I'll do the main functions. function revertTo2 { var oldID = temp_oldID; if (http_request.readyState == 4) { if (http_request.status == 200) { var result = http_request.responseText; var temp = document.body.innerHTML; var POSTvars = "";

document.body.innerHTML = getBody(result);

var editToken = document.getElementsByName("wpEditToken")[0].value; var autoSummary = document.getElementsByName("wpAutoSummary")[0].value; var revertText = document.getElementsByName("wpTextbox1")[0].value; var startTime = document.getElementsByName("wpStarttime")[0].value; var editTime = document.getElementsByName("wpEdittime")[0].value; var minorEdit = document.getElementsByName("wpMinoredit")[0].value; var watchList = document.getElementsByName("wpWatchthis")[0].value; var author = document.getElementById("mw-revision-name").getElementsByTagName("a")[0].text;

document.body.innerHTML = temp; POSTvars = addPOSTVar("wpTextbox1", revertText) + "&" + addPOSTVar("wpSummary", temp_summary) + "&" + addPOSTVar("wpMinoredit", minorEdit) + "&" + addPOSTVar("wpWatchthis", watchList) + "&" + addPOSTVar("wpSave", "Save page") + "&" + addPOSTVar("wpSection", "") + "&" + addPOSTVar("wpStarttime", startTime) + "&" + addPOSTVar("wpEdittime", editTime) + "&" + addPOSTVar("wpScrolltop", "") + "&" + addPOSTVar("wpAutoSummary", autoSummary) + "&" + addPOSTVar("wpEditToken", editToken).replace("+", "%2B"); makePOSTRequest('/w/index.php?title=' + wgPageName + '&action=submit', POSTvars, reloadPage); } else { alert('There was a problem with the request.'); }	} }

function revertTo(oldID, summary) { temp_oldID = oldID; temp_summary = summary; makeGETRequest('/w/index.php?title=' + wgPageName + '&action=edit&oldid=' + oldID, revertTo2); }

function giveWarning(user, warning) { temp_warning = warning; temp_user = user; makeGETRequest('/w/index.php?title=User_talk:' + user + '&action=edit', giveWarning2); }

function giveWarning2 { if (http_request.readyState == 4) { if (http_request.status == 200) { var result = http_request.responseText; var temp = document.body.innerHTML; var POSTvars = "";

document.body.innerHTML = getBody(result);

var editToken = document.getElementsByName("wpEditToken")[0].value; var autoSummary = document.getElementsByName("wpAutoSummary")[0].value; var revertText = document.getElementsByName("wpTextbox1")[0].value; var startTime = document.getElementsByName("wpStarttime")[0].value; var editTime = document.getElementsByName("wpEdittime")[0].value; var minorEdit = document.getElementsByName("wpMinoredit")[0].value; var watchList = document.getElementsByName("wpWatchthis")[0].value;

document.body.innerHTML = temp; POSTvars = addPOSTVar("wpTextbox1", revertText + "\n" + temp_warning + " ~") + "&" + addPOSTVar("wpSummary", "Adding " + temp_warning) + "&" + addPOSTVar("wpMinoredit", minorEdit) + "&" + addPOSTVar("wpWatchthis", watchList) + "&" + addPOSTVar("wpSave", "Save page") + "&" + addPOSTVar("wpSection", "") + "&" + addPOSTVar("wpStarttime", startTime) + "&" + addPOSTVar("wpEdittime", editTime) + "&" + addPOSTVar("wpScrolltop", "") + "&" + addPOSTVar("wpAutoSummary", autoSummary) + "&" + addPOSTVar("wpEditToken", editToken).replace("+", "%2B"); makePOSTRequest('/w/index.php?title=User_talk:' + temp_user + '&action=submit', POSTvars, reloadPage); }   } }

function revertAll(oldID, author, oldIndex) { var safeID = false; var safeAuthor = false; var safeIndex = false;

var history = document.getElementById("pagehistory"); var hItems = history.getElementsByTagName("li"); var len = hItems.length; var oldIDs = document.getElementsByName("oldid"); var authors = document.getElementsByClassName("history-user"); var alinks = document.getElementsByClassName("mw-history-undo");

for (var i=oldIndex; i<len; i++) { if (authors[i].getElementsByTagName("a")[0].innerHTML != author) { safeID = oldIDs[i].value; safeIndex = i;               safeAuthor = authors[i].getElementsByTagName("a")[0].innerHTML; break; }       }        if (safeID) { temp_oldID = safeID; temp_summary = "Reverting " + (safeIndex - oldIndex) + " edits by " + author + " (c/t) to version " + safeID + " by " + safeAuthor + ""; makeGETRequest('/w/index.php?title=' + wgPageName + '&action=edit&oldid=' + safeID, revertTo2); } else { alert("There are no safe version to revert to; " + author + " seems to be the only contributor in sight. Perhaps there are more versions on the next page?"); } }

function doHistory { var history = document.getElementById("pagehistory"); var hItems = history.getElementsByTagName("li"); var len = hItems.length; var oldIDs = document.getElementsByName("oldid"); var authors = document.getElementsByClassName("history-user"); var alinks = document.getElementsByClassName("mw-history-undo"); for (var i=0; i<len; i++) { var hItem = hItems[i]; var actionLinks = alinks[i]; var oldID = oldIDs[i].value; var author = authors[i].getElementsByTagName("a")[0].innerHTML; if (i < len - 1) { var revertSummary = "Reverting edits by " + author + " (c/t) to version " + oldIDs[i + 1].value + " by " + authors[i + 1].getElementsByTagName("a")[0].innerHTML + "";

var revert = addLink("javascript:revertTo('" + oldIDs[i + 1].value + "', '" + revertSummary + "')", addText("revert")); var revertAll = addLink("javascript:revertAll('" + oldID + "', '" + author + "', " + i + ")", addText("revert all")); var warn = addLink("javascript:showWarnOptions('" + author + "')", addText("warn")); actionLinks.appendChild(addText(", ")); actionLinks.appendChild(revert); actionLinks.appendChild(addText(", ")); actionLinks.appendChild(revertAll); actionLinks.appendChild(addText(", ")); actionLinks.appendChild(warn); }	} } // Meh, that wasn't half as fun as I thought it would be.

// Let's finish this up. if (window.addEventListener) window.addEventListener("load", doLoad, false); else if (window.attachEvent) window.attachEvent("onload", doLoad); function doLoad { setTimeout(onLoadVB, 500); } // Bye!

//