Lepro Total Comments JS

lepro total comments JS

当前为 2016-09-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Lepro Total Comments JS
  3. // @description lepro total comments JS
  4. // @license MIT
  5. // @namespace leprosorium.ru
  6. // @include http://leprosorium.ru/comments/*
  7. // @include http://*.leprosorium.ru/comments/*
  8. // @include http*://leprosorium.ru/comments/*
  9. // @include http*://*.leprosorium.ru/comments/*
  10. // @copyright 2016, lynxtaa
  11. // @grant none
  12. // @version 1.5
  13. // ==/UserScript==
  14.  
  15. var BEST_TRESHOLD = 0.75,
  16. std_dev;
  17.  
  18. var all = document.createElement('a'),
  19. best = document.createElement('a'),
  20. controls = document.getElementById('js-comments').querySelector('.b-comments_controls'),
  21. holder = document.getElementById('js-comments_holder'),
  22. style = document.createElement('style');
  23.  
  24. style.textContent = ".is_hidden { display: none; }";
  25. document.body.appendChild(style);
  26.  
  27. controls.querySelector('a[data-key="sort"]').className = '';
  28.  
  29. // Кнопки управления
  30. best.textContent = 'лучшие';
  31. all.textContent = 'все';
  32. controls.appendChild(best);
  33. controls.appendChild(all);
  34.  
  35. // Лучшее
  36. best.addEventListener('click', function(e) {
  37. e.preventDefault();
  38.  
  39. if (this.className === 'active') {
  40. return false;
  41. }
  42.  
  43. this.className = 'active';
  44. this.nextSibling.className = '';
  45.  
  46. if (std_dev) {
  47. document.body.appendChild(style);
  48. } else {
  49. parseComments();
  50. }
  51. }, false);
  52.  
  53. // Показать всё
  54. all.addEventListener('click', function(e) {
  55. e.preventDefault();
  56.  
  57. if (std_dev && this.className !== 'active') {
  58. this.className = 'active';
  59. this.previousSibling.className = '';
  60. document.body.removeChild(style);
  61. }
  62. }, false);
  63.  
  64.  
  65. function parseComments() {
  66. var votes = [].slice.call(holder.querySelectorAll('strong.vote_result'));
  67. var abovenull = 0,
  68. rating_square_sum = 0,
  69. rating_sum = 0,
  70. shown = votes.length;
  71.  
  72. var ratings = votes.map(function(el) {
  73. var rating = +el.textContent;
  74.  
  75. if (rating > 0) {
  76. abovenull++;
  77. rating_sum += rating;
  78. rating_square_sum += Math.pow(rating, 2);
  79. }
  80.  
  81. return { el, rating };
  82. });
  83.  
  84. abovenull = abovenull || 1;
  85.  
  86. std_dev = Math.sqrt((rating_square_sum / abovenull) -
  87. Math.pow(rating_sum / abovenull, 2)) || 0.1;
  88.  
  89. for (var i = ratings.length; i--; ) {
  90. if (ratings[i].rating / std_dev < BEST_TRESHOLD) {
  91. var comment = ratings[i].el.parentNode.parentNode.parentNode.parentNode;
  92. if (!comment.classList.contains('comment')) throw new Error("Can't find comment.");
  93. comment.classList.add('is_hidden');
  94. shown--;
  95. }
  96. }
  97.  
  98. best.setAttribute('title', shown);
  99. }