Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Reddit - Top Comments Preview
- // @namespace http://userscripts.org/scripts/show/126494
- // @author Erik Wannebo (rewritten by gavin19)
- // @description Preview to the top comments on Reddit
- // @include http://www.reddit.com/*
- // @exclude http://www.reddit.com/*/comments/*
- // @version 1.02
- // ==/UserScript==
- (function (nbrComments, comments, commentLinks) {
- var initTCP = {
- init: function () {
- var c;
- document.body.addEventListener('DOMNodeInserted', function (event) {
- if ((event.target.tagName === 'DIV') && (event.target.getAttribute('id') && event.target.getAttribute('id').indexOf('siteTable') !== -1)) {
- initTCP.addTopLinks();
- }
- }, true);
- initTCP.addTopLinks();
- },
- addTopLinks: function () {
- var i, len, link, articleID, obj = this,
- a = document.querySelectorAll('a.comments');
- if (a.length > commentLinks) {
- for (i = 0, len = a.length; i < len; i += 1) {
- if (!a[i].parentNode.querySelector('.toplink') && a[i].textContent.match(/[0-9]/)) {
- articleID = a[i].getAttribute('href');
- articleID = articleID.substring(articleID.indexOf("/comments/") + 10, articleID.indexOf("/comments/") + 15);
- link = document.createElement('a');
- link.className = 'toplink';
- link.href = 'javascript:void(0);';
- link.setAttribute('id', 'toplink' + articleID);
- link.setAttribute('style', 'color:red;text-decoration:none;');
- link.textContent = ' top';
- a[i].parentNode.parentNode.querySelector('.first').insertBefore(link, null);
- (function (link, articleID) {
- link.addEventListener('click', function () {
- obj.retrieveTopComments(this, articleID);
- });
- })(link, articleID);
- }
- }
- commentLinks = a.length;
- }
- },
- retrieveTopComments: function (ele, articleID) {
- var pre, thisPre;
- ele = ele.parentNode.parentNode;
- if (!document.querySelector('#' + articleID + 'preview')) {
- pre = document.createElement('div');
- pre.setAttribute('id', articleID + 'preview');
- 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;');
- pre.textContent = 'loading...';
- ele.querySelector('.first').insertBefore(pre, null);
- ele.style.display = 'inline-block';
- GM_xmlhttpRequest({
- method: "GET",
- url: "http://www.reddit.com/comments/" + articleID + "/.json?limit=" + nbrComments,
- headers: {
- "Content-Type": "application/x-www-form-urlencoded"
- },
- onload: this.onloadJSON
- });
- } else {
- thisPre = document.querySelector("#" + articleID + "preview");
- thisPre.parentNode.removeChild(thisPre);
- ele.style.display = 'inline';
- }
- },
- onloadJSON: function onloadJSON(response) {
- var i, content, ups, downs, author, contentDiv, pct = 100,
- newHtml = '',
- comments = JSON.parse(response.responseText),
- articleID = comments[0].data.children[0].data.id;
- for (i = 0; i < nbrComments; i += 1) {
- if (comments[1].data.children.length >= (i + 1)) {
- content = comments[1].data.children[i].data.body_html;
- if (content !== undefined) {
- contentDiv = document.createElement('div');
- contentDiv.innerHTML = content;
- content = contentDiv.firstChild.textContent;
- author = comments[1].data.children[i].data.author;
- ups = comments[1].data.children[i].data.ups;
- downs = comments[1].data.children[i].data.downs;
- if (downs > 0) {
- pct = Math.floor((ups / (ups + downs)) * 100 + 0.5)
- };
- newHtml += (i > 0 ? "<hr>" : "") + "<b>" + author + "</b> " + pct + "% +" + ups + " -" + downs + "<br>" + content;
- }
- }
- }
- document.querySelector('#' + articleID + 'preview').innerHTML = newHtml;
- }
- };
- if (document.body) {
- setTimeout(function () {
- initTCP.init();
- }, 1000);
- } else {
- window.addEventListener("load", function () {
- initTCP.init();
- }, false);
- };
- })(nbrComments = 2, comments = "", commentLinks = 0);
Add Comment
Please, Sign In to add comment