Reddit Reveal

Reveal hidden information on reddit

当前为 2014-05-22 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name          Reddit Reveal
// @author        buddydvd
// @description   Reveal hidden information on reddit
// @namespace      Reddit Revealer
// @include        http://www.reddit.com/*
// @include        http://www.baconbuzz.com/*
// @include        http://reddit.destructoid.com/*
// @include        http://www.thecutelist.com/*
// @include        http://reddit.independent.co.uk/*
// @include        http://www.redditgadgetguide.com/*
// @include        http://www.weheartgossip.com/*
// @include        http://www.idealistnews.com/*
// @include        https://www.reddit.com/*
// @include        https://www.baconbuzz.com/*
// @include        https://reddit.destructoid.com/*
// @include        https://www.thecutelist.com/*
// @include        https://reddit.independent.co.uk/*
// @include        https://www.redditgadgetguide.com/*
// @include        https://www.weheartgossip.com/*
// @include        https://www.idealistnews.com/*
// @version        1.0
// ==/UserScript==

function reveal() {
  var l = window.document.location,
    h = l.href,
    q = l.search,
    u = h.substring(0, h.length - q.length) + ".json" + q;
  $.getJSON(u, function (json_data) {

    var dom_data = (function () {
      var result = {},
        key;
      $.each($(".thing"), function () {
        var thingDom = this;
        if ($(this).css("display") === "none") {
          return;
        }
        $.each(this.className.split(" "), function () {
          if (this.indexOf("id-") === 0) {
            var thing_type_name = this.substr(3).split("_");
            if (typeof(result[thing_type_name[0]]) === "undefined") {
              result[thing_type_name[0]] = [];
            }
            result[thing_type_name[0]].push({
              name: thing_type_name[1],
              domObj: thingDom
            });
          }
        });
      });
      return result;
    })();

    var json_result = {};

    function gather_data(json_data) {
      var crawl = function (obj) {
        if (obj && obj.length) {
          $.each(obj, function () {
            crawl(this);
          });
        } else if (typeof(obj) === "object") {
          var kind = obj.kind;
          var data = obj.data;
          if (kind) {
            if (kind === "Listing") {
              crawl(data.children);
            } else if (data) {
              if (typeof(json_result[kind]) === "undefined") {
                json_result[kind] = {};
              }
              json_result[kind][data.id] = data;
              if (kind === "t1" && data.replies) {
                crawl(data.replies);
              }
            }
          }
        }
      };
      crawl(json_data);
    }

    gather_data(json_data);

    var enable_source = function (entry, text) {
      if (entry.find(".usertext-edit").size() > 0 || entry.find(".usertext-body").size() === 0) {
        return;
      }
      entry.find(".usertext-body").after("<div class=\"usertext-edit\" style=\"display:none;\"><div><textarea rows=\"1\" cols=\"1\" name=\"text\">" + text + "</textarea></div><div class=\"bottom-area\"><div class=\"usertext-buttons\"><button type=\"button\" class=\"cancel\" onclick=\"cancel_usertext(this)\">hide</button></div></div></div>");
      entry.find(".flat-list.buttons").append("<li><a class=\"edit-usertext\" href=\"javascript:void(0)\" onclick=\"return edit_usertext(this)\">source</a></li>");
    };

    var get_upvote_downvote_html = function (data) {
      var rv = " (";
      rv += "<span style=\"color:orangeRed\">+" + data.ups + "</span>/";
      rv += "<span style=\"color:#5F99CF\">-" + data.downs + "</span>";
      rv += ") ";
      return rv;
    };

    var update_ui = function () {
      if (dom_data.t1) {
        $.each(dom_data.t1, function () {
          var id = this.name;
          var domObj = this.domObj;
          var data = json_result.t1[id];
          if (!data) {
            return;
          }
          var entry = $(domObj).children(".entry");
          entry.find(".score.likes").after(get_upvote_downvote_html(data));
          enable_source(entry, data.body);
        });
      }

      var update_t3_ui = function (domObj, data) {
        var thing = $(domObj);
        var entry = thing.children(".entry");
        var midcol = thing.children(".midcol");
        var score = data.score;

        // reveal up/down vote score
        entry.find(".reddit-comment-link, .tagline").prepend(get_upvote_downvote_html(data));

        // reveal mark down source
        enable_source(entry, data.selftext);

        // reveal vote score
        if (midcol.hasClass("likes")) {
          score--;
        }
        if (midcol.hasClass("dislikes")) {
          score++;
        }
        midcol.find(".score.likes").text(score + 1);
        midcol.find(".score.unvoted").text(Math.max(0, score)); // min score is 0
        midcol.find(".score.dislikes").text(Math.max(0, score - 1)); // min score is 0
      };

      if (dom_data.t3) {
        $.each(dom_data.t3, function () {
          var name = this.name;
          var domObj = this.domObj;
          var data = json_result.t3[name];
          if (!data) {
            $.getJSON("/by_id/t3_" + name + ".json", function (json_data) {
              update_t3_ui(domObj, json_data.data.children[0].data);
            });
            return;
          } else {
            update_t3_ui(domObj, data);
          }
        });
      }
    };
    update_ui();
  });

  $(".button").on("click", function(ev) {
  	  console.log(this.id);
      if (this.id.indexOf("more_t1_") === 0) {
        var id = this.id.split("_")[2];
        console.log(id);
      }
  });

  $(".author").on("mouseover", function (ev) {
  	  var username = $(this).text();
      var rev = $(this).next(".revelation");
      if (rev.size() === 0) {
        var tip = $("<span class=\"revelation\" style=\"position:relative;\"/>");
        $(this).after(tip);
        $.getJSON("/user/" + username + "/about.json", function (a) {
          tip.html(" (<span style=\"color:#B40404\"><b>" + a.data.link_karma + "</b> link karma</span>," + " <span style=\"color:#04B404\"><b>" + a.data.comment_karma + "</b> comment karma</span>, " + " <span style=\"color:#0404B4\"><b>" + parseInt((((new Date()).getTime() / 1000) - a.data.created_utc) / 86400, 10) + "</b> days</span>) ");
        });
      } else {
        rev.css("display", "");
      }
  }).on("mouseout", function (ev) {
      $(this).next(".revelation").css("display", "none");
  });
  
  }
  
  
(function () {
        var s = document.createElement('script');
        s.textContent = "(" + reveal.toString() + ')();';
        document.head.appendChild(s);
})();