Reddit Uppers and Downers Enhanced

By brasso Last update May 22, 2011 — Installed 37,841 times.

Script Update

in
Subscribe to Script Update 1 post, 1 voice

Allan Bogh Scriptwright
FirefoxWindows

The script has been updated below. Average processing time on 550+ comments - 847ms (originally 6800ms). Works on submitted comments page and deleted comments. Please edit the script with this version:

// ==UserScript==
// @name           Reddit Uppers and Downers
// @namespace      mistercow
// @description    Show up-votes and down-votes next to the total score on reddit comments.
// @include        http://*.reddit.com/*/comments/*
// @include        http://reddit.com/*/comments/*
// ==/UserScript==

/*
This code is provided as is, with no warranty of any kind.

I hacked it together in one night for my own use, and have not tested it extensively.

The script can slow down comment page load time; if the lag is noticeable, you may want
to change your preferences to load fewer comments per page.

Note that this runs once and does not install any persistent code on the page. So any votes
you cast will not affect the numbers displayed until you reload.

Also note that the ups and downs will not always add up to the score displayed on reddit.
I think this is because of caching on reddit's part. It's usually within one or two points though.

Code contributors: Allan Bogh - http://www.opencodeproject.com 
		brasso - http://userscripts.org/scripts/show/56641
		savetheclocktower - http://gist.github.com/174069
*/

//Get the URL for the JSON details of this comments page
var loc = ""+location;
var jsonURL = loc + "/.json";
if(loc.indexOf("?") != -1) {
	jsonURL = loc.replace("?","/.json?");
}


var voteTable = new Object();

//load the JSON
GM_xmlhttpRequest({
	method:	"GET",
	url:	jsonURL,
	onload:	onloadJSON
});

function onloadJSON(response) {
	var jsonText = response.responseText,data;
	//Parse the json text
	// Use native JSON (if it's available) because it's much faster.
	// code by savetheclocktower - http://gist.github.com/174069
    	if (window.JSON && JSON.parse) {
		data = JSON.parse(jsonText);
	}else{
		data = eval("("+jsonText+")");
	}
	//Load the vote table by processing the tree
	processTree(data); //this takes up no time (4ms on 4000 records)

	//Display the loaded votes
//var date1 = new Date();
//var milliseconds1 = date1.getTime();
	displayVotes();
//var date2 = new Date();
//var milliseconds2 = date2.getTime();

//var difference = milliseconds2 - milliseconds1;
//alert(difference+"ms - ");
}

function displayVotes(){
	//Add the style sheets for up and down ratings
	GM_addStyle(".moo_ups { color:rgb(255, 139, 36); font-weight:bold; }");
	GM_addStyle(".moo_downs { color:rgb(148,148,255); font-weight:bold; }");
	
	var taglines = document.getElementsByClassName("tagline");

	var commentID = null;
	for(i=0, il=taglines.length; i < il; i++){
		if(taglines[i].nextSibling.nodeName === "FORM"){ //the first item is the title of the post
			commentID = taglines[i].nextSibling.firstChild.value;
			if(voteTable[commentID]){
				votes = voteTable[commentID];
				
				var parent = taglines[i];
				
				var openparen = document.createTextNode(" (");
				parent.appendChild(openparen);
				var mooups = document.createElement("span");
				mooups.className = "moo_ups";
				var voteUps = document.createTextNode(votes.ups);
				mooups.appendChild(voteUps);
				parent.appendChild(mooups);
				var pipe = document.createTextNode("|");
				parent.appendChild(pipe);
				var moodowns = document.createElement("span");
				moodowns.className = "moo_downs";
				var voteDowns = document.createTextNode(votes.downs);
				moodowns.appendChild(voteDowns);
				parent.appendChild(moodowns);
				var closeparen = document.createTextNode(")");
				parent.appendChild(closeparen);
				
				
				parent.appendChild(openparen);
				parent.appendChild(mooups);
				parent.appendChild(pipe);
				parent.appendChild(moodowns);
				parent.appendChild(closeparen);
			}
		}
	}
}

//Recursively process the comment tree
function processTree(obj) {
	if(obj instanceof Array) {
		for(var i=0, il=obj.length; i < il; i++) { 
			processTree(obj[i]);
		}
	}
	var data = obj.data;
	if(data) { //Data found
		if(isComment(obj) && data.author !== "[deleted]") {
			var name = data.name;
			if(name) { //Store the votes in the vote table
				voteTable[name] = {
					downs:data.downs || 0, 
					ups:data.ups || 0
				};
			}
		}
		
		//Process any subtrees
		processChildren(data);
		processReplies(data);
		
	}
}

function isComment(obj) {
	return obj.kind === "t1";
}

function processChildren(data) {
	var children = data["children"];
	if(children) {
		for(var i=0, il=children.length; i < il; i++){		
			processTree(children[i]);
		}
	}
}

function processReplies(data) {
	var replies = data["replies"];
	if(replies) processTree(replies);
}

Cross
Presentational HTML allowed.
Use <code> for inline code and <pre> for code blocks. Use &lt; and &gt; for literal < and >.
We help break paragraphs and link your links.
or cancel