User:Whoda/drek.js

/********************************/ /* DREK - Double Redirect Fixer */ /* v1.0, by Whoda              */ /*     17 Jun 2006             */ /********************************/

var drek={ debugMode:false, wikiUrl:"http://en.wikipedia.org"/*location.protocol+"//"+location.host*/+"/w/index.php?redirect=no&action=", rxRedirect:/^\s*#REDIRECT:?\s*\[\[([^\]]+)\]\][\s\S]*$/i, clickHandler:function (e) { /* Handle all document clicks */ var oEl; if (e && e.ctrlKey && (oEl=e.srcElement || e.target) && oEl.tagName=="A") { /* Filter A elements */ var rxMatch=/^\r\n]*>([^<]+)<\/A> ]*>\(Edit\)<\/A> \u2192 ]*>([^<]+)<\/A> \u2192 ]*>([^<]+)<\/A> $/,				sCode=oEl.parentNode.outerHTML;			if (rxMatch.test(sCode)) { /* Attempt to match Special:DoubleRedirects item syntax */				var sEditPage=RegExp.$1,					sTitle2=RegExp.$2,					sTitle3=RegExp.$3;				/* Initiate fix process */				drek.fixDbl(sEditPage,sTitle2,sTitle3);				return e.returnValue=false; /* Cancel link click */			}		}	},	fixDbl:function (sEditPage,sTitle2,sTitle3) {		/* Verify first redirect, fetch current page contents */		drek.verifyTitles(			sEditPage,			sTitle2,			function (sOldTarget,sContent) {		/* Verify second redirect */		drek.verifyTitles(			sOldTarget,			sTitle3,			function (sNewTarget) {		/* Get hidden fields required for submitting          */ /* (specifically wpStarttime, wpEdittime & wpEditToken */		drek.getHiddenFields( sEditPage, function (sFields) { /* Prepare and submit amended article */ var sSummary="Fixed double redirect: "+sOldTarget+" \u2192 "+sNewTarget+""; sContent=sContent.replace(drek.rxRedirect,"#REDIRECT "+sNewTarget+""); drek.fetch(			drek.wikiUrl+"submit&title="+encodeURIComponent(sEditPage),			function (sResponse) {				/* Copy response to clipboard */				clipboardData.setData("text",sResponse);				alert("Done!");			},			sFields+"&wpMinoredit=1&wpSummary="+encodeURIComponent(sSummary)+"&wpTextbox1="+encodeURIComponent(sContent)		); }		);			}		);			}		);	},	verifyTitles:function (sSource,sTarget,fCallback) {		/* Fetch source page, and compare actual redirect target to logged one */		drek.fetch( drek.wikiUrl+"raw&title="+encodeURIComponent(sSource), function (sText) { if (drek.rxRedirect.test(sText)) { var sTrueTarget=RegExp.$1; /* When non-identical, offer both options, plus option to abort completely */ if (sTarget!=sTrueTarget && sTarget) { if (!confirm("ok: "+sTrueTarget+", cancel: "+sTarget)) { if (confirm("ok: "+sTarget+", cancel - abort")) sTrueTarget=sTarget; /* Retain original title */ else return false; /* Aborted by user */ }					}					fCallback(sTrueTarget,sText); } else alert("Unable to find redirect code in "+sSource+", aborting\r\n'"+sText+"'"); }		);	},	getHiddenFields:function (sTitle,fCallback) {		/* Fetch inputs of type hidden, and return a urlencoded string */		drek.fetch( drek.wikiUrl+"edit&title="+encodeURIComponent(sTitle), function (sText) { var aFields=[], aInput, rxInput=//g;				while (aInput=rxInput.exec(sText)) {					for (var i=1,o={};i= 5)		try { oXH=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {			try { oXH=new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) {}		}		@end @*/		if (!oXH) return alert("Unable to initialize");		if (drek.debugMode && sData) return alert(sData);		/* Initialize a GET/POST HTTP request */		oXH.open(sData ? "POST" : "GET",sUrl,true);		/* When posting data, set proper headers */		if (sData) {			oXH.setRequestHeader("Method","POST "+sUrl+" HTTP/1.1"); oXH.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); }		/* Call callback function when complete */ oXH.onreadystatechange=function { if (oXH.readyState!=4) return; fCallback(oXH.responseText); }		/* Send the request */ oXH.send(sData); } }

if (document && document.attachEvent && document.attachEvent("onclick",drek.clickHandler)) status="drek initialized";