User:Lightdarkness/godmode2.js

/* */ // Gonna try and speed up godmode // - // God-like Monobook skin // (c) 2005 Sam Hocevar  // $Id: godmode-light.js 1029 2006-01-09 11:05:19Z 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 $2 (Talk); someone else has edited or rolled back the page already. Last edit was by $3 (Talk). '; var finalmessage = 'Talk'; var revertpage = 'Reverted edits by $2 (Talk) to last version by $1';

// - // XMLHttpRequest support // - if (document.implementation.createDocument) { var gml_xmlparser = new DOMParser; }

function gml_XMLParse(string) { if (document.implementation.createDocument) { return gml_xmlparser.parseFromString(string, "text/xml"); } else if (window.ActiveXObject) { var gml_xmldoc = new ActiveXObject("Microsoft.XMLDOM"); gml_xmldoc.async = "false"; ret = gml_xmldoc.loadXML(string); if (!ret) return null; return gml_xmldoc.documentElement; } return null; }

var gml_xmlhttp;

function HTTPClient { var gml_http; if(window.XMLHttpRequest) { gml_http = new XMLHttpRequest; } else if (window.ActiveXObject) { try { gml_http = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { gml_http = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { gml_http = false; }   }  }  return gml_http; }

// - // Our nice Revert functions // - var gml_vandal, gml_editor, gml_url;

function PerformRevert(vandal) { var l, token = '', revert = false; gml_vandal = vandal; gml_url = 'http://en.wikipedia.org/w/index.php?title='; gml_url += document.getElementsByTagName('h1')[0].innerHTML; gml_xmlhttp = HTTPClient; if (!gml_xmlhttp) return; history_url = gml_url + '&action=history&limit=50'; document.getElementById('bodyContent').innerHTML = 'Getting article history (' + history_url + ')... '; gml_xmlhttp.open("GET", history_url, true); gml_xmlhttp.onreadystatechange = RevertStepTwo; gml_xmlhttp.send(null); }

function RevertStepTwo { if (gml_xmlhttp.readyState != 4) return; var l; var oldid; // Get the vandal and new editor names gml_vandal = gml_vandal.replace(/_/g, ' '); gml_editor = ''; // Hack for a MediaWiki bug xml = gml_xmlhttp.responseText.replace(//, ''); doc = gml_XMLParse(xml); if (!doc || !doc.getElementById('pagehistory')) { document.getElementById('godmode').innerHTML += ' Failed to parse XML data returned by the server.'; document.getElementById('godmode').innerHTML += ' Hint: try to disable both "Enable section editing by right-clicking on section titles (JavaScript)" and "Edit pages on double click (JavaScript)" in your editing preferences until a proper fix is found.'; document.getElementById('godmode').innerHTML += ' Dump of the unparseable page : ' + xml.replace(/&/g, '&amp;').replace(//g, '&gt;'); return; } l = doc.getElementById('pagehistory').getElementsByTagName('li'); //l = doc.selectSingleNode('//*[@id="pagehistory"]').getElementsByTagName('li'); for (i = 0; i < l.length; i++) { var name = l[i].getElementsByTagName('span')[0].getElementsByTagName('a')[0].innerHTML.replace(/_/g, ' '); if (i == 0 && name != gml_vandal) { document.getElementById('godmode').innerHTML += ' Error: Last editor is ' + name + ', not  ' + gml_vandal + '!'; window.location('http://en.wikipedia.org/wiki/User:Lightdarkness'); return; } else if (i > 0 && name != gml_vandal) { oldid = l[i].getElementsByTagName('input')[0].value; gml_editor = name; break; } }  if (gml_editor == '') { document.getElementById('godmode').innerHTML += ' Error: ' + gml_vandal + ' is the only editor!'; return; }

gml_xmlhttp = HTTPClient; if (!gml_xmlhttp) return; document.getElementById('godmode').innerHTML += ' Getting article edit form (GET' + gml_url + '&action=edit&oldid=' + oldid + ')...'; gml_xmlhttp.open('GET', gml_url + '&action=edit&oldid=' + oldid, true); gml_xmlhttp.onreadystatechange = RevertStepThree; gml_xmlhttp.send(null); }

function RevertStepThree { if (gml_xmlhttp.readyState != 4) return var form, newform, l; // Insert the downloaded form in our current page, using // only hidden form inputs. doc = gml_XMLParse(gml_xmlhttp.responseText); 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.getElementById('godmode').innerHTML += ' Submitting form... (Talk)'; document.getElementById('bodyContent').appendChild(newform); // Submit the form newform.submit; }

function GML2Revert(ouroldid) { theoldid = ouroldid; var reason = prompt('Why are you reverting to this version?'); if(!reason) return; GML2StepTwo(reason); }

function GML2StepTwo(reason) { thereason = reason; gml_xmlhttp = HTTPClient; if (!gml_xmlhttp) return; document.getElementById('bodyContent').innerHTML = ' Getting article edit form (GET' + document.getElementsByTagName('h1')[0].innerHTML + '&action=edit&oldid=' + theoldid + ')...'; gml_xmlhttp.open('GET', 'http://en.wikipedia.org/w/index.php?title=' + document.getElementsByTagName('h1')[0].innerHTML + '&action=edit&oldid=' + theoldid, true); gml_xmlhttp.onreadystatechange = GML2StepThree; gml_xmlhttp.send(null); }

function GML2StepThree { if (gml_xmlhttp.readyState != 4) return var form, newform, l; // Insert the downloaded form in our current page, using // only hidden form inputs. doc = gml_XMLParse(gml_xmlhttp.responseText); 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 = "Reverted to revision id (" + theoldid + ") using GML2, (" + thereason + ")"; } 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.getElementById('bodyContent').innerHTML += ' Submitting form...'; document.getElementById('bodyContent').appendChild(newform); // Submit the form newform.submit; }

// - // Add revert buttons to the page // - function AddRevertButtons { if(location.href.indexOf('&diff=') != -1){ var l, article = '', vandal; // Add 'revert' links to a diff page l = document.getElementById('bodyContent').getElementsByTagName('td'); for (i = 0; i < l.length; i++) { if (l[i].className == 'diff-otitle') { article = l[i].getElementsByTagName('a')[0].href.split('&')[0].replace(/[^\/]*\/\/[^\/]*/, ''); if((location.href.indexOf('&diff=') != -1) && (location.href.indexOf('&oldid=') != -1)){ var t = l[i].innerHTML; n = t.indexOf(' Revert to this version] ' + t.substring(n + 13, t.length); }   } else if (l[i].className == 'diff-ntitle') { var toplink = l[i].getElementsByTagName('a')[0].href; vandal = l[i].getElementsByTagName('a')[2].title.split(':')[1]; var t = l[i].innerHTML n = t.indexOf(') ) = 0 && article != '' && toplink.indexOf('oldid=') == -1) { l[i].innerHTML = t.substring(0, n + 5) + '   [' + rollbacklink + '] ' + t.substring(n + 5, t.length); }   }  }} }

if (window.addEventListener) { window.addEventListener("load", AddRevertButtons, false); } else if (window.attachEvent) { window.attachEvent("onload", AddRevertButtons); }

/* */