User:Quarenon/rcrollback.js

/* * This code is available under GPL. */

// Add rollback links to the recent changes list. function initRcRollback { var lists = getElementsByClassName(document,'ul','special'); for (i = 0; i < lists.length; i++) { var items = lists[i].getElementsByTagName('li'); for (j = 0; j < items.length; j++) { var links = items[j].getElementsByTagName('a'); if (links[0].innerHTML != "diff") { continue; }

var title = links[2].innerHTML; var user = links[3].innerHTML;

var span = document.createElement('span'); span.setAttribute('class', 'rc-rollback'); span.setAttribute('className', 'rc-rollback'); var a = document.createElement('a'); a.setAttribute('href', 'javascript:doRollback("' + encodeURIComponent(title) + '", "' + encodeURIComponent(user) + '");'); a.appendChild(document.createTextNode('rollback')); span.appendChild(document.createTextNode(' [')); span.appendChild(a); span.appendChild(document.createTextNode(']')); items[j].appendChild(span); }	}	}

// Perform a rollback. First, use AJAX to obtain a rollback token, then redirect to the rollback page. function doRollback(title, user) { var ajax = null;

if (window.XMLHttpRequest) { ajax = new XMLHttpRequest; } else { ajax = new ActiveXObject("Microsoft.XMLHTTP"); }

ajax.onreadystatechange = function { if (ajax.readyState === 4) { if (ajax.status == 200) { var revs = ajax.responseXML.getElementsByTagName("rev"); var token = revs[0].getAttribute('rollbacktoken');

if (token) { document.location.href = wgScript + '?title=' + title + '&action=rollback&from=' + user + '&token=' + encodeURIComponent(token); }			} else { // Server error }		}	}

ajax.open('GET', wgScriptPath + '/api.php?action=query&prop=revisions&rvtoken=rollback&format=xml&titles=' + title, true); ajax.send(null); }

// Check for RC page and user has sufficient rights if (wgCanonicalSpecialPageName && wgCanonicalSpecialPageName == 'Recentchanges' && wgUserGroups && wgUserGroups.join(' ').indexOf('sysop') != -1) { addOnloadHook(initRcRollback) }