User:Sid/HistDiffTemp

From RationalWiki
Jump to navigation Jump to search
// ==UserScript==
// @name           HistoryDiff
// @namespace      http://rationalwiki.com
// @description    Tweak History display to replace byte size with diff size (just like in the Recent Changes)
// @include        http://www.conservapedia.com/*action=history*
// @include        http://conservapedia.com/*action=history*
// @include        http://rationalwiki.com/*action=history*
// ==/UserScript==

(function(){
var tagselect, subselect, currently, diff, previous, format, length;
tagselect = document.getElementById("bodyContent");
subselect = document.evaluate("./form/ul/li/span[@class='history-size']", tagselect, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 
length = subselect.snapshotLength;

function parsesize(z)
{
	var size;
	size=subselect.snapshotItem(z).innerHTML;
	format=size;
	if (size=="(empty)")
	{
		size=0;
	}
	else
	{
		size=size.slice(1,-7);
		size=size.replace(/,/,"");
		size=parseInt(size);
	}
	return size;
}

if (length > 1)
{
	previous=parsesize(length-1);
}

for (var i = length-2; i >= 0; i--)
{
	currently=parsesize(i);
	diff=currently-previous;
	previous=currently;
	if (diff>0)
	{
		if (diff>999)
		{
			diff='<strong class="mw-plusminus-pos">(+' + diff + ')</strong>';
		}
		else
		{
			diff='<span class="mw-plusminus-pos">(+' + diff + ')</span>';
		}
	}
	if (diff <0)
	{
		if (diff<-999)
		{
			diff='<strong class="mw-plusminus-neg">(' + diff + ')</strong>';
		}
		else
		{
			diff='<span class="mw-plusminus-neg">(' + diff + ')</span>';
		}
	}
	if (diff==0)
	{
		diff='<span class="mw-plusminus-null">(0)</span>';
	}

	//If you want to show the total file size and the diff size, simply uncomment the next line and comment out the one behind it.
	format=format + " " + diff;
	//format=diff;

	subselect.snapshotItem(i).innerHTML = format;
}
})();