User:Lightdarkness/GML2.js

/* */

// - // 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;

var theoldid; var thereason;

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 AddGML2Buttons { l = document.getElementById('bodyContent').getElementsByTagName('li'); for (iaa = 0; iaa < l.length; iaa++) { //myoldid = l[iaa].getElementsByTagName('a')[2].href.split('&oldid=')[1]; myoldid = 1; if(myoldid != undefined){ //l[iaa].innerHTML += ' (roll) '; }   else{ l[iaa].innerHTML += ' (roll) '; } } }

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

/* */