User:Animum/revdelete.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Animum/revdelete. |
importScript('User:Animum/urlparameters.js');
importScript('User:Animum/formatresponse.js');
function makeLink(color, link, text, br) {
var span = document.createElement("span");
span.id = "delete-revision";
var b = document.createElement("b");
b.appendChild(document.createTextNode(" ["));
span.appendChild(b);
var na = document.createElement("a");
na.href = link;
var span2 = document.createElement("span");
span2.style.color = color;
span2.style.fontWeight = "bold";
span2.appendChild(document.createTextNode(text));
na.appendChild(span2);
span.appendChild(na);
var b2 = document.createElement("b");
b2.appendChild(document.createTextNode("]"));
span.appendChild(b2);
return span;
}
function deleteRevision(title, timestamp) {
var req = sajax_init_object();
req.open("GET", wgScript + "?title=Special:Undelete/" + title, false);
req.send(null);
var deletedrevs = req.responseText.split("<fieldset>")[1];
if(deletedrevs) {
deletedrevs2 = deletedrevs.split("<ul>")[1].split("</ul>")[0].split("<li>");
for(x=1;x<deletedrevs2.length;x++) {
if(deletedrevs2[x].split("</li>")[0].split('name="ts')[1]) {
var link = (typeof(link) == "undefined" ? wgScript + "?title=Special:Undelete/" + title + "&norestore=" : link);
link += deletedrevs2[x].split("</li>")[0].split('<input name="ts')[1].split('"')[0] + "|";
}
}
}
var req = sajax_init_object();
req.open("GET", wgScriptPath + "/api.php?format=json&action=query&prop=info&intoken=delete&titles=" + title, false);
req.send(null);
var deletetoken = formatResponse(eval("(" + req.responseText + ")")).deletetoken;
var reason = prompt("Optional reason:");
var postdata = "wpReason=" + (reason.length > 0 ? reason : encodeURIComponent("Deleting to expunge a revision from this page's history."))
+ "&wpEditToken=" + encodeURIComponent(deletetoken);
var req = sajax_init_object();
req.open("POST", wgScript + "?title=" + title + "&action=delete", true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", postdata.length);
req.send(postdata);
req.onreadystatechange = function() {
if(req.status == 200 && req.readyState == 4) {
location.href = link + timestamp;
}
}
}
addOnloadHook(function() {
if(/[?&]diff=/.test(location.href)) {
if(document.getElementById("mw-diff-ntitle1")) {
var article = document.getElementById("mw-diff-ntitle1").getElementsByTagName("a")[0].href.split("?title=")[1].split("&")[0];
var oldid = document.getElementById("mw-diff-ntitle1").getElementsByTagName("a")[0].href.split("oldid=")[1].split("&")[0];
var req = sajax_init_object();
req.open("GET", wgScriptPath + "/api.php?format=json&action=query&prop=revisions&rvlimit=2&rvstartid=" + oldid + "&titles=" + article, false);
req.send(null);
var info = formatResponse(eval("(" + req.responseText + ")"));
if(info.revisions) {
var timestamp = info.revisions[0].timestamp.replace(/[^0-9]/g, "");
var link = makeLink("purple", "javascript:deleteRevision(\"" + article + "\", '" + timestamp + "')", "delete revision", true);
getElementsByClassName(document, "td", "diff-ntitle")[0].insertBefore(link, document.getElementById("mw-diff-ntitle4"));
}
}
}
if(wgPageName == "Special:Undelete") {
if(UrlParameters["norestore"]) {
var inputs = document.getElementsByTagName("input");
for(i=0;i<inputs.length;i++) {
var input = inputs[i];
if(input.type == "checkbox") {
input.checked = true;
}
var norestore_timestamps = UrlParameters["norestore"].split("|");
for(n=0;n<norestore_timestamps.length;n++) {
if(input.name.indexOf("ts" + norestore_timestamps[n]) != -1 || input.checked == false) {
input.checked = false;
}
}
}
document.getElementById("wpComment").value = "Expunging revision from page history.";
document.getElementById("mw-undelete-submit").click();
}
}
});