Jump to content

User:Gary/minutes later for diff.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gary (talk | contribs) at 01:06, 2 June 2010 (lnk). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
	Link: [[User:Gary King/minutes later for diff.js]]
*/

addOnloadHook(function()
{
	function convertTimestampStringToDate(id)
	{
		var timestamp = document.getElementById(id).firstChild.firstChild.firstChild.nodeValue;
		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');
	}

	if (!document.getElementById('mw-diff-otitle1') || !document.getElementById('mw-diff-ntitle1'))
		return;

	var leftNode = document.getElementById('mw-diff-otitle1');
	var rightNode = document.getElementById('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;
	else if (minutesAgo < 1)
		minutesAgo = 'Less than a minute later';
	else if (minutesAgo == 1)
		minutesAgo = 'One minute later';
	else
		minutesAgo = minutesAgo + ' minutes later';

	var newNode = document.createElement('span');
	// newNode.style.fontSize = '90%';
	newNode.appendChild(document.createTextNode(minutesAgo));
	newNode.appendChild(document.createElement('br'));

	rightNode.insertBefore(newNode, rightNode.firstChild);
});