Jump to content

User:TFOWR/easyDiff2.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.
// Schedule it to run after the HTML page is rendered
$(fullUrlLinks);

// Define main function
function fullUrlLinks() {
	importStylesheet("User:TFOWR/easyDiff2.css");

	doDiffs();
	if (mw.config.get('wgNamespaceNumber') === -1) {
		doSpecialLog();
	}
}

function doSpecialLog() {
	var url = document.URL;
	if ('Special:Log' == getTitle(url)) {
		var span = getSpan();
		span.setAttribute('style', 'float: right;');
		var args = new Array('type', 'user', 'page');
		span.innerHTML += '{{fullurl:Special:Log' + getKvPairsString(args, url) + '}}';
		document.getElementById('siteSub').appendChild(span);
	}
}

function doDiffs() {

	if (document.URL.indexOf('diff=') != -1) {
		var prev = document.getElementById('mw-diff-otitle4');
		var current = document.getElementById('mw-diff-ntitle4');
		var prevId = getRevId(document.getElementById('mw-diff-otitle1').firstChild.firstChild.href);
		var currentId = getRevId(document.getElementById('mw-diff-ntitle1').firstChild.firstChild.href);

		// Left : diff2
		if ((prevId != null) && ('prev' != prevId) && ('next' != prevId)) {
			var span = getSpan();
			var br = getBr();
			prev.appendChild(br);
			span.innerHTML += '{{diff2|' + prevId + '|<i>label</i>}}';
			prev.appendChild(span);
		}

		// Right : diff2
		if ((currentId != null) && ('prev' != currentId)
				&& ('next' != currentId)) {
			var span = getSpan();
			var br = getBr();
			current.appendChild(br);
			span.innerHTML += '{{diff2|' + currentId + '|<i>label</i>}}';
			current.appendChild(span);
		}
		// Left-right : diff
		if ((prevId != null) && (prevId != 'prev') && (prevId != 'current')
				&& ((currentId != null) && currentId != 'prev')
				&& (currentId != 'current')) {
			var span = getSpan();
			var br = getBr();
			current.appendChild(br);
			span.innerHTML += '{{diff|1=' + getTitle(document.URL) + '|2='
					+ currentId + '|3=' + prevId + '|4=<i>label</i>}}';
			current.appendChild(span);
		}
	}
}

function getSpan() {
	var rv = document.createElement('span');
	rv.setAttribute('class', 'tfowr-diff');
	return rv;
}

function getBr() {
	return document.createElement('br');
}

function getRevId(href) {
	var rv = getSubstring(href, "oldid=", "&");
	if (rv != null)
		return rv;

	return getSubstring(document.URL, "diff=", "&");
}

function getTitle(url) {
	return unescape(getSubstring(url, "title=", "&"));
}

function getKvPairsString(args, url) {
	var rv = '';
	for ( var i = 0; i < args.length; i++) {
		var key = args[i];
		var value = getSubstring(url, key + "=", "&");
		if (value != null && value != '') {
			if (rv == '') {
				rv += '|';
			} else {
				rv += '&';
			}
			rv += key + '=' + unescape(value);
		}
	}
	return rv;
}

function getSubstring(stringIn, start, end) {
	if (stringIn == null)
		return null;

	if (stringIn.indexOf(start) == -1) {
		return null;
	}

	var startIdx = stringIn.indexOf(start) + start.length;
	var endIdx = stringIn.indexOf(end, startIdx);
	if (endIdx != -1) {
		return stringIn.substring(startIdx, endIdx);
	} else {
		return stringIn.substring(startIdx);
	}
}