Lepro Total Comments JS

lepro total comments JS

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

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