User:DoriSmith/scripts/getdiff.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Adds a "Diff" tab which pops up a prompt box; put in a url and get back a {{diff}}
function wpTextboxDiff() {
	var theURL = prompt("Enter URL:","");
	if (theURL) {
		var theTitle = getParam('title');
		var theDiff = getParam('diff');
		var theOldid = getParam('oldid');
		var diffTxt = "Not a valid URL";

		if (theTitle != '' && theDiff != '') {
			diffTxt = '{{diff|' + theTitle + '|' + theDiff + '|' + theOldid + '|}}'; 
		}
		if (theTitle != '' && theDiff == '') {
			diffTxt = '{{oldid|' + theTitle + '|' + theOldid + '|}}'; 
		}
		prompt("Your template is:",diffTxt);
	}

	function getParam(name) {
		var result = '';
		var start = theURL.indexOf("?"+name+"=");
		if (start < 0) start = theURL.indexOf("&"+name+"=");
		if (start >= 0) {
			start += name.length+2;
			var end = theURL.indexOf("&",start)-1;
			if (end < 0) {
				end = theURL.length;
				if (theURL.charCodeAt(end-1)<48) end-=2;
			}
			for (var i=start; i<=end; i++) {
				var c = theURL.charAt(i);
				result += (c=='+' || c=='_') ? ' ' : c;
			}
		}
		return unescape(result);
	}
}

$(function() {
    mw.util.addPortletLink('p-cactions', 'javascript:wpTextboxDiff()', 'Diff', 'ca-diff',
    	'Diff window', '', document.getElementById('ca-history'));
});