User:Gary/minutes later for diff.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.
/*
	MINUTES LATER FOR DIFF
	Description: When viewing an edit diff, shows how many minutes have passed from the old edit and the new one.
		Only appears if the difference is less than an hour.
*/

function convertTimestampStringToDate(id)
{
	var timestamp = $('#' + id).children().first().children().first().text();
	timestamp = timestamp.substring('Revision as of '.length).match(/(\d\d):(\d\d), (\d{1,2}) ([A-Z][a-z]+) (\d{4})/);
	return new Date(timestamp[4] + ' ' + timestamp[3] + ', ' + timestamp[5] + ' ' + timestamp[1] + ':' + timestamp[2] + ':00');
}

function minutesLaterForDiff()
{
	if (!$('#mw-diff-otitle1').length || !$('#mw-diff-ntitle1').length) return false;

	var leftNode = $('#mw-diff-otitle1');;
	var rightNode = $('#mw-diff-ntitle1');
	var firstDate = convertTimestampStringToDate('mw-diff-otitle1');
	var secondDate = convertTimestampStringToDate('mw-diff-ntitle1');

	var timeDifference = secondDate.getTime() - firstDate.getTime();
	var minutesAgo = Math.round(timeDifference / 1000 / 60);

	if (minutesAgo >= 60) return false;
	else if (minutesAgo < 1) minutesAgo = 'Less than a minute later';
	else if (minutesAgo == 1) minutesAgo = 'One minute later';
	else minutesAgo = minutesAgo + ' minutes later';

	leftNode.prepend($('<span>&nbsp;</span><br />'));
	rightNode.prepend($('<span></span>').append(minutesAgo).append('<br />'));
}

$(minutesLaterForDiff);