lepro total comments JS
当前为
// ==UserScript==
// @name Lepro Total Comments JS
// @description lepro total comments JS
// @license MIT
// @namespace leprosorium.ru
// @include http://leprosorium.ru/comments/*
// @include http://*.leprosorium.ru/comments/*
// @include http*://leprosorium.ru/comments/*
// @include http*://*.leprosorium.ru/comments/*
// @copyright 2016, lynxtaa
// @grant none
// @version 1.3
// ==/UserScript==
var BEST_TRESHOLD = 0.75;
var controls = document.getElementById('js-comments').querySelector('.b-comments_controls'),
comments_holder = document.getElementById('js-comments_holder'),
style = document.createElement('style'),
best = document.createElement('a'),
all = document.createElement('a'),
std_dev,
comments = [];
style.textContent = ".is_hidden { display: none; }";
document.body.appendChild(style);
controls.querySelector('a[data-key="sort"]').className = '';
// Кнопки управления
best.textContent = 'лучшие';
all.textContent = 'все';
controls.appendChild(best);
controls.appendChild(all);
// Лучшее
best.addEventListener('click', function(e) {
e.preventDefault();
// var start = new Date().getTime();
if (this.className === 'active') {
return false;
} else if (std_dev) {
document.body.appendChild(style);
} else {
var votes = comments_holder.querySelectorAll('strong.vote_result'),
best_count = votes.length,
comment;
std_dev = (getStdDev(votes) || 0.1);
for (var i = 0; i < comments.length; i++) {
if (comments[i].rating / std_dev < BEST_TRESHOLD) {
comment = comments[i].el.parentNode.parentNode.parentNode.parentNode;
if (comment.classList.contains('comment')) {
comment.classList.add('is_hidden');
} else {
throw new Error('Can\'t find comment container.');
}
best_count--;
}
}
this.setAttribute('title', best_count);
}
// var end = new Date().getTime();
// var time = end - start;
// alert('Execution time: ' + time);
this.className = 'active';
this.nextSibling.className = '';
}, false);
// Показать всё
all.addEventListener('click', function(e) {
e.preventDefault();
if (std_dev && this.className !== 'active') {
this.className = 'active';
this.previousSibling.className = '';
document.body.removeChild(style);
}
}, false);
// Среднеквадратическое отклонение
function getStdDev(votes) {
var abovenull = 0,
rating_square_sum = 0,
rating_sum = 0,
rating,
vote;
for (var i = 0; i < votes.length; i++) {
vote = votes[i];
rating = parseInt(vote.textContent);
comments.push({ el: vote, rating: rating });
if ( rating > 0 ) {
abovenull++;
rating_sum += rating;
rating_square_sum += Math.pow(rating, 2);
}
}
abovenull = (abovenull || 1);
return Math.sqrt( (rating_square_sum / abovenull) -
Math.pow((rating_sum / abovenull), 2) );
}