Advertisement
Guest User

Reddit - Top Comments Preview [ Lite-mod ]

a guest
Mar 1st, 2015
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 8.35 KB | None | 0 0
  1. // ==UserScript==
  2. // @name           Reddit - Top Comments Preview
  3. // @namespace      https://greasyfork.org/users/5174-jesuis-parapluie
  4. // @author         Erik Wannebo, gavin19, jesuis-unparapluie
  5. // @description    Preview to the top comments on Reddit
  6. // @include        http://*.reddit.com/*
  7. // @exclude        http://*.reddit.com/*/comments/*
  8. // @include        https://*.reddit.com/*
  9. // @exclude        https://*.reddit.com/*/comments/*
  10. // @grant          none
  11. // @version        1.62
  12. // ==/UserScript==
  13. (function () {
  14.     'use strict';
  15.     var topCP = {
  16.         opts: {
  17.             /* Number of comments to display. Default is 3 */
  18.             topComments: 3,
  19.             /* click or hover. Do you want to hover the link to trigger it, or click? */
  20.             eventType: 'click',
  21.             /* set keyboard shortcut 't' to show/hide active top comments (only with RES) */
  22.             setShortCut: true,
  23.             /* auto expand on siteload (for RES) */
  24.             setAutoExpand: false
  25.         },
  26.         addTopLinks: function () {
  27.             var i, len, link, articleID, tmp,
  28.                 a = document.querySelectorAll('.linklisting .comments:not(.empty)');
  29.             if (a.length) {
  30.                 for (i = 0, len = a.length; i < len; i += 1) {
  31.                     if (!a[i].parentNode.querySelector('.toplink') && /[0-9]/.test(a[i])) {
  32.                         articleID = a[i].getAttribute('href');
  33.                         articleID = articleID.substring(articleID.indexOf('/comments/') + 10, articleID.indexOf('/comments/') + 16);
  34.                         link = document.createElement('a');
  35.                         link.className = 'toplink';
  36.                         tmp = "java";
  37.                         link.href = tmp + 'script:;';
  38.                         link.setAttribute('id', 'toplink' + articleID);
  39.                         link.setAttribute('style', 'color:grey;text-decoration:none;');
  40.                         link.textContent = ' top';
  41.                         a[i].parentNode.parentNode.querySelector('.first').insertBefore(link, null);
  42.                         topCP.addListener(link, articleID);
  43.                     }
  44.                 }
  45.             }
  46.         },
  47.         addListener: function (link, id) {
  48.             link.addEventListener(topCP.opts.eventType, function () {
  49.                 topCP.retrieveTopComments(this, id);
  50.             });
  51.         },
  52.         retrieveTopComments: function (ele, articleID) {
  53.             var pre, url, xhr, thisPre;
  54.             topCP.kill_preview = function () { this.parentNode.removeChild(this); }
  55.             ele = ele.parentNode.parentNode;
  56.             if (!document.querySelector('#preview' + articleID)) {
  57.                 pre = document.createElement('div');
  58.                 pre.setAttribute('id', 'preview' + articleID);
  59.                 pre.classList.add('loading');
  60.                 pre.addEventListener('click', topCP.kill_preview);
  61.                 ele.querySelector('.first').insertBefore(pre, null);
  62.                
  63.                 url =  window.location.href.split("/")[0]+'//www.reddit.com/comments/' + articleID + '/.json?limit=' + (topCP.opts.topComments + 5) + '&sort=top';
  64.                 xhr = new XMLHttpRequest();
  65.                 xhr.open('GET', url, true);
  66.                 xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  67.                 xhr.onreadystatechange = function () {
  68.                     if (xhr.readyState === 4 && xhr.status === 200) {
  69.                         topCP.onloadJSON(xhr);
  70.                     }
  71.                 };
  72.                 xhr.send(null);
  73.             } else {
  74.                 thisPre = document.querySelector('#preview' + articleID);
  75.                 thisPre.parentNode.parentNode.style.marginBottom = '';
  76.                 thisPre.parentNode.removeChild(thisPre);
  77.             }
  78.         },
  79.         onloadJSON: function (response) {
  80.             var i, len, content, ups, downs, contentDiv, article, author, permalink,
  81.                 newHTML = '',
  82.                 comments = JSON.parse(response.responseText),
  83.                 commentsLength = comments[1].data.children.length,
  84.                 articleID = comments[0].data.children[0].data.id,
  85.                 threadLink = comments[0].data.children[0].data.permalink;
  86.             len = topCP.opts.topComments < commentsLength ? topCP.opts.topComments : commentsLength;
  87.             for (i = 0; i < len; i += 1) {
  88.                 content = comments[1].data.children[i].data.body_html;
  89.                 if (content) {
  90.                     contentDiv = document.createElement('div');
  91.                     contentDiv.innerHTML = content;
  92.                     content = contentDiv.firstChild.textContent;
  93.                     author = comments[1].data.children[i].data.author;
  94.                     ups = comments[1].data.children[i].data.ups;
  95.                     permalink = threadLink + comments[1].data.children[i].data.id;
  96.                     newHTML += (i > 0 ? '<hr noshade color=#dddddd>' : '');
  97.                     newHTML += '<a class="authorLink" target="_blank" href="/u/' + author;
  98.                     newHTML += '">' + author + '</a>&nbsp;&nbsp;';
  99.                     newHTML += '(+' + ups + ')';
  100.                     newHTML += '<a href="' + permalink + '" target="_blank" class="perma">permalink</a>';
  101.                     newHTML += '<br />' + content;
  102.                 }
  103.             }
  104.             article = document.querySelector('#preview' + articleID);
  105.             article.classList.remove('loading');
  106.             article.innerHTML = newHTML;
  107.             article.parentNode.parentNode.style.marginBottom = (article.offsetHeight + 5) + 'px';
  108.             article.removeEventListener('click', topCP.kill_preview);
  109.         },
  110.         addStyle: function () {
  111.             var style,
  112.                 sheet = '';
  113.             sheet += "div[id^=preview]{color:#f4f4f4;box-sizing:border-box;-moz-box-sizing:border-box;background:rgba(247,247,248,.9);border-radius:5px;white-space:normal;padding:5px;position:absolute;margin-top: 2px;}";
  114.             sheet += ".loading:before{content:\"Loading...\";}div[id^=preview] .md{background:rgba(254,255,237,.9);box-sizing:border-box;-moz-box-sizing:border-box;margin:3px;padding:2px 8px;}";
  115.             sheet += "div[id^=preview] .md a{font-weight:bold;text-decoration:underline;color:#4D9AE7!important;}";
  116.             sheet += "div[id^=preview] .md{background-color:#ffffff!important;}";
  117.             sheet += "div[id^=preview] .md p{font-size: 115%;color:#000000;}";
  118.             sheet += "div[id^=preview] .authorLink{color:#3366aa!important;}";
  119.             sheet += ".listing-page .linklisting .buttons li { vertical-align: top; }";
  120.             sheet += ".perma {float:right;color:#888!important;}";
  121.             style = document.createElement('style');
  122.             style.type = 'text/css';
  123.             style.textContent = sheet;
  124.             document.querySelector('head').appendChild(style);
  125.         },
  126.         init: function () {
  127.             document.body.addEventListener('DOMNodeInserted', function (e) {
  128.                 if ((e.target.tagName === 'DIV') && (e.target.getAttribute('id') && e.target.getAttribute('id').indexOf('siteTable') !== -1)) {
  129.                     topCP.addTopLinks();
  130.                 }
  131.             }, true);
  132.             document.body.addEventListener('DOMNodeInserted', function (e) {
  133.                 if ((e.target.tagName === 'DIV') && (e.target.getAttribute('id') && e.target.getAttribute('id').indexOf('siteTable') !== -1)) {
  134.                     topCP.addTopLinks();
  135.                 }
  136.             }, true);
  137.             if (topCP.opts.setShortCut)
  138.             {
  139.                 window.addEventListener("keyup", function(e) {
  140.                  //t: keycode 84
  141.                  if(e.keyCode === 84 && document.querySelector('.RES-keyNav-activeElement')) {
  142.                     document.querySelector('.RES-keyNav-activeElement .toplink').click();
  143.                   }
  144.                 });
  145.             }
  146.             if (topCP.opts.setAutoExpand)
  147.             {
  148.                 document.querySelector('#viewImagesButton').click();
  149.             }
  150.             topCP.addStyle();
  151.             topCP.addTopLinks();
  152.         }
  153.     };
  154.     if (document.body) {
  155.         setTimeout(function () {
  156.             topCP.init();
  157.         }, 300);
  158.     } else {
  159.         window.addEventListener('load', function () {
  160.             topCP.init();
  161.         }, false);
  162.     }
  163. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement