ppaskannyy / pikabu_best_comments

// ==UserScript==
// @name        pikabu_best_comments
// @namespace   http://pikabu.ru
// @include     http://pikabu.ru/story/*
// @author 		ppaskannyy
// @grant       none
// @version     1.2.1
// @match      http://pikabu.ru/story/*
// @match      https://pikabu.ru/story/*
// @copyright  2012+, ppaskannyy
// ==/UserScript==

$(function($){
	// options
	var _fgAuthor = '#F76D59';
	var _bgAuthor = '#FFAA9D';
	var _fgPositiveMark = '#339900';
	var _fgNegativeMark = '#CC0000';
	var _bgColor = '#F8F8F8';
	var _bgColorNew = '#E8E8FF';
	var _bgColorSelected = '#3D438D';


	var authorName = $.trim($('.info a:nth-child(3):first').text());
	ShowCommentsPanel();

	function ShowCommentsPanel()
	{
		var allComments = GetAllComments();
		ShowComments(allComments);
	}
	
	function GetAllComments()
	{
		var allComments = [];
		$('.comm_wrap_counter').each(function(index, item){ 
			var id = $(item).attr('id');
			var markItem = $('td.info noindex :first-child', item);
			var userName = $.trim($('td.info > noindex > a[style=""], a[style*="color"]', item).text());
			var mark = parseInt(markItem.text().match(/\-*\d+/)); 
			var isRoot = $(item).attr('incl') == 0;
			
			// var hasImg = $('.comment_desc', item).find('img').length > 0;
			var hasImg = $('.c_img', item).length > 0;
			var hasGif = $('.preview_gif', item).length > 0;

			// if ($('a[href *= "youtu"]', item).length > 0) 
			var hasYou = $('a[href *= "youtu"]', item).length > 0;

			var hasHref = ($('a[rel][href]', item).length - $('a[href *= "youtu"]', item).length) > 0;
			
			allComments.push(
			{
				id: id,
				mark: mark,
				isRoot: isRoot,
				isAuthor: userName == authorName,
				hasImg: hasImg,
				hasGif: hasGif,
				hasYou: hasYou,
				hasHref: hasHref
			});
		});
		

		// remove comments without mark
		allComments = allComments.reduce(function(prev, cur)
		{
			if (!isNaN(cur.mark))
			{
				prev.push(cur);
			}
			return prev;
		}, []);

		// best desc, time asc		
		allComments.sort(function(a, b) {
			return a.mark == b.mark
				? (a.id < b.id ? 1 : -1)
				: ((isNaN(a.mark) ? 0 : a.mark) > (isNaN(b.mark) ? 0 : b.mark) ? 1 : -1)
		});
		allComments.reverse();
		
		return allComments;
	}
	
	
	function ShowComments(comments)
	{
		var wnd = $('<div class="hbc" style="width: 80px; top: 35px; bottom: 10px; right: 22px; overflow: auto; position: fixed; font-size:90%"></div>');
		$(wnd).css('background-color', _bgColor);
		$('body').append(wnd);
		$.each(comments, function(index, comment) {
			// create item
            var item = $('<div class="hbc__item" style="text-align: right; padding-right:15px;"><a href="#">0</a></div>');
			$('a', item).attr('href', '#' + comment.id);
			$('a', item).text(isNaN(comment.mark) ? '?' : (comment.mark >= 0 ? '+'+comment.mark : comment.mark));
			
			// mark color
			if (comment.mark >= 0)
				$('a', item).css('color', _fgPositiveMark);
			else
				$('a', item).css('color', _fgNegativeMark);
			
			if (comment.isAuthor)
			{
				$('a', item).css('background-color', 'pink');
			}
			if (comment.hasImg)
			{
				$('a', item).prepend('<span style="color: blue; font-weight: bold;">i </span>');
			}
			if (comment.hasGif)
			{
				$('a', item).prepend('<span style="color: blue; font-weight: bold;">g </span>');
			}
			// root
			if (comment.isRoot)
			{
				$('a', item).prepend('<span style="color: grey; font-weight: bold;">r </span>');
			}
			if (comment.hasYou)
			{
				$('a', item).prepend('<span style="color: orange; font-weight: bold;">y </span>');
			}
			if (comment.hasHref)
			{
				$('a', item).prepend('<span style="color: violet; font-weight: bold;">u </span>');
			}
			
			// onclick event
/*			$(item).bind('click', function(){
				Comment_OnClick();
				setTimeout(function(){ShowCommentsPanel();}, 50); // ??????test	notcorrect !			
			});*/
			$(item).bind('click', Comment_OnClick());
			
			// add item
			$(wnd).append(item);
		});
		
		// highlight author name td.info > noindex > 
		$('a:contains("' + authorName + '")').css({'padding':'0px','background-color':_bgAuthor});

	}
	
	function Comment_OnClick()
	{ 
		$('.hbc__item').css('background-color', _bgColor); 
		$(this).css('background-color', _bgColorSelected);
	}
});