Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Reddit Uppers and Downers Enhanced EX+2 Alpha Turbo Edition
- // @namespace mistercow
- // @description Show up-votes and down-votes next to the total score on reddit comments.
- // @include http://www.reddit.com/*/comments/*
- // @include http://www.reddit.com/user/*
- // ==/UserScript==
- /*jslint strict:false, browser:true, plusplus:false, newcap:false*/
- /*global window:false, GM_addStyle:false, GM_xmlhttpRequest:false*/
- /*
- 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
- skeww (jslint, fragment, chunking) - http://kaioa.com
- */
- var loc, jsonURL, voteTable, onloadJSON, displayVotes, processTree, isComment, processChildren, processReplies, chunker, processingTime = 0, idleTime = 0;
- //Get the URL for the JSON details of this comments page
- loc = "" + window.location;
- jsonURL = loc + "/.json";
- if (loc.indexOf("?") !== -1) {
- jsonURL = loc.replace("?", "/.json?");
- }
- voteTable = {};
- onloadJSON = function (response) {
- var jsonText = response.responseText, data;
- try {
- data = JSON.parse(jsonText);
- } catch (e) {
- if (window.console) {
- window.console.error(e);
- }
- }
- //Load the vote table by processing the tree
- processTree(data); //this takes up no time (4ms on 4000 records)
- //Display the loaded votes
- displayVotes();
- };
- // spend up to 50msec a time with a task, wait for 25msec and continue if necessary
- chunker = function (items, process) {
- var todo = items.concat(), took;
- setTimeout(function () {
- var start = Date.now();
- do {
- process(todo.shift());
- took = Date.now() - start;
- } while (todo.length && took < 50);
- processingTime += took;
- idleTime += 25;
- if (todo.length) {
- setTimeout(arguments.callee, 25);
- } else {
- console.log('processing time: ' + processingTime);
- console.log('idle time: ' + idleTime);
- console.log('total time: ' + (processingTime + idleTime));
- }
- }, 25);
- };
- displayVotes = function () {
- //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,
- commentID = null,
- toArray;
- toArray = function (col) {
- var a = [], i, len;
- for (i = 0, len = col.length; i < len; i++) {
- a[i] = col[i];
- }
- return a;
- };
- taglines = toArray(document.getElementsByClassName("tagline"));
- chunker(taglines, function (item) {
- var votes, openparen, mooups, pipe, moodowns, voteDowns, voteUps, closeparen, frag;
- if (item.nextSibling.nodeName === "FORM") { //the first item is the title of the post
- commentID = item.nextSibling.firstChild.value;
- if (voteTable[commentID]) {
- frag = document.createDocumentFragment(); //using a fragment speeds this up by a factor of about 2
- votes = voteTable[commentID];
- openparen = document.createTextNode(" (");
- frag.appendChild(openparen);
- mooups = document.createElement("span");
- mooups.className = "moo_ups";
- voteUps = document.createTextNode(votes.ups);
- mooups.appendChild(voteUps);
- frag.appendChild(mooups);
- pipe = document.createTextNode("|");
- item.appendChild(pipe);
- moodowns = document.createElement("span");
- moodowns.className = "moo_downs";
- voteDowns = document.createTextNode(votes.downs);
- moodowns.appendChild(voteDowns);
- frag.appendChild(moodowns);
- closeparen = document.createTextNode(")");
- frag.appendChild(closeparen);
- frag.appendChild(openparen);
- frag.appendChild(mooups);
- frag.appendChild(pipe);
- frag.appendChild(moodowns);
- frag.appendChild(closeparen);
- item.appendChild(frag);
- }
- }
- });
- };
- //Recursively process the comment tree
- processTree = function (obj) {
- var i, il, data, name;
- if (obj instanceof Array) {
- for (i = 0, il = obj.length; i < il; i++) {
- processTree(obj[i]);
- }
- }
- data = obj.data;
- if (data) { //Data found
- if (isComment(obj) && data.author !== "[deleted]") {
- 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);
- }
- };
- isComment = function (obj) {
- return obj.kind === "t1";
- };
- processChildren = function (data) {
- var children = data.children, i, il;
- if (children) {
- for (i = 0, il = children.length; i < il; i++) {
- processTree(children[i]);
- }
- }
- };
- processReplies = function (data) {
- var replies = data.replies;
- if (replies) {
- processTree(replies);
- }
- };
- //load the JSON
- if (jsonURL.indexOf('/comscore-iframe/') === -1) {
- GM_xmlhttpRequest({
- method: "GET",
- url: jsonURL,
- onload: onloadJSON
- });
- }
Add Comment
Please, Sign In to add comment