Unread eComments

Shows unread comments on eRepublik articles

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Unread eComments
// @namespace   http://www.erepublik.com
// @description	Shows unread comments on eRepublik articles
// @include     http://www.erepublik.com/*/article/*
// @include     https://www.erepublik.com/*/article/*
// @version     0.0.6
// @require		https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
// @grant		GM_getValue
// @grant		GM_setValue
// @grant		GM_listValues
// @grant		GM_deleteValue
// ==/UserScript==

init();

function init() {
	start(true);
	observeNewComments();
	deleteOldValues();
}

function deleteOldValues() {
    
	var maxArticles = 200;
    
	var storedValues = GM_listValues();
	console.log('Unread eComments: currently tracking ' + storedValues.length + ' article(s)');
    
	if(storedValues.length > maxArticles ){
		storedValues.sort(
			function sortFunction(a, b) {
				return getStoredData(a).registeredAt - getStoredData(b).registeredAt;
			}
		);
		
		var valuesToDelete = storedValues.length - maxArticles;
		
		console.log('Deleting comment info for ' + valuesToDelete + ' old article(s)');
		
		for(var i = 0; i < valuesToDelete; i++) {
			GM_deleteValue(storedValues[i]);
		}
	}
}


function start(isCountUnread) {
	var articleId = findArticleId();
	var commentsRead = getStoredData(articleId);

	if(commentsRead.registeredAt <= 0) {
		commentsRead.registeredAt = Math.round(new Date().getTime() / 1000); // timestamp in seconds
	}
	
	if(isCountUnread) {
		countUnread(commentsRead);
	}
	
	scanComments(commentsRead);
        
	setStoredData(articleId, commentsRead);
}


function observeNewComments() {
	var target = document.querySelector('#loadMoreComments');
	var observer = new MutationObserver(function(mutations) {
            start(false);
		});
	var config = { childList: true };
	observer.observe(target, config);
}

function scanComments(commentsRead) {
	
	var unreadComments = $('.comment-holder')
		.filter( function(index) {
					var commentId = $(this).attr('id').replace(/comment/, '');
					if(commentId in commentsRead.ids) {
						return false;
					} else {
						commentsRead.ids[commentId] = true;
						return true;
					}
				});
				
	unreadComments.css('background-color', '#e6f3f9');
	unreadComments.children('.comments-right')
		.children().andSelf()
		.css('background-color', '#e6f3f9');
}

function countUnread(commentsRead) {
	var nbrOfComments = parseInt($('#comments_button_on>span').text().match(/\d+/), 10);

	var unreadCount = nbrOfComments - commentsRead.commentsLastVisit;
	commentsRead.commentsLastVisit = nbrOfComments;

	if(unreadCount > 0){    
		$('#comments_button_on>span').append(' *** ' + unreadCount + ' ***');
	}
}

function getStoredData(articleId) {
	
	var commentsRead = JSON.parse( GM_getValue(articleId, '{"registeredAt": 0, "commentsLastVisit": 0, "ids": {}}') ); 

	if(!commentsRead) {
		console.log('The stored data is likely to be corrupted.');
	}
	
	return commentsRead;
}

function setStoredData(articleId, commentsRead) {
	GM_setValue(articleId, JSON.stringify(commentsRead));
}

function findArticleId() {
	var id = $('.post_content > h2 > a').attr('href').replace(/\/1\/20/, '');
	id = id.substring(id.lastIndexOf('-')+1);
	return id;
}