There are 14 previous versions of this script.
// ==UserScript==
// @name HabrTree
// @namespace dotneter
// @include http://habrahabr.ru/post/*
// @include http://habrahabr.ru/company/*/blog/*
// ==/UserScript==
var scoreLeftSide = false;
var autoSort = false;
var green = "#339900";
var red = "#CC0000";
var neutral = "#339900";
var hightlight = "LightGreen";
if ('undefined' == typeof __PAGE_SCOPE_RUN__) {
(function page_scope_runner() {
// If we're _not_ already running in the page, grab the full source
// of this script.
var my_src = "(" + page_scope_runner.caller.toString() + ")();";
// Create a script node holding this script, plus a marker that lets us
// know we are running in the page scope (not the Greasemonkey sandbox).
// Note that we are intentionally *not* scope-wrapping here.
var script = document.createElement('script');
script.setAttribute("type", "application/javascript");
script.setAttribute("src",
"data:,"+escape("var __PAGE_SCOPE_RUN__ = true;\n" + my_src));
// Insert the script node into the page, so it will run, and immediately
// remove it to clean up. Use setTimeout to force execution "outside" of
// the user script scope completely.
setTimeout(function() {
document.body.appendChild(script);
document.body.removeChild(script);
}, 0);
})();
// Stop running, because we know Greasemonkey actually runs us in
// an anonymous wrapper.
return;
}
function letsJQuery() {
(function($){
$(function(){
if(scoreLeftSide){
var comments = $("#comments");
comments.css({overflow:'visible'});
$("#comments .mark").css({position:"absolute", right:comments.width()});
}
function tag(name, attrs){
return $("<" + name + ">", attrs);
}
function getIntRating(span){
var rating = span.html();
if(!rating) return 0;
var minus = rating.indexOf('–') >= 0 ? -1 : 1;
rating = /(\d+)/.exec(rating)[1];
return parseInt(rating,10) * minus;
}
function desc(a,b) { return b - a; }
function hightlightRating(i){
$("a.open-comment").remove();
$("div.comment_item").each(function(){
var comment = $(this);
var message = $('div.message:first',comment);
message.css("border-top", "");
var rating = getIntRating($(scoreSelector+':first',comment));
if(rating >= i){
message.show();
var color = hightlight;
message.css("border-top", "5px solid " + color);
message.parents("div.comment_item.comment-close").each(function(){
showMessage($(this));
});
}else{
comment.addClass("comment-close");
message.hide();
$("div.reply_comments:first",comment).hide();
addOpenLink(comment);
}
});
}
function showMessage(comment){
comment.removeClass("comment-close");
$("a.open-comment:first", comment).remove();
$("div.message:first", comment).show();
$("div.reply_comments:first", comment).show();
}
function addOpenLink(comment){
var div = $("div.reply:first", comment);
var open = tag("a", {"class": "reply open-comment", href:"#", text:"раскрыть"});
open.click(function(){
showMessage(comment);
return false;
});
div.append(open);
}
var scoreSelector = "span.score";
var maxRating = 0;
function showRatings(){
var ratings = {};
$("#comments " + scoreSelector).each(function(){
var intRating = getIntRating($(this));
if(!ratings[intRating]) ratings[intRating] = 0;
ratings[intRating] = ratings[intRating] + 1;
});
var ratingsDiv = tag('div');
var ratingsSorted = $.map(ratings, function(i,k){return parseInt(k,10);}).sort(desc);
if(!ratingsSorted.length)
return;
$.each(ratingsSorted, function(k,i){
var anchor = tag('a', {'class': 'rating-link', href:"#", text:i}).css("color", getRatingColor(i));
anchor.click(function(){
hightlightRating(parseInt(anchor.html(),10));
return false;
});
ratingsDiv.append(anchor);
if(ratings[i] > 1) ratingsDiv.append("(" + ratings[i] + ") ");
else ratingsDiv.append(" ");
});
$("#comments").prepend(ratingsDiv);
}
showRatings();
function getRatingColor(rating){
if(rating < 0)
return red;
if(rating === 0)
return neutral;
return green;
}
$.fn.sortElements = (function(){
var sort = [].sort;
return function(comparator, getSortable) {
getSortable = getSortable || function(){return this;};
var placements = this.map(function(){
var sortElement = getSortable.call(this),
parentNode = sortElement.parentNode,
nextSibling = parentNode.insertBefore(
document.createTextNode(''),
sortElement.nextSibling);
return function() {
if (parentNode === this) {
throw new Error(
"You can't sort elements if any one is a descendant of another.");
}
parentNode.insertBefore(this, nextSibling);
parentNode.removeChild(nextSibling);
};
});
return sort.call(this, comparator).each(function(i){
placements[i].call(getSortable.call(this));
});
};
})();
if(autoSort){
(function sortComments(){
var mylist = $('#comments>div.comment_item');
mylist.sortElements(function(a, b){
var ratingA = getIntRating($(scoreSelector, a).first());
var ratingB = getIntRating($(scoreSelector, b).first());
return ratingB - ratingA;
});
})();
}
});
})(jQuery);
}
letsJQuery();