User:Sid/HistDiff

From RationalWiki
Jump to: navigation, search
HistDiff.png

This is a little script I quickly wrote when I started to lose track of all the censorship on cp:Talk:PNAS Response to Letter. I could see the absolute size of each revision in the history, but that isn't overly intuitive. The Recent Changes with their color-coded messages are much nicer, so I made a script that calculates the diff sizes and displays them just like the RC do.

While I developed it for CP, it works on any wiki (1) that uses the +/- view in the RC and (2) that shows total sizes in the history view.

How to install it (Greasemonkey)[edit]

If you're using Greasemonkey (Firefox extension, grab it from the Mozilla Extensions site), there are two ways you can install it:

  1. Creating a text file and installing from there:
    1. Start a text editor that can save plain text files. On Windows, this would typically be Notepad. Just don't use MS Word or stuff like that.
    2. Copy-paste the code from the box below into a blank document. (If you want to include other wikis, just add more "include" lines, following the pattern there.)
    3. Save that document under the name HistDiff.user.js
    4. Drag and drop that file into the Firefox window (or use "File -> Open File" to navigate to it)
    5. A box will pop up, going all "Install this script?". Just confirm that.
  2. Creating a new script directly from Firefox
    1. In Firefox, right-click on the monkey head (lower right corner) and click on "New user script".
    2. Enter the following:
    3. Hit "Okay". An editor (Notepad, etc.) will open with a quasi-blank script containing the info you entered above, basically.
    4. Replace the entire content of the document with the code from the box below and save the document.

The script[edit]

// ==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;
var sizes= new Array();
tagselect = document.getElementById("bodyContent");
subselect = document.evaluate("./form/ul/li/span[@class='history-size']", tagselect, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 
for (var i = 0; i < subselect.snapshotLength; i++)
{
	currently=subselect.snapshotItem(i).innerHTML;
	if (currently=="(empty)")
	{
		currently=0;
	}
	else
	{
		currently=currently.slice(1,-7);
		currently=currently.replace(/,/,"");
		currently=parseInt(currently);
	}
	sizes[i]=currently;
}

for (var j = 0; j < subselect.snapshotLength-1; j++)
{
	diff=sizes[j]-sizes[j+1];
	currently=subselect.snapshotItem(j).innerHTML;
	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.
	//currently=currently + diff;
	currently=diff;

	subselect.snapshotItem(j).innerHTML = currently;
}
})();

How to install it (Userscript)[edit]

Simply edit your userscript file. It's always located at User:[your name]/[your skin name].js - which isn't terribly intuitive, so here are two examples:

  • RW default skin is "monobook", so my user script location would be User:Sid/monobook.js
  • CP default skin is "conserv" (and my CP user name is "Sid 3050"), so my user script location there would be User:Sid_3050/conserv.js

Do note that the first letter of the skin name is NOT capitalized.

Once you found your location, copy-paste the below box into it, save and do a few full refreshes of History pages to see the effect.

OBVIOUS WARNING[edit]

If you're socking on CP, don't use this script. It originated here on RW, so anybody using it on CP will automatically admit knowledge of The Site That Must Not Be Mentioned. Additionally, CP's completely senseless "Must ask for permission before installing user scripts" rule will pretty much have sysops breathing down your neck anyway.

Advice: Wait until a sysop or parodist installs this as a user script first and gets away with it. Then you can claim that you're just using their script. This should be safe, but we all know that on CP, you can get banned for pretty much anything, so do this at your own risk, obviously.

Hint: The Greasemonkey script above also works when you're not logged in and can't be detected on CP.

The script[edit]

function rw(){
var tagselect, subselect, currently, diff;
var sizes= new Array();
tagselect = document.getElementById("bodyContent");
subselect = document.evaluate("./form/ul/li/span[@class='history-size']", tagselect, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 
for (var i = 0; i < subselect.snapshotLength; i++)
{
	currently=subselect.snapshotItem(i).innerHTML;
	if (currently=="(empty)")
	{
		currently=0;
	}
	else
	{
		currently=currently.slice(1,-7);
		currently=currently.replace(/,/,"");
		currently=parseInt(currently);
	}
	sizes[i]=currently;
}

for (var j = 0; j < subselect.snapshotLength-1; j++)
{
	diff=sizes[j]-sizes[j+1];
	currently=subselect.snapshotItem(j).innerHTML;
	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.
	//currently=currently + diff;
	currently=diff;

	subselect.snapshotItem(j).innerHTML = currently;
}
}

addOnloadHook(rw);

Known bugs[edit]

So far, I only know of one bug: If an article has more edits than fit onto one page (usually, this means "more than 50 edits"), the lowest diff will still have the absolute number (even though it's not really the first edit).

Something that isn't a bug is the fact that this script will not work for diffs that predate the time the byte size was introduced. Check this History page to see what I mean - the edits before JM's Sep 14 edit don't have any size listed, so there won't be a diff size for those edits, either.