User:Quarl/rollback.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:Quarl/rollback. |
// User:quarl/rollback.js - rollback button.
// requires wikipage.js, util.js, md5.js
// based on http://sam.zoy.org/wikipedia/godmode-light.js
// <pre><nowiki>
// -----------------------------------------------------------------------------
// God-like Monobook skin
// (c) 2005 Sam Hocevar <sam@zoy.org>
// $Id: godmode-light.js 980 2005-11-12 01:51:51Z sam $
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Language support, taken from phase3/languages/*
// -----------------------------------------------------------------------------
var rollbacklink = 'rollback';
var cantrollback = 'Cannot revert edit; last contributor is only author of this page.';
var alreadyrolled = 'Cannot rollback last edit of [[$1]] by [[User:$2|$2]] ([[User talk:$2|Talk]]); someone else has edited or rolled back the page already. Last edit was by [[User:$3|$3]] ([[User talk:$3|Talk]]). ';
var revertpage = 'Reverted edits by [[User:$2|$2]] ([[User talk:$2|t]]) ([[Special:Contributions/$2|c]]) to last version by $1';
var xmlhttp;
// -----------------------------------------------------------------------------
// Our nice Revert functions
// -----------------------------------------------------------------------------
var gml_vandal, gml_editor;
function rollback_Revert() {
if (queryVars['fakeaction'] != 'rollback') return;
var token = queryVars['token'];
gml_vandal = queryVars['vandal'];
document.cont = document.getElementById('bodyContent');
document.cont.innerHTML = 'Please wait, reverting edits by ' + gml_vandal + '...';
// Avoid XSS kiddies by using a special token
if (!token || token != rollback_mkToken(gml_vandal)) {
document.cont.innerHTML += 'ERROR: Bad authentication token!';
return;
}
xmlhttp = HTTPClient();
if (!xmlhttp) return;
document.getElementById('bodyContent').innerHTML += '<br />Getting article history...';
xmlhttp.open("GET", wikiPage.qurl + '&action=history&limit=50', true);
xmlhttp.overrideMimeType('text/xml');
xmlhttp.onreadystatechange = rollback_Revert2;
xmlhttp.send(null);
}
function rollback_Revert2() {
if (xmlhttp.readyState != 4) return;
var l;
var oldid;
// Get the vandal and new editor names
gml_editor = null;
doc = xmlhttp.responseXML;
l = doc.getElementById('pagehistory').getElementsByTagName('li');
for (i = 0; i < l.length; i++) {
var wp = new WikiPage(l[i].getElementsByTagName('span')[0].getElementsByTagName('a')[0]);
if (!wp.nsUserP) continue; // bad URL
var name = wp.article;
if (i == 0) {
if (name != gml_vandal) {
document.cont.innerHTML += '<br />Error: Last editor is ' + name + ', not ' + gml_vandal + '!';
return;
}
} else {
if (name != gml_vandal) {
oldid = l[i].getElementsByTagName('input')[0].value;
gml_editor = name;
break;
}
}
}
if (!gml_editor) {
document.cont.innerHTML += '<br />Error: ' + gml_vandal + ' is the only editor!';
return;
}
var url = wikiPage.qurl + '&action=edit&oldid=' + oldid;
document.cont.innerHTML += '<br />Getting article edit form...';
xmlhttp = HTTPClient();
xmlhttp.open('GET', url, true);
xmlhttp.onreadystatechange = rollback_Revert3;
xmlhttp.overrideMimeType('text/xml');
xmlhttp.send(null);
}
function rollback_Revert3() {
if (xmlhttp.readyState != 4) return;
var form, newform, l;
// Insert the downloaded form in our current page, using
// only hidden form inputs.
doc = xmlhttp.responseXML;
form = doc.getElementById('editform');
newform = document.createElement('form');
l = form.getElementsByTagName('textarea');
for (i = l.length; i--; ) {
var t = document.createElement('input');
t.type = 'hidden';
t.name = l[i].name;
t.value = l[i].value;
newform.appendChild(t);
}
l = form.getElementsByTagName('input');
for (i = l.length; i--; ) {
if (l[i].name == 'wpSummary') {
l[i].value = revertpage.replace(/\$1/g, gml_editor).replace(/\$2/g, gml_vandal);
} else if (l[i].name == 'wpMinoredit') {
l[i].value = '1';
} else if (l[i].name == 'wpWatchthis') {
if (!l[i].checked)
continue; // Don't touch the "watch" status
l[i].value = "on";
} else if (l[i].name == 'wpPreview') {
continue;
} else if (l[i].name == 'wpDiff') {
continue;
}
l[i].type = 'hidden';
newform.appendChild(l[i]);
}
newform.name = form.name;
newform.method = form.method;
newform.id = form.id;
newform.action = form.action;
document.cont.innerHTML += '<br />Submitting form...';
document.cont.appendChild(newform);
newform.submit(); // Submit the form
}
function rollback_link_to_username(link) {
return link && (new WikiPage(link)).article;
}
function rollback_mkToken(vandal) {
return hex_md5(wikiPage.page + '%' + vandal + '%' + document.cookie);
}
function rollback_mkUrl(wp, vandal) {
return url_getpath(wp.qurl + '&fakeaction=rollback&vandal=' +
escape(vandal) + '&token=' + escape(rollback_mkToken(vandal));
}
// -----------------------------------------------------------------------------
// Add revert buttons to the page
// -----------------------------------------------------------------------------
function rollback_AddButtonDiff() {
var difftag = getElementsByClass('diff-ntitle',document.getElementById('bodyContent'),'td')[0];
if (!difftag) return;
// if toplink has an oldid then this is not a diff against current revision.
var toplink = difftag.getElementsByTagName('a')[0].href;
if (toplink.match(/oldid=/)) return;
var vandal = rollback_link_to_username(difftag.getElementsByTagName('a')[1]);
if (!vandal) { alert("Couldn't parse username in diff page!"); return; }
var url = rollback_mkUrl(wikiPage,vandal);
var newtext = ' <strong>[<a href="' + url + '">' + rollbacklink + '</a>]</strong> ';
difftag.innerHTML = difftag.innerHTML.replace(/(<\/a>)\)( *<br)/i, '$1'+newtext+'$2');
}
function rollback_AddButtonContributions() {
if (wikiPage.page != 'Special:Contributions') return;
var vandal = rollback_link_to_username(document.getElementById('contentSub').getElementsByTagName('a')[0]);
if (!vandal) { alert("Couldn't parse user for Special:Contributions page"); return; }
var l = document.getElementById('bodyContent').getElementsByTagName('li');
for (i = 0; i < l.length; i++) {
var t = l[i].innerHTML;
// If we are already a sysop on this wiki, abort
if (t.match(/>rollback<\/a>]/)) return;
if (t.match(/<strong> \(/)) {
var wp = new WikiPage(l[i].getElementsByTagName('a')[0].href);
var url = rollback_mkUrl(wp,vandal);
l[i].innerHTML += ' [<a href="' + url + '">' + rollbacklink + '</a>]';
}
}
}
function rollback_Load() {
rollback_Revert();
rollback_AddButtonDiff();
rollback_AddButtonContributions();
}
addOnloadHook(rollback_Load);
// </nowiki></pre>