Lepro Total Comments JS

lepro total comments JS

目前為 2016-07-05 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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.1
// ==/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,
	best_count;

style.textContent = ".is_hidden { display: none; }";
document.body.appendChild(style);


// Кнопки управления
best.textContent = 'лучшее';
all.textContent = 'все';
controls.appendChild(best);
controls.appendChild(all);


// Лучшее
best.addEventListener('click', function(e) {
	e.preventDefault();
	if (this.className === 'active') {
		return false;
	} else if (std_dev) {
		document.body.appendChild(style);
	} else {
		var votes = comments_holder.querySelectorAll('strong.vote_result');
		var	rating;
		best_count = votes.length;
		std_dev = (getStdDev(votes) || 0.1);
		for (var i = 0; i < votes.length; i++) {
			rating = parseInt(votes[i].textContent);
			if ( rating > 0 && (rating / std_dev) < BEST_TRESHOLD) {
				hideComment(votes[i]);
			}
		}
		this.setAttribute('title', best_count);
	}

	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;

	for (var i = 0; i < votes.length; i++) {
		rating = parseInt(votes[i].textContent);
		if ( rating > 0 ) {
			abovenull++;
			rating_sum += rating;
			rating_square_sum += Math.pow(rating, 2);
		} else {
			hideComment(votes[i]);
		}
	}
	
	abovenull = (abovenull || 1);
	return Math.sqrt( (rating_square_sum / abovenull) -
				Math.pow((rating_sum / abovenull), 2) );
}


// Прячем
function hideComment(el) {
	while (!el.classList.contains('comment')) {
		el = el.parentNode;
		if (el === document) {
			throw new Error('Can\'t find comment container.');
		}
	}
	el.classList.add('is_hidden');
	best_count--;
}