Jump to content

User:Gary/mark todays edits.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.
/*
	MARK TODAY'S EDITS
	Description: Marks edits on contributions pages that were made today.
*/

if (typeof(unsafeWindow) != 'undefined')
{
	mw = unsafeWindow.mw;
}

$(markTodaysEdits);
function markTodaysEdits()
{
	if (!mw.config.get('wgPageName').match('Special:Contributions') && mw.config.get('wgAction') != 'history') return false;
	
	var today = new Date();
	var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	var firstMatchedNode, lastMatchedNode;
	var maxTime = 1000 * 60 * 60 * 24;
	
	$('#bodyContent ul:last').children().each(function()
	{
		var edit = $(this);
		if (edit[0].nodeType == 3) return true;
		if (edit.contents().length <= 3) edit = edit.children().first();
		
		if (mw.config.get('wgPageName').match('Special:Contributions')) var date = new Date(edit.children().first().text());
		else if (mw.config.get('wgAction') == 'history') var date = new Date(edit.children().eq(3).text());
		
		// Revision's date is less than 24 hours
		if ((today.getTime() - date.getTime()) < maxTime)
		{
			if (firstMatchedNode) lastMatchedNode = edit;
			else firstMatchedNode = lastMatchedNode = edit;
		}
	});
	
	if (firstMatchedNode && lastMatchedNode)
	{
		if (mw.config.get('wgPageName').match('Special:Contributions'))
		{
			firstMatchedNode = firstMatchedNode.children().first();
			lastMatchedNode = lastMatchedNode.children().first();
		}
		else if (mw.config.get('wgAction') == 'history')
		{
			firstMatchedNode = firstMatchedNode.children().eq(3);
			lastMatchedNode = lastMatchedNode.children().eq(3);
		}
		
		var firstColorRatio = calculateColorRatio(maxTime, 70, 95, new Date(firstMatchedNode.text()));
		var lastColorRatio = calculateColorRatio(maxTime, 70, 95, new Date(lastMatchedNode.text()));
		
		firstMatchedNode.css('background-color', 'rgb(' + firstColorRatio + '%, ' + firstColorRatio + '%, 100%)');
		lastMatchedNode.css('background-color', 'rgb(' + lastColorRatio + '%, ' + lastColorRatio + '%, 100%)');
	}
}

function calculateColorRatio(maxTime, minPercentage, maxPercentage, timestamp)
{
	var today = new Date();
	minPercentage = minPercentage / 100;
	maxPercentage = maxPercentage / 100;
	var colorRatio = ((maxPercentage - minPercentage) * (1 - (today.getTime() - timestamp.getTime()) / maxTime) + minPercentage) * 100;
	if (colorRatio < minPercentage) colorRatio = minPercentage;
	return colorRatio;
}