reddit

Reddit Uppers and Downers Enhanced EX+2 Alpha Turbo Edition (bench)

Jun 30th, 2010
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           Reddit Uppers and Downers Enhanced EX+2 Alpha Turbo Edition
  3. // @namespace      mistercow
  4. // @description    Show up-votes and down-votes next to the total score on reddit comments.
  5. // @include        http://www.reddit.com/*/comments/*
  6. // @include        http://www.reddit.com/user/*
  7. // ==/UserScript==
  8.  
  9. /*jslint strict:false, browser:true, plusplus:false, newcap:false*/
  10. /*global window:false, GM_addStyle:false, GM_xmlhttpRequest:false*/
  11.  
  12. /*
  13. This code is provided as is, with no warranty of any kind.
  14.  
  15. I hacked it together in one night for my own use, and have not tested it extensively.
  16.  
  17. The script can slow down comment page load time; if the lag is noticeable, you may want
  18. to change your preferences to load fewer comments per page.
  19.  
  20. Note that this runs once and does not install any persistent code on the page. So any votes
  21. you cast will not affect the numbers displayed until you reload.
  22.  
  23. Also note that the ups and downs will not always add up to the score displayed on reddit.
  24. I think this is because of caching on reddit's part. It's usually within one or two points though.
  25.  
  26. Code contributors: Allan Bogh - http://www.opencodeproject.com
  27.         brasso - http://userscripts.org/scripts/show/56641
  28.         savetheclocktower - http://gist.github.com/174069
  29.         skeww (jslint, fragment, chunking) - http://kaioa.com
  30. */
  31.  
  32. var loc, jsonURL, voteTable, onloadJSON, displayVotes, processTree, isComment, processChildren, processReplies, chunker, processingTime = 0, idleTime = 0;
  33.  
  34. //Get the URL for the JSON details of this comments page
  35. loc = "" + window.location;
  36. jsonURL = loc + "/.json";
  37. if (loc.indexOf("?") !== -1) {
  38.     jsonURL = loc.replace("?", "/.json?");
  39. }
  40.  
  41. voteTable = {};
  42.  
  43. onloadJSON = function (response) {
  44.     var jsonText = response.responseText, data;
  45.  
  46.     try {
  47.         data = JSON.parse(jsonText);
  48.     } catch (e) {
  49.         if (window.console) {
  50.             window.console.error(e);
  51.         }
  52.     }
  53.  
  54.     //Load the vote table by processing the tree
  55.     processTree(data); //this takes up no time (4ms on 4000 records)
  56.  
  57.     //Display the loaded votes
  58.     displayVotes();
  59. };
  60.  
  61. // spend up to 50msec a time with a task, wait for 25msec and continue if necessary
  62. chunker = function (items, process) {
  63.     var todo = items.concat(), took;
  64.     setTimeout(function () {
  65.         var start = Date.now();
  66.         do {
  67.             process(todo.shift());
  68.             took = Date.now() - start;
  69.         } while (todo.length && took < 50);
  70.         processingTime += took;
  71.         idleTime += 25;
  72.         if (todo.length) {
  73.             setTimeout(arguments.callee, 25);
  74.         } else {
  75.             console.log('processing time: ' + processingTime);
  76.             console.log('idle time: ' + idleTime);
  77.             console.log('total time: ' + (processingTime + idleTime));
  78.         }
  79.     }, 25);
  80. };
  81.  
  82. displayVotes = function () {
  83.     //Add the style sheets for up and down ratings
  84.     GM_addStyle(".moo_ups { color:rgb(255, 139, 36); font-weight:bold; }");
  85.     GM_addStyle(".moo_downs { color:rgb(148,148,255); font-weight:bold; }");
  86.  
  87.     var taglines,
  88.         commentID = null,
  89.         toArray;
  90.  
  91.     toArray = function (col) {
  92.         var a = [], i, len;
  93.         for (i = 0, len = col.length; i < len; i++) {
  94.             a[i] = col[i];
  95.         }
  96.         return a;
  97.     };
  98.  
  99.     taglines = toArray(document.getElementsByClassName("tagline"));
  100.  
  101.     chunker(taglines, function (item) {
  102.         var votes, openparen, mooups, pipe, moodowns, voteDowns, voteUps, closeparen, frag;
  103.         if (item.nextSibling.nodeName === "FORM") { //the first item is the title of the post
  104.             commentID = item.nextSibling.firstChild.value;
  105.             if (voteTable[commentID]) {
  106.                 frag = document.createDocumentFragment(); //using a fragment speeds this up by a factor of about 2
  107.  
  108.                 votes = voteTable[commentID];
  109.  
  110.                 openparen = document.createTextNode(" (");
  111.                 frag.appendChild(openparen);
  112.  
  113.                 mooups = document.createElement("span");
  114.                 mooups.className = "moo_ups";
  115.                 voteUps = document.createTextNode(votes.ups);
  116.  
  117.                 mooups.appendChild(voteUps);
  118.                 frag.appendChild(mooups);
  119.  
  120.                 pipe = document.createTextNode("|");
  121.                 item.appendChild(pipe);
  122.  
  123.                 moodowns = document.createElement("span");
  124.                 moodowns.className = "moo_downs";
  125.  
  126.                 voteDowns = document.createTextNode(votes.downs);
  127.                 moodowns.appendChild(voteDowns);
  128.  
  129.                 frag.appendChild(moodowns);
  130.  
  131.                 closeparen = document.createTextNode(")");
  132.                 frag.appendChild(closeparen);
  133.  
  134.                 frag.appendChild(openparen);
  135.                 frag.appendChild(mooups);
  136.                 frag.appendChild(pipe);
  137.                 frag.appendChild(moodowns);
  138.                 frag.appendChild(closeparen);
  139.  
  140.                 item.appendChild(frag);
  141.             }
  142.         }
  143.     });
  144. };
  145.  
  146. //Recursively process the comment tree
  147. processTree = function (obj) {
  148.     var i, il, data, name;
  149.     if (obj instanceof Array) {
  150.         for (i = 0, il = obj.length; i < il; i++) {
  151.             processTree(obj[i]);
  152.         }
  153.     }
  154.     data = obj.data;
  155.     if (data) { //Data found
  156.         if (isComment(obj) && data.author !== "[deleted]") {
  157.             name = data.name;
  158.             if (name) { //Store the votes in the vote table
  159.                 voteTable[name] = {
  160.                     downs: data.downs || 0,
  161.                     ups: data.ups || 0
  162.                 };
  163.             }
  164.         }
  165.  
  166.         //Process any subtrees
  167.         processChildren(data);
  168.         processReplies(data);
  169.  
  170.     }
  171. };
  172.  
  173. isComment = function (obj) {
  174.     return obj.kind === "t1";
  175. };
  176.  
  177. processChildren = function (data) {
  178.     var children = data.children, i, il;
  179.     if (children) {
  180.         for (i = 0, il = children.length; i < il; i++) {
  181.             processTree(children[i]);
  182.         }
  183.     }
  184. };
  185.  
  186. processReplies = function (data) {
  187.     var replies = data.replies;
  188.     if (replies) {
  189.         processTree(replies);
  190.     }
  191. };
  192.  
  193. //load the JSON
  194. if (jsonURL.indexOf('/comscore-iframe/') === -1) {
  195.     GM_xmlhttpRequest({
  196.         method: "GET",
  197.         url:    jsonURL,
  198.         onload: onloadJSON
  199.     });
  200. }
Add Comment
Please, Sign In to add comment