User:Pablo X/diffconverter.js

$(function {	if (document.editform)	{		document.editform.wpTextbox1.onkeydown = keydetect;	} });

function keydetect(k) {	if ((k.keyCode == 191) && k.altKey && k.ctrlKey)		//Ctrl-Alt-/ {		if (window.getSelection)			//if any text is selected {			var diffstr = window.getSelection.toString; if (diffstr == "") diffstr = document.editform.wpTextbox1.value.substr(document.editform.wpTextbox1.selectionStart, document.editform.wpTextbox1.selectionEnd - document.editform.wpTextbox1.selectionStart); var diffstr2 = diffstr; var article = decodeURIComponent(urlparse("title", diffstr).replace(/_/g, " ")); var diff = urlparse("diff", diffstr); var oldid = urlparse("oldid", diffstr); var label = "Diff of " + article; var oldidlabel = "Previous revision of " + article; var anchor = ""; if (diffstr.indexOf(" ") != -1) {				label = diffstr.slice(diffstr.indexOf(" ") + 1); diffstr2 = diffstr.slice(0, diffstr.indexOf(" ")); if (label[label.length - 1] == "]") {					label = label.slice(0, label.length - 1); }				oldidlabel = label; }			if (diffstr.indexOf("#") != -1) {				anchor = encodeURIComponent(diffstr2.slice(diffstr.indexOf("#")).replace(/_/g, " ")); if (anchor[anchor.length - 1] == "]") {					anchor = anchor.slice(0, anchor.length - 1); }			}			if (article == "") {				alert("Diffconverter.js: Invalid URL. Ensure that you have selected a valid diff URL, and then press Ctrl-Alt-D."); }			else if (article != "" && diff != "" && oldid != "")	//diff URL should have all three parameters {				var start = document.editform.wpTextbox1.selectionStart; var len = diffstr.length; var difftemplate = "{" + "{diff|" + article + anchor + "|" + diff + "|" + oldid + "|" + label + "}" + "}"; document.editform.wpTextbox1.value = document.editform.wpTextbox1.value.substring(0, start) + difftemplate + document.editform.wpTextbox1.value.substring(start + len); }			else if (article != "" && oldid != "" && diff == "")	//oldid URL doesn't contain a diff parameter {				var start = document.editform.wpTextbox1.selectionStart; var len = diffstr.length; var difftemplate = "{" + "{oldid|" + article + anchor + "|" + oldid + "|" + oldidlabel + "}" + "}"; document.editform.wpTextbox1.value = document.editform.wpTextbox1.value.substring(0, start) + difftemplate + document.editform.wpTextbox1.value.substring(start + len); }			else if (article != "" && oldid == "" && diff == "")	//no diff, no oldid, just create a wikilink out of it			{ var start = document.editform.wpTextbox1.selectionStart; var len = diffstr.length; var wikilink = "" + article + ""; document.editform.wpTextbox1.value = document.editform.wpTextbox1.value.substring(0, start) + wikilink + document.editform.wpTextbox1.value.substring(start + len); }		}		else {			alert("Diffconverter.js: No text selected."); }	} }

function urlparse(name, url)	//thanks to http://www.netlobo.com/url_query_string_javascript.html for this function {	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regexS = "[\\?&]"+name+"=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(url); if(results == null) {		return ""; }	else {		if (results[1].indexOf(" ") != -1) results[1] = results[1].slice(0, results[1].indexOf(" ")); if (results[1][results[1].length - 1] == "]") results[1] = results[1].slice(0, results[1].length - 1); return results[1]; } }