Lepro Total Comments JS

lepro total comments JS

当前为 2016-07-05 提交的版本,查看 最新版本

  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.1
  13. // ==/UserScript==
  14.  
  15.  
  16. var BEST_TRESHOLD = 0.75;
  17.  
  18. var controls = document.getElementById('js-comments').querySelector('.b-comments_controls'),
  19. comments_holder = document.getElementById('js-comments_holder'),
  20. style = document.createElement('style'),
  21. best = document.createElement('a'),
  22. all = document.createElement('a'),
  23. std_dev,
  24. best_count;
  25.  
  26. style.textContent = ".is_hidden { display: none; }";
  27. document.body.appendChild(style);
  28.  
  29.  
  30. // Кнопки управления
  31. best.textContent = 'лучшее';
  32. all.textContent = 'все';
  33. controls.appendChild(best);
  34. controls.appendChild(all);
  35.  
  36.  
  37. // Лучшее
  38. best.addEventListener('click', function(e) {
  39. e.preventDefault();
  40. if (this.className === 'active') {
  41. return false;
  42. } else if (std_dev) {
  43. document.body.appendChild(style);
  44. } else {
  45. var votes = comments_holder.querySelectorAll('strong.vote_result');
  46. var rating;
  47. best_count = votes.length;
  48. std_dev = (getStdDev(votes) || 0.1);
  49. for (var i = 0; i < votes.length; i++) {
  50. rating = parseInt(votes[i].textContent);
  51. if ( rating > 0 && (rating / std_dev) < BEST_TRESHOLD) {
  52. hideComment(votes[i]);
  53. }
  54. }
  55. this.setAttribute('title', best_count);
  56. }
  57.  
  58. this.className = 'active';
  59. this.nextSibling.className = '';
  60. }, false);
  61.  
  62.  
  63. // Показать всё
  64. all.addEventListener('click', function(e) {
  65. e.preventDefault();
  66. if (std_dev && this.className !== 'active') {
  67. this.className = 'active';
  68. this.previousSibling.className = '';
  69. document.body.removeChild(style);
  70. }
  71. }, false);
  72.  
  73.  
  74. // Среднеквадратическое отклонение
  75. function getStdDev(votes) {
  76. var abovenull = 0,
  77. rating_square_sum = 0,
  78. rating_sum = 0,
  79. rating;
  80.  
  81. for (var i = 0; i < votes.length; i++) {
  82. rating = parseInt(votes[i].textContent);
  83. if ( rating > 0 ) {
  84. abovenull++;
  85. rating_sum += rating;
  86. rating_square_sum += Math.pow(rating, 2);
  87. } else {
  88. hideComment(votes[i]);
  89. }
  90. }
  91. abovenull = (abovenull || 1);
  92. return Math.sqrt( (rating_square_sum / abovenull) -
  93. Math.pow((rating_sum / abovenull), 2) );
  94. }
  95.  
  96.  
  97. // Прячем
  98. function hideComment(el) {
  99. while (!el.classList.contains('comment')) {
  100. el = el.parentNode;
  101. if (el === document) {
  102. throw new Error('Can\'t find comment container.');
  103. }
  104. }
  105. el.classList.add('is_hidden');
  106. best_count--;
  107. }