olegkrasnov

Untitled

Jul 15th, 2021
1,557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        habr-best-comments
  3. // @namespace   http://habr.com
  4. // @include     /^https?://habr\.com/(ru|en)/((post|company|article|news)/)?.*$/
  5. // @grant       none
  6. // @run-at      document-start
  7. // @version     0.4.6
  8. // @downloadURL https://bitbucket.org/liiws/habr-best-comments/downloads/habr-best-comments.user.js
  9. // @updateURL   https://bitbucket.org/liiws/habr-best-comments/downloads/habr-best-comments.meta.js
  10. // ==/UserScript==
  11.  
  12.  
  13. // fix blocked broken ads
  14. (function() {
  15.   var wnd = typeof unsafeWindow == "undefined" ? window : unsafeWindow;
  16.   if (typeof wnd.adriver == "undefined") {
  17.     wnd.adriver = function() {};
  18.   }
  19. })();
  20.  
  21. window.addEventListener('DOMContentLoaded', Run);
  22. window.addEventListener('load', Run);
  23.  
  24. function Run() {
  25.   // if we called from 'DOMContentLoaded' then we don't need be called from 'onload'
  26.   window.removeEventListener('load', Run);
  27.  
  28.   // options
  29.   var _fgAuthor = '#F76D59';
  30.   var _bgAuthor = '#FFAA9D';
  31.   var _fgPositiveMark = '#339900';
  32.   var _fgNegativeMark = '#CC0000';
  33.   var _fgZeroMark = '#548EAA';
  34.   var _bgColor = 'transparent';
  35.   var _bgColorNew = '#E8E8FF';
  36.   var _bgColorSelected = '#3D438D';
  37.   var _bgColorSelected = 'rgba(0,0,0,.1)';
  38.   var _highlightIntervalMs = 5400;
  39.   var _scrollTopOffsetPc = 0.2;
  40.   var _fgMedia = '#0000FF';
  41.   var _fgLink = '#366804';
  42.  
  43.   var authorElement = $(".post__user-info.user-info");
  44.   var authorLogin = authorElement.length == 0 ? "" : authorElement.attr("href").split("/").filter(x => x != "").pop();
  45.  
  46.   ShowCommentsPanel();
  47.  
  48.   // update button
  49.   $('span.refresh').click(function() {
  50.     $('.hbc').remove();
  51.     setTimeout(function() {
  52.       WaitForCommentsWillBeLoadedAndUpdateComments();
  53.     }, 500);
  54.  
  55.     function WaitForCommentsWillBeLoadedAndUpdateComments() {
  56.       if ($('span.refresh').hasClass('loading')) {
  57.         // wait till update end
  58.         setTimeout(WaitForCommentsWillBeLoadedAndUpdateComments, 100);
  59.       } else {
  60.         // update comments
  61.         ShowCommentsPanel();
  62.       }
  63.     }
  64.   });
  65.  
  66.   function ShowCommentsPanel() {
  67.     var allComments = GetAllComments();
  68.     ShowComments(allComments);
  69.   }
  70.  
  71.   function GetAllComments() {
  72.     var allComments = [];
  73.     $('.comment').each(function(index, item) {
  74.       var isBanned = $('> .comment__message_banned', item).length > 0;
  75.       if (isBanned) {
  76.         return;
  77.       }
  78.       var id = $(item).attr('id');
  79.       var markItemWrapper = $('> .comment__head > .js-comment-vote', item);
  80.       var markItem = $('> .js-score', markItemWrapper);
  81.       var markTitle = markItem.attr('title');
  82.       var plus = 0;
  83.       var minus = 0;
  84.       if (markTitle && markTitle.length > 0) {
  85.         plus = +(markTitle.match(/↑(\d+)/) || [])[1];
  86.         minus = +(markTitle.match(/↓(\d+)/) || [])[1];
  87.       }
  88.       var isNew = $('> .comment__head', item).hasClass('comment__head_new-comment');
  89.       var userInfoHref = $('> .comment__head > .user-info', item).attr("href") || "";
  90.       var userLogin = $.trim(userInfoHref.split("/").filter(x => x != "").pop());
  91.       var mark = parseInt(markItem.text().match(/\d+/));
  92.       if (markItem.hasClass('voting-wjt__counter_negative'))
  93.         mark = -mark;
  94.       var hasImg = $('> .comment__message', item).find('img').length > 0;
  95.       var hasVideo = $('> .comment__message', item).find('iframe').length > 0;
  96.       var hasLink = $('> .comment__message', item).find('a').length > 0;
  97.  
  98.       allComments.push({
  99.         id: id,
  100.         mark: mark,
  101.         isNew: isNew,
  102.         isAuthor: userLogin == authorLogin,
  103.         hasImg: hasImg,
  104.         hasVideo: hasVideo,
  105.         hasLink: hasLink,
  106.         plus: plus,
  107.         minus: minus,
  108.         markItemWrapper: markItemWrapper
  109.       });
  110.     });
  111.  
  112.     // remove comments without mark
  113.     allComments = allComments.reduce(function(prev, cur) {
  114.       if (!isNaN(cur.mark) && (cur.mark >= 3 || cur.mark <= -3)) {
  115.         prev.push(cur);
  116.       }
  117.       return prev;
  118.     }, []);
  119.  
  120.     // best desc, time asc
  121.     allComments.sort(function(a, b) {
  122.       return a.mark == b.mark ?
  123.         (a.id < b.id ? 1 : -1) :
  124.         ((isNaN(a.mark) ? 0 : a.mark) > (isNaN(b.mark) ? 0 : b.mark) ? 1 : -1)
  125.     });
  126.     allComments.reverse();
  127.  
  128.     return allComments;
  129.   }
  130.  
  131.   function ShowComments(comments) {
  132.     var wnd = $('<div class="hbc" style="width: 80px; top: 55px; bottom: 10px; right: 49px; overflow: auto; position: fixed; z-index: 999; line-height: 1.1em;"></div>');
  133.     $(wnd).css('background-color', _bgColor);
  134.     $('body').append(wnd);
  135.     $.each(comments, function(index, comment) {
  136.  
  137.       // right panel
  138.  
  139.       // create item
  140.       var item = $('<div class="hbc__item" style="text-align: right;"><a href="#" onclick="return false">0</a></div>');
  141.       //$('a', item).attr('href', '#' + comment.id);
  142.       $('a', item).text(isNaN(comment.mark) ? '?' : (comment.mark > 0 ? '+' + comment.mark : comment.mark));
  143.       $('a', item).attr('iid', comment.id);
  144.  
  145.       // mark color
  146.       if (comment.mark > 0)
  147.         $('a', item).css('color', _fgPositiveMark);
  148.       else if (comment.mark < 0)
  149.         $('a', item).css('color', _fgNegativeMark);
  150.       else
  151.         $('a', item).css('color', _fgZeroMark);
  152.  
  153.  
  154.       if (comment.isAuthor) {
  155.         $('a', item).before('<span style="color: ' + _fgAuthor + '; font-weight: bold;">A </span>');
  156.       }
  157.       if (comment.hasImg) {
  158.         $('a', item).before('<span style="color: ' + _fgMedia + '; font-weight: bold;">i </span>');
  159.       } else if (comment.hasVideo) {
  160.         $('a', item).before('<span style="color: ' + _fgMedia + ';">v </span>');
  161.       }
  162.       if (comment.hasLink) {
  163.         $('a', item).before('<span style="color: ' + _fgLink + '; font-weight: bold;">L </span>');
  164.       }
  165.  
  166.       // bg color
  167.       if (comment.isNew) {
  168.         $(item).addClass('hbc__item-when-new');
  169.         $(item).css('background-color', _bgColorNew);
  170.       }
  171.  
  172.       // onclick event
  173.       $(item).bind('click', Comment_OnClick);
  174.  
  175.       // add item
  176.       $(wnd).append(item);
  177.  
  178.  
  179.       // add plus/minus to the comment mark
  180.  
  181.       if (comment.plus > 0 && comment.minus > 0) {
  182.         var markItemWrapper = comment.markItemWrapper;
  183.         markItemWrapper.find('.hbc__mark-add').remove();
  184.         item = $('<div class="hbc__mark-add" style="font-weight: bold; line-height: 6px; opacity: 0.4; text-align: right; padding-right: 25px; margin-top: -6px;"><span style="color: ' + _fgPositiveMark + ';">+' + comment.plus + '</span> <span style="color: ' + _fgNegativeMark + ';">-' + comment.minus + '</span></div>');
  185.         markItemWrapper.closest('.comment').find('.comment__head').after(item);
  186.       }
  187.  
  188.     });
  189.  
  190.     // highlight author name
  191.     $('.comment__head > a.user-info:contains("' + authorLogin + '")').css('background-color', _bgAuthor);
  192.   }
  193.  
  194.   function Comment_OnClick() {
  195.     $('.hbc__item').css('background-color', _bgColor);
  196.     $('.hbc__item-when-new').css('background-color', _bgColorNew);
  197.     $(this).css('background-color', _bgColorSelected);
  198.     // go to url before browser "A" to emulate click at "A" two times. Habr has bug - click on "A" first time after page opening goes to wrong comment.
  199.     //document.location = $(this).find('a').attr('href');
  200.  
  201.     // scroll to comment
  202.     var id = $(this).find('a').attr('iid');
  203.     var commentElement = document.getElementById(id);
  204.     var elementPosition = GetElementPosition(commentElement);
  205.     var viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
  206.     window.scrollTo(0, elementPosition.top - viewHeight * _scrollTopOffsetPc);
  207.  
  208.     // highlight comment
  209.     $(commentElement).effect("highlight", {}, _highlightIntervalMs);
  210.   }
  211.  
  212.   function GetElementPosition(elem) {
  213.     var body = document.body;
  214.     var docEl = document.documentElement;
  215.  
  216.     var scrollTop = window.pageYOffset || docEl.scrollTop || body.scrollTop;
  217.     var scrollLeft = window.pageXOffset || docEl.scrollLeft || body.scrollLeft;
  218.  
  219.     var clientTop = docEl.clientTop || body.clientTop || 0;
  220.     var clientLeft = docEl.clientLeft || body.clientLeft || 0;
  221.  
  222.     var box = elem.getBoundingClientRect();
  223.     var top = box.top + scrollTop - clientTop;
  224.     var left = box.left + scrollLeft - clientLeft;
  225.  
  226.     return {
  227.       top: Math.round(top),
  228.       left: Math.round(left)
  229.     };
  230.   }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment