gavin19

Reddit - Top Comments Preview

Feb 23rd, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           Reddit - Top Comments Preview
  3. // @namespace      http://userscripts.org/scripts/show/126494
  4. // @author         Erik Wannebo (rewritten by gavin19)
  5. // @description    Preview to the top comments on Reddit
  6. // @include        http://www.reddit.com/*
  7. // @exclude        http://www.reddit.com/*/comments/*
  8. // @version        1.02
  9. // ==/UserScript==
  10. (function (nbrComments, comments, commentLinks) {
  11.     var initTCP = {
  12.         init: function () {
  13.             var c;
  14.             document.body.addEventListener('DOMNodeInserted', function (event) {
  15.                 if ((event.target.tagName === 'DIV') && (event.target.getAttribute('id') && event.target.getAttribute('id').indexOf('siteTable') !== -1)) {
  16.                         initTCP.addTopLinks();
  17.                 }
  18.             }, true);
  19.             initTCP.addTopLinks();
  20.         },
  21.         addTopLinks: function () {
  22.             var i, len, link, articleID, obj = this,
  23.             a = document.querySelectorAll('a.comments');
  24.             if (a.length > commentLinks) {
  25.                 for (i = 0, len = a.length; i < len; i += 1) {
  26.                     if (!a[i].parentNode.querySelector('.toplink') && a[i].textContent.match(/[0-9]/)) {
  27.                         articleID = a[i].getAttribute('href');
  28.                         articleID = articleID.substring(articleID.indexOf("/comments/") + 10, articleID.indexOf("/comments/") + 15);
  29.                         link = document.createElement('a');
  30.                         link.className = 'toplink';
  31.                         link.href = 'javascript:void(0);';
  32.                         link.setAttribute('id', 'toplink' + articleID);
  33.                         link.setAttribute('style', 'color:red;text-decoration:none;');
  34.                         link.textContent = '  top';
  35.                         a[i].parentNode.parentNode.querySelector('.first').insertBefore(link, null);
  36.                         (function (link, articleID) {
  37.                             link.addEventListener('click', function () {
  38.                                 obj.retrieveTopComments(this, articleID);
  39.                             });
  40.                         })(link, articleID);
  41.                     }
  42.                 }
  43.                 commentLinks = a.length;
  44.             }
  45.         },
  46.         retrieveTopComments: function (ele, articleID) {
  47.             var pre, thisPre;
  48.             ele = ele.parentNode.parentNode;
  49.             if (!document.querySelector('#' + articleID + 'preview')) {
  50.                 pre = document.createElement('div');
  51.                 pre.setAttribute('id', articleID + 'preview');
  52.                 pre.setAttribute('style', 'font-size:11px;width:100%;max-width:100%;background-color:#f8f899;border-radius:5px;border:solid black 1px;white-space:pre-line;padding:5px;');
  53.                 pre.textContent = 'loading...';
  54.                 ele.querySelector('.first').insertBefore(pre, null);
  55.                 ele.style.display = 'inline-block';
  56.                 GM_xmlhttpRequest({
  57.                     method: "GET",
  58.                     url: "http://www.reddit.com/comments/" + articleID + "/.json?limit=" + nbrComments,
  59.                     headers: {
  60.                         "Content-Type": "application/x-www-form-urlencoded"
  61.                     },
  62.                     onload: this.onloadJSON
  63.                 });
  64.             } else {
  65.                 thisPre = document.querySelector("#" + articleID + "preview");
  66.                 thisPre.parentNode.removeChild(thisPre);
  67.                 ele.style.display = 'inline';
  68.             }
  69.         },
  70.         onloadJSON: function onloadJSON(response) {
  71.             var i, content, ups, downs, author, contentDiv, pct = 100,
  72.                 newHtml = '',
  73.                 comments = JSON.parse(response.responseText),
  74.                 articleID = comments[0].data.children[0].data.id;
  75.             for (i = 0; i < nbrComments; i += 1) {
  76.                 if (comments[1].data.children.length >= (i + 1)) {
  77.                     content = comments[1].data.children[i].data.body_html;
  78.                     if (content !== undefined) {
  79.                         contentDiv = document.createElement('div');
  80.                         contentDiv.innerHTML = content;
  81.                         content = contentDiv.firstChild.textContent;
  82.                         author = comments[1].data.children[i].data.author;
  83.                         ups = comments[1].data.children[i].data.ups;
  84.                         downs = comments[1].data.children[i].data.downs;
  85.                         if (downs > 0) {
  86.                             pct = Math.floor((ups / (ups + downs)) * 100 + 0.5)
  87.                         };
  88.                         newHtml += (i > 0 ? "<hr>" : "") + "<b>" + author + "</b> " + pct + "% +" + ups + " -" + downs + "<br>" + content;
  89.                     }
  90.                 }
  91.             }
  92.             document.querySelector('#' + articleID + 'preview').innerHTML = newHtml;
  93.         }
  94.     };
  95.     if (document.body) {
  96.         setTimeout(function () {
  97.             initTCP.init();
  98.         }, 1000);
  99.     } else {
  100.         window.addEventListener("load", function () {
  101.             initTCP.init();
  102.         }, false);
  103.     };
  104. })(nbrComments = 2, comments = "", commentLinks = 0);
Add Comment
Please, Sign In to add comment