Advertisement
Guest User

HabrTree_201420520

a guest
May 20th, 2014
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           HabrTree
  3. // @namespace      dotneter
  4. // @grant none
  5. // @include http://habrahabr.ru/post/*
  6. // @include http://habrahabr.ru/company/*/blog/*
  7. // ==/UserScript==
  8.  
  9. var scoreLeftSide = false;
  10. var autoSort = false;
  11. var green = "#339900";
  12. var red = "#CC0000";
  13. var neutral = "#339900";
  14. var hightlight = "LightGreen";
  15.  
  16. function letsJQuery() {
  17.     (function($){
  18.  
  19.  
  20.         $(function(){
  21.             $("b.spoiler_title").addClass("clickable");
  22.             if(scoreLeftSide){
  23.                 var comments = $("#comments");
  24.                 comments.css({overflow:'visible'});
  25.                 $("#comments .mark").css({position:"absolute", left:-40});
  26.             }
  27.  
  28.  
  29.  
  30.  
  31.  
  32.             function tag(name, attrs){
  33.                 return $("<" + name + ">", attrs);
  34.             }
  35.  
  36.             function getIntFromText(text){
  37.                 if(!text) return 0;
  38.                 var number = /([+-]?\d+)/.exec(text.replace("–","-"))[1];
  39.                 return parseInt(number,10);
  40.             }
  41.  
  42.             function getIntRating(span){
  43.                 var rating = span.html();
  44.                 return getIntFromText(rating);
  45.             }
  46.  
  47.             function desc(a,b) { return b - a; }
  48.  
  49.             function hightlightRating(i){
  50.                 $("a.open-comment").remove();
  51.                 $("div.comment_item").each(function(){
  52.                     var comment = $(this);
  53.                     var message = $('div.message:first',comment);
  54.                     message.css("border-top", "");
  55.                     var rating = getIntRating($(scoreSelector+':first',comment));
  56.                     if(rating >= i){
  57.                         message.show();
  58.                         var color = hightlight;
  59.                         message.css("border-top", "5px solid " + color);
  60.                         message.parents("div.comment_item.comment-close").each(function(){
  61.                             showMessage($(this));
  62.                         });
  63.                     }else{
  64.                         comment.addClass("comment-close");
  65.                         message.hide();
  66.                         $("div.reply_comments:first",comment).hide();
  67.                         addOpenLink(comment);
  68.  
  69.                     }
  70.                 });
  71.             }
  72.  
  73.             function showMessage(comment){
  74.                 comment.removeClass("comment-close");
  75.                 $("a.open-comment:first", comment).remove();
  76.                 $("div.message:first", comment).show();
  77.                 $("div.reply_comments:first", comment).show();
  78.             }
  79.  
  80.             function addOpenLink(comment){
  81.                 var div = $("div.reply:first", comment);
  82.                 var open = tag("a", {"class": "reply open-comment", href:"#", text:"раскрыть"});
  83.                 open.click(function(){
  84.                     showMessage(comment);
  85.                     return false;
  86.                 });
  87.                 div.append(open);
  88.             }
  89.  
  90.             function wilsonScore(up, down) {
  91.                     if (!up) return 0;
  92.                     var n = up + down;
  93.                     var z = 1.64485; //1.0 = 85%, 1.6 = 95%
  94.                     var phat = up / n;
  95.                     return (phat+z*z/(2*n)-z*Math.sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n);
  96.             }
  97.  
  98.  
  99.             var scoreSelector = "span.score";
  100.  
  101.             var allRatings = {};
  102.  
  103.             (function initRatings(){
  104.                 $("#comments div[id^='voting_']").each(function(){
  105.                     var id = getIntFromText($(this).attr("id"));
  106.                     var scoreSpan = $(scoreSelector,$(this));
  107.                     var habrRating = getIntRating(scoreSpan);
  108.                     var upDown = scoreSpan.attr('title').split(':')[1].split('и');
  109.                     var up = getIntFromText(upDown[0]);
  110.                     var down = getIntFromText(upDown[1]);
  111.                     var wilsonRating = parseInt(wilsonScore(up,down)*100,10);
  112.                     scoreSpan.append(tag("span",{'style':"color:yellow;",'text':" " + wilsonRating}));
  113.                     allRatings[id] = [habrRating,wilsonRating];
  114.                 });
  115.             })();
  116.  
  117.             function appendRatings(ratings){
  118.  
  119.                 var ratingsDiv = tag('div');
  120.                 var ratingsSorted = $.map(ratings, function(i,k){return parseInt(k,10);}).sort(desc);
  121.                 if(!ratingsSorted.length)
  122.                     return;
  123.                 $.each(ratingsSorted, function(k,i){
  124.                     var anchor = tag('a', {'class': 'rating-link', href:"#", text:i}).css("color", getRatingColor(i));
  125.                     anchor.click(function(){
  126.                         hightlightRating(parseInt(anchor.html(),10));
  127.                         return false;
  128.                     });
  129.                     ratingsDiv.append(anchor);
  130.                     if(ratings[i] > 1) ratingsDiv.append("(" + ratings[i] + ") ");
  131.                     else  ratingsDiv.append(" ");
  132.                 });
  133.  
  134.                 $("#comments").prepend(ratingsDiv);
  135.             }
  136.  
  137.             function showRatings(){
  138.                 var habrRatings = $.map(allRatings, function(x){return x[0];});
  139.                 var wilsonRatings = $.map(allRatings, function(x){return x[1];});
  140.                 function groupByRating(list){
  141.                     var ratings = {};
  142.                     $(list).each(function(i,v){
  143.                         var intRating = v;
  144.                         if(!ratings[intRating]) ratings[intRating] = 0;
  145.                         ratings[intRating] = ratings[intRating] + 1;       
  146.                     });
  147.                     return ratings;
  148.                 }
  149.                 appendRatings(groupByRating(wilsonRatings));
  150.                 appendRatings(groupByRating(habrRatings));
  151.  
  152.             }
  153.  
  154.             showRatings();
  155.  
  156.             function getRatingColor(rating){
  157.                 if(rating < 0)
  158.                     return red;
  159.                 if(rating === 0)
  160.                     return neutral;
  161.                 return green;
  162.             }
  163.             $.fn.sortElements = (function(){
  164.  
  165.                 var sort = [].sort;
  166.  
  167.                 return function(comparator, getSortable) {
  168.  
  169.                     getSortable = getSortable || function(){return this;};
  170.  
  171.                     var placements = this.map(function(){
  172.  
  173.                         var sortElement = getSortable.call(this),
  174.                         parentNode = sortElement.parentNode,
  175.  
  176.                         nextSibling = parentNode.insertBefore(
  177.                             document.createTextNode(''),
  178.                             sortElement.nextSibling);
  179.  
  180.                             return function() {
  181.  
  182.                                 if (parentNode === this) {
  183.                                     throw new Error(
  184.                                         "You can't sort elements if any one is a descendant of another.");
  185.                                 }
  186.  
  187.                                 parentNode.insertBefore(this, nextSibling);
  188.                                 parentNode.removeChild(nextSibling);
  189.  
  190.                             };
  191.  
  192.                     });
  193.  
  194.                     return sort.call(this, comparator).each(function(i){
  195.                         placements[i].call(getSortable.call(this));
  196.                     });
  197.  
  198.                 };
  199.  
  200.             })();
  201.  
  202.  
  203.             if(autoSort){
  204.                 (function sortComments(){
  205.                     var mylist = $('#comments>div.comment_item');
  206.                     mylist.sortElements(function(a, b){
  207.                         var ratingA = getIntRating($(scoreSelector, a).first());
  208.                         var ratingB = getIntRating($(scoreSelector, b).first());
  209.                         return ratingB - ratingA;
  210.                     });
  211.                 })();
  212.             }
  213.  
  214.  
  215.             (function showWordCount(){
  216.                 var count = $("div.content").text().split(/\s+/).length;
  217.                 $("h1.title").append(" [" + count + "]");
  218.             })();
  219.  
  220.         });
  221.  
  222.     })(jQuery);
  223.  
  224.  
  225.  
  226.  
  227.  
  228. }
  229. letsJQuery();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement