Greasy Fork

Greasy Fork is available in English.

Novel Ranking Filter

Hameln Kakuyomu Narou Ranking Page Filtering by AuthorID and Tag

当前为 2024-03-01 提交的版本,查看 最新版本

// ==UserScript==
// @name           Novel Ranking Filter
// @name:ja        小説ランキングフィルター
// @namespace      http://greasyfork.icu/en/users/1264733
// @version        2024-03-01
// @description    Hameln Kakuyomu Narou Ranking Page Filtering by AuthorID and Tag
// @description:ja ハーメルン・カクヨム・なろう 作者IDとタグによるランキングページの小説フィルタリング
// @author         LE37
// @license        MIT
// @match          https://kakuyomu.jp/rankings/*
// @match          https://kakuyomu.jp/*recent_works
// @match          https://syosetu.org/?mode=rank*
// @match          https://yomou.syosetu.com/rank/*
// @exclude        https://yomou.syosetu.com/rank/top/
// @grant          GM_registerMenuCommand
// @grant          GM_setValue
// @grant          GM_getValue
// @grant          GM_addStyle
// ==/UserScript==

(function() {
	'use strict';
	GM_registerMenuCommand("SelectMode", smode);
	GM_registerMenuCommand("Show_Type", stype);
	// a_key: AuthorListKey, a_lk: AuthorLink, a_re: RegForAuthorID, a_lst: DislikedAuthorList, n_nc: NovelNode, n_ic: NovelIntro, t_nc: Tag
	let a_key, a_lk, a_re, a_lst, n_nc, n_ic, t_nc;
	switch (location.host) {
		// Hameln
		case "syosetu.org":
			a_key = "Hameln";
			a_lk = "div.blo_title_sak a";
			a_re = /user\/(\d+)/;
			a_lst = [];
			n_nc = "div.section3";
			n_ic = "div.all_arasuji";
			t_nc = "div.all_keyword:nth-child(9) a";
			break;
		// Kakuyomu
		case "kakuyomu.jp":
			a_key = "Kakuyomu";
			a_lk = "a.widget-workCard-authorLabel";
			a_re = /users\/(.*)$/;
			a_lst = [];
			n_nc = "div.widget-work";
			n_ic = "p.widget-workCard-introduction";
			t_nc = "a[itemprop='keywords']";
			break;
		// Narou
		case "yomou.syosetu.com":
			a_key = "Narou";
			a_lk = "div.p-ranklist-item__author a";
			a_re = /\/(\d+)/;
			a_lst = [];
			n_nc = "div.p-ranklist-item";
			n_ic = "div.p-ranklist-item__synopsis";
			t_nc = "div.p-ranklist-item__keyword a";
			break;
		default:
			console("Unknown Host");
	}
	// Show disliked novel: 0 blur 1 hide
	let s_type = 0;
	// DislikedTagList
	let t_lst = [];
	// Load stored values
	if (GM_getValue("ShowType") !== undefined) {
		s_type = GM_getValue("ShowType");
		console.log("ShowType: " + s_type);
	};
	if (GM_getValue(a_key) !== undefined) {
		a_lst = GM_getValue(a_key);
		console.log(a_key + " AuthorID List loaded: " + a_lst);
	};
	if (GM_getValue("Tags") !== undefined) {
		t_lst = GM_getValue("Tags");
		console.log("Tag List loaded: " + t_lst);
	};
	// Add to settings for edit???
	GM_addStyle(`
		.nm_bgc { background-color: #C0C0C0; }
		.nm_blr { opacity: 36%; }
		.nm_hid { display: none; }
		.nm_int { max-height: 120px; overflow: hidden;}
		.sm_bgc { background-color: #0099FF; }
	`);
	const no = document.querySelectorAll(n_nc);
	// NormalMode
	nmode();
	function nmode() {
		let i = no.length;
		while (i--) {
			let dislike = false;
			const al = no[i].querySelector(a_lk);
			// Hameln: No AuthorLink
			if (al !== null) {
				const aid = al.href.match(a_re)[1];
				//console.log(aid);
				if (a_lst.some(v => aid.includes(v))) {
					dislike = true;
					al.classList.add("nm_bgc");
				} else {
					al.classList.remove("nm_bgc");
				}
			}
			if (dislike === false) {
				const kl = no[i].querySelectorAll(t_nc);
				// All: No Tag
				if (kl !== null) {
					let k = kl.length;
					while (k--) {
						const kwd = kl[k];
						if (t_lst.some(v => kwd.text.includes(v))) {
							dislike = true;
							kwd.classList.add("nm_bgc");
							break;
						} else {
							kwd.classList.remove("nm_bgc");
						}
					}
				}
			}
			const ss = no[i].querySelector(n_ic);
			if (dislike === true) {
				switch (s_type) {
					case 0:
						no[i].classList.remove("nm_hid");
						no[i].classList.add("nm_blr");
						// All: No Intro
						if (ss !== null) {
							ss.classList.add("nm_hid");
						}
						break;
					case 1:
						no[i].classList.add("nm_hid");
						break;
					default:
						console("Unknown ShowType");
				}
			} else {
				// Novel isn't on any list
				//no[i].style.backgroundColor = "#00FFFF";
				no[i].classList.remove("nm_blr");
				// All: No Intro
				if (ss !== null) {
					ss.classList.remove("nm_hid");
					ss.classList.add("nm_int");
				}
			}
		}
	}
	function DChandler(e) {
		if(e.target.matches(a_lk) || e.target.closest(t_nc)) {
			e.preventDefault();
			e.target.classList.add("sm_bgc");
			if (e.target.matches(a_lk)) {
				const aid = e.target.href.match(a_re)[1];
				console.log(aid);
				if (a_lst.some(v => aid.includes(v))) {
					let a_i;
					let t = prompt("This AuthorID is alreay on list, remove it?", "Yes");
					switch (t) {
						case "Yes":
							a_i = a_lst.findIndex(x => x === aid);
							a_lst.splice(a_i,1);
							console.log(a_i, a_lst);
							GM_setValue(a_key, a_lst);
							e.target.classList.remove("sm_bgc");
							nmode();
							break;
						case null:
						default:
							console.log("Canceled");
					}
				} else {
					a_lst.push(aid);
					console.log(a_key + ": " + a_lst);
					GM_setValue(a_key, a_lst);
					nmode();
				}
			} else {
				const kwd = e.target.textContent;
				console.log(kwd);
				if (t_lst.some(v => kwd.includes(v))) {
					let k_i;
					let t = prompt("This Tag is alreay on list, remove it?", "Yes");
					switch (t) {
						case "Yes":
							k_i = t_lst.findIndex(x => x === kwd);
							t_lst.splice(k_i,1);
							console.log(k_i, t_lst);
							GM_setValue("Tags", t_lst);
							e.target.classList.remove("sm_bgc");
							nmode();
							break;
						case null:
						default:
							console.log("Canceled");
					}
				} else {
					t_lst.push(kwd);
					console.log("Tags: " + t_lst);
					GM_setValue("Tags", t_lst);
					nmode();
				}
			}
		}
		return false;
	}
	// SelectMode: 0 Off 1 On
	let s_mode = 0;
	function smode() {
		console.log(a_key + ": " + a_lst);
		console.log("Tags: " + t_lst);
		let css;
		if (s_mode === 0) {
			s_mode = 1;
			// Disable default click
			document.addEventListener("click", DChandler, true);
			css = a_lk + ", " + t_nc + "{ border: thin solid #0000FF; }";
		} else {
			s_mode = 0;
			// Restore default click
			document.removeEventListener('click', DChandler, true);
			// Clear remain selected elements's background color
			const et_no = document.getElementsByClassName("sm_bgc");
			let i = et_no.length;
			while (i--) {
				et_no[i].classList.remove("sm_bgc");
			}
			css = a_lk + ", " + t_nc + " { border: none; }";
			nmode();
		}
		GM_addStyle(css);
	}
	function stype() {
		if (s_type === 0) {
			s_type = 1;
		} else {
			s_type = 0;
		}
		GM_setValue("ShowType", s_type);
		console.log("ShowType: " + s_type);
		nmode();
	}
})();