Greasy Fork

Greasy Fork is available in English.

Novel Ranking Filter

Hameln Kakuyomu Narou Ranking Page Filtering by AuthorID and Tag

当前为 2024-02-29 提交的版本,查看 最新版本

// ==UserScript==
// @name           Novel Ranking Filter
// @name:ja        小説ランキングフィルター
// @namespace      http://greasyfork.icu/en/users/1264733
// @version        2024-02-29
// @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_deleteValue
// @grant          GM_listValues
// ==/UserScript==

(function() {
	'use strict';
	GM_registerMenuCommand("SelectMode", smode);
	GM_registerMenuCommand("RemoveList", rlist);
	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;
	}
	// Show disliked novel: 0 blur 1 hide
	let s_type = 0;
	// DislikedTagList
	let t_lst = [];
	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);
	};
	const no = document.querySelectorAll(n_nc);
	let i = no.length;
	while (i--) {
		let dislike = false;
		const al = no[i].querySelector(a_lk);
		// Hameln: Novel doesn't have a 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.style.backgroundColor = "#C0C0C0";
			}
		}
		if (dislike === false) {
			const kl = no[i].querySelectorAll(t_nc);
			// All: Novel doesn't have a 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.style.backgroundColor = "#C0C0C0";
						break;
					}
				}
			}
		}
		const ss = no[i].querySelector(n_ic);
		if (dislike === true) {
			switch (s_type) {
				case 0:
					no[i].style.filter = "opacity(36%)";
					// All: Novel doesn't have a Intro
					if (ss !== null) {
						ss.style.display = "none";
					}
					break;
				case 1:
					no[i].style.display = "none";
					break;
			}
		} else {
			// Novel isn't on any list
			//no[i].style.backgroundColor = "#0099FF";
			// All: Novel doesn't have a Intro
			if (ss !== null) {
				ss.style.maxHeight = "120px";
				ss.style.overflow = "hidden";
			}
		}
	}
	function smode() {
		console.log(a_key + ": " + a_lst);
		console.log("Tags: " + t_lst);
		let i = no.length;
		while (i--) {
			const al = no[i].querySelector(a_lk);
			// Hameln: Novel doesn't have a AuthorLink
			if (al !== null) {
				const aid = al.href.match(a_re)[1];
				al.onclick = (e) => {
					e.preventDefault();
					console.log(aid);
					// New added AuthorID
					al.style.backgroundColor = "#0099FF";
					if (a_lst.some(v => aid.includes(v))) {
						let t = prompt("This AuthorID is alreay on ur list, remove it?", "Yes");
						if (t === null) {
							//console.log("Canceled");
						} else if (t.toLowerCase() === "yes") {
							let 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);
							// New removed AuthorID
							al.style.backgroundColor = "";
						} else {
							//console.log("EDIT???");
						}
					} else {
						a_lst.push(aid);
						console.log(a_key + ": " + a_lst);
						GM_setValue(a_key, a_lst);
					}
				}
				al.style.border = "thin solid #0000FF";
			}
			const kl = no[i].querySelectorAll(t_nc);
			// All: Novel doesn't have a Tag
			if (kl !== null) {
				let k = kl.length;
				while (k--) {
					const kwd = kl[k];
					kwd.onclick = (e) => {
						e.preventDefault();
						console.log(kwd.text);
						// New added Tag
						kwd.style.backgroundColor = "#0099FF";
						if (t_lst.some(v => kwd.text.includes(v))) {
							let t = prompt("This Tag is alreay on ur list, remove it?", "Yes");
							if (t === null) {
								//console.log("Canceled");
							} else if (t.toLowerCase() === "yes") {
								let k_i = t_lst.findIndex(x => x === kwd.text);
								t_lst.splice(k_i,1);
								console.log(k_i, t_lst);
								GM_setValue("Tags", t_lst);
								// New removed Tag
								kwd.style.backgroundColor = "";
							} else {
								//console.log("EDIT???");
							}
						} else {
							t_lst.push(kwd.text);
							console.log("Tags: " + t_lst);
							GM_setValue("Tags", t_lst);
						}
					}
					kwd.style.border = "thin solid #0000FF";
				}
			}
		}
	}
	function rlist() {
		// Temp fix for TamperMonkey
		let keys;
		const t = Number(prompt("Which List u want to remove: 0. " + a_key + " Remove AuthorID List 1. Remove Tags List, 2. Remove Both, 3. Remove All, 4. Show All", "4"));
		switch (t) {
			case 0:
				GM_deleteValue(a_key);
				break;
			case 1:
				GM_deleteValue("Tags");
				break;
			case 2:
				GM_deleteValue(a_key);
				GM_deleteValue("Tags");
				break;
			case 3:
				keys = GM_listValues();
				for (let key of keys) {
					GM_deleteValue(key);
				}
				break;
			case 4:
				prompt("All Saved Key&Value, Copy&Backup in case", "Key: " + GM_listValues() + "; Value: " + GM_listValues().map(GM_getValue));
				break;
			default:
				//console.log("EDIT???");
		}
		//console.log(GM_listValues());
	}
	function stype() {
		if (s_type === 0) {
			s_type = 1;
		} else {
			s_type = 0;
		}
		GM_setValue("ShowType", s_type);
		console.log("ShowType: " + s_type);
	}
})();