Ruqqus viewed posts tracker

Tracks the posts you have already seen. Works with RuqES expando button and infinite scroll (not required!).

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Ruqqus viewed posts tracker
// @namespace    Doomness
// @version      0.6
// @description  Tracks the posts you have already seen. Works with RuqES expando button and infinite scroll (not required!).
// @author       @Doomness
// @match        https://ruqqus.com/*
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @grant        none
// @homepageURL  https://github.com/NoudH/Ruqqus-viewed-posts-tracker
// @supportURL   https://github.com/NoudH/Ruqqus-viewed-posts-tracker/issues
// ==/UserScript==

/* jshint esversion: 6 */

/* Feel free to change these values to better fit your needs */
let maxViewedPostsCount = 1000 // type: number >= 0; Maximum number of posts to store in the localstorage, 0 is infinite;
/* --------------- */

let viewedPosts = localStorage.getItem('viewedPosts') ? JSON.parse(localStorage.getItem('viewedPosts')) : [];
let cardCount = $('.card').length;

const addStyle = (style) => {
    style = style instanceof Array ? style.join('\n') : style;
    $('head').append($('<style type="text/css">' + style + '</style>'));
};

addStyle(`
.viewedPost-link {
    color: #805ad5!important;
}
`);

const viewedPostsContainsUrl = (url) => {
	return viewedPosts.filter(x => url.includes(x.url)).length > 0;
};

const rememberLink = ($el, url) => {
	if(!viewedPostsContainsUrl(url)){
		viewedPosts.push({date: new Date(), url: url});
		localStorage.setItem('viewedPosts', JSON.stringify(viewedPosts));
		$el.addClass('viewedPost-link');
	}
};

const setupRuqesButton = () => {
	$('a.RuqES-by-enefi--expando-button').click(function() {
		var $linkEl = $(this).closest('div.card').find('a.stretched-link');
		rememberLink($linkEl, $linkEl.attr('href'));
	});
};

const setupLinkClicked = () => {
	//Title anchor
	$('a.stretched-link').mousedown(function() {
		var $linkEl = $(this);
		var url = $linkEl.attr('href');
		rememberLink($linkEl, url);
	});

	//Comment icon
	$('i.fas.fa-comment-dots').parent('a').mousedown(function() {
		var $linkEl = $(this).closest('div.card').find('a.stretched-link');
		var url = $linkEl.attr('href');
		rememberLink($linkEl, url);
	});
};

const setupViewedPosts = () => {
	if(maxViewedPostsCount > 0) {
		while(viewedPosts.length >= maxViewedPostsCount) {
			viewedPosts.shift();
		}
		localStorage.setItem('viewedPosts', JSON.stringify(viewedPosts));
	}
	$('a.stretched-link').filter((_, el) => viewedPostsContainsUrl($(el).attr('href'))).addClass('viewedPost-link');
};

//FIXME: preferably this should trigger when the next page is loaded, but I don't think I can hook into RuqES onNextPageSuccessResponse() function.
const setupInfiniteScrollReload = () => {
	const doc = $(document);
	doc.on('scroll', () => {
		let actualCardCount = $('.card').length;
		if(actualCardCount != cardCount) {
			cardCount = actualCardCount;
			setupViewedPosts();
			setupRuqesButton();
			setupLinkClicked();
		}
	});
};

const work = () => {
	setupViewedPosts();
	setupLinkClicked();
	setupRuqesButton();
	setupInfiniteScrollReload();
};

$(work);