Greasy Fork

Greasy Fork is available in English.

Novel Ranking Filter

Alphapolis Hameln Kakuyomu Narou Ranking Page Filtering by AuthorID and Tag

目前为 2024-03-25 提交的版本,查看 最新版本

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

(function() {
	'use strict';
	// a_k: AuthorListKey, a_l: AuthorLink, a_r: RegForAuthorID, n_n: NovelNode, t_n: Tag;
	let a_k, a_l, a_r, n_n, t_n;
	switch (location.host) {
		case "www.alphapolis.co.jp":
			a_k = "A";
			a_l = "div.author>a:nth-child(1)";
			a_r = /detail\/(\d+)$/;
			n_n = "div.section";
			t_n = "li.tag a";
			break;
		case "syosetu.org":
			a_k = "H";
			a_l = "div.blo_title_sak a";
			a_r = /user\/(\d+)/;
			n_n = "div.section3";
			t_n = "div.all_keyword:nth-child(9) a";
			break;
		case "kakuyomu.jp":
			a_k = "K";
			a_l = "a.widget-workCard-authorLabel";
			a_r = /users\/(.*)$/;
			n_n = "div.widget-work";
			t_n = "a[itemprop='keywords']";
			break;
		case "yomou.syosetu.com":
			a_k = "N";
			a_l = "div.p-ranklist-item__author a";
			a_r = /\/(\d+)/;
			n_n = "div.p-ranklist-item";
			t_n = "div.p-ranklist-item__keyword a";
			break;
	}
	// Default Css
	let ds = (`.nbg { background-color: #CCC; } .nbl { opacity: 36%; }.nhd { display: none; }.sbg { background-color: #09F; } .bna { position: fixed; top: 16%; left: 19%; width: 62%; height: 44px; z-index: 9999; } .bio { width: 44px; height: 44px; font-size: 200%; } #bmi { opacity: 50%; } #bta { background-color: #5CE; padding: 2%; } #bta:focus { color: lime; background-color: #222; } #bta, .bcd { visibility: hidden; }`);
	const G = GM_getValue(a_k);
	// gas: AutoSave [0 Off 1 On(Default)]
	let gas = G !== undefined ? G.AS : 1;
	// gs: ShowType [0 Blur 1 Hide(Default)]
	let gs = G !== undefined ? G.ST : 1;
	// ga: DislikedAuthorList
	let ga = G !== undefined ? G.AL : [];
	// gt: DislikedTagList;
	let gt = G !== undefined ? G.TL : [];
	// us: Custom StyleSheet
	let us = G !== undefined ? G.CS : ds;
	function S() {
		const O = { AS:gas, ST:gs, AL:ga, TL:gt, CS:us };
		GM_setValue(a_k, O);
	}
	const AC = (() => {
		const cs = document.createElement('style');
		document.head.appendChild(cs);
		return (sS) => cs.textContent = sS;
	})();
	AC(us);
	const no = document.querySelectorAll(n_n);
	// NormalMode
	NM();
	function NM() {
		for (let i = 0; i < no.length; i++) {
			let d = CT(i);
			const al = no[i].querySelector(a_l);
			// No AuthorLink
			if (al === null) {
				d;
			} else {
				const ai = al.href.match(a_r)[1];
				//console.log(ai);
				if (ga.some((v) => ai === v)) {
					al.classList.add("nbg");
					DN(i);
					continue;
				}
				al.classList.remove("nbg");
				d;
			}
			if (d) {
				DN(i);
			} else {
				no[i].classList.remove("nbl");
				no[i].classList.remove("nhd");
			}
		}
	}
	function CT(i) {
		const kl = no[i].querySelectorAll(t_n);
		// No Tag
		if (kl === null) {
			return false;
		}
		for (let k = 0; k < kl.length; k++) {
			const kd = kl[k];
			//console.log(kd);
			if (gt.some((v) => kd.text === v)) {
				kd.classList.add("nbg");
				return true;
			}
			kd.classList.remove("nbg");
		}
		return false;
	}
	function DN(i) {
		switch (gs) {
			case 0:
				no[i].classList.add("nbl");
				no[i].classList.remove("nhd");
				break;
			case 1:
				no[i].classList.remove("nbl");
				no[i].classList.add("nhd");
				break;
		}
	}
	// SelectMode: 0 Off 1 On
	let s_m = 0;
	const AE = (() => {
		const es = document.createElement('style');
		document.head.appendChild(es);
		return (sS) => es.textContent = sS;
	})();
	function SM() {
		//console.log(a_k + ": " + ga);
		//console.log("TG: " + gt);
		let ess;
		if (s_m === 0) {
			s_m = 1;
			// Disable default click
			document.addEventListener("click", UH, true);
			ess = a_l + ", " + t_n + "{ border: thin solid #00F; }";
		} else {
			s_m = 0;
			// Restore default click
			document.removeEventListener("click", UH, true);
			// Clear remain selected elements's background color
			const tno = document.getElementsByClassName("sbg");
			for (let l = 0; l < tno.length; l++) {
				tno[l].classList.remove("sbg");
			}
			ess = a_l + ", " + t_n + " { border: none; }";
		}
		AE(ess);
	}
	// UpdateHandler
	function UH(e) {
		if (e.target.matches(a_l)) {
			const ai = e.target.href.match(a_r)[1];
			UT(e, ai, ga);
			if (gas === 1) {
				S();
			}
			bta.innerText = ga;
		} else if (e.target.closest(t_n)) {
			const kd = e.target.textContent;
			UT(e, kd, gt);
			if (gas === 1) {
				S();
			}
			bta.innerText = gt;
		}
		NM();
		return false;
	}
	// UpdateTemp
	function UT(e, s, l) {
		e.preventDefault();
		e.target.classList.add("sbg");
		const li = l.findIndex((v) => v === s);
		if (li !== -1) {
			l.splice(li,1);
			e.target.classList.remove("sbg");
		} else {
			l.push(s);
		}
		return l;
	}
	// CustomMenu
	const bba = document.createElement("div");
	bba.innerHTML = '<nav class="bna">' +
						'<button id="bmi" class="bio" type="button">📘</button>' + 
						'<button id="bst" class="bcd bio" type="button">👀</button>' + 
						'<button id="bsm" class="bcd bio" type="button">💮</button>' + 
						'<button id="bos" class="bcd bio" type="button">💾</button>' + 
						'<p id="bta" class="bcd" contenteditable>' + a_k + '</p>' + 
					'</nav>';
	document.body.appendChild(bba);
	const bta = document.getElementById("bta");
	const bmi = document.getElementById("bmi");
	bmi.addEventListener("click", CM, true);
	function CM() {
		bta.innerText = a_k;
		const bc = document.getElementsByClassName("bcd");
		let bf;
		for (let j = 0; j < bc.length; j++) {
			switch (bc[j].id) {
				case "bst":
					bf = ST;
					break;
				case "bsm":
					bf = SM;
					break;
				case "bos":
					bf = OS;
					break;
				case "bta":
					bf = null;
					break;
			}
			if (bc[j].style.visibility !== "visible") {
				bc[j].style.visibility = "visible";
				bc[j].addEventListener("click", bf, true);
			} else {
				bc[j].style.visibility = "hidden";
				bc[j].removeEventListener("click", bf, true);
			}
		}
	}
	// ShowType
	function ST() {
		if (gs === 0) {
			gs = 1;
		} else {
			gs = 0;
		}
		if (gas === 1) {
			S();
		}
		bta.innerText = gs;
		NM();
	}
	// OptionSave
	function OS() {
		if (!bta.innerText.startsWith('S')) {
			bta.innerText = 'SAVE=' + gas + '\r\nSHOW=' + gs + '\r\nAIDS=' + ga + '\r\nTAGS=' + gt + '\r\nUCSS=' + us;
		} else {
			try {
				const tc = bta.innerText.split('\n', 5);
				const tas = tc[0].slice(5);
				const ts = tc[1].slice(5);
				const ta = tc[2].slice(5);
				const tt = tc[3].slice(5);
				const tu = tc[4].slice(5);
				gas = tas === NaN || tas === '' ? 0 : parseInt(tas);
				gs = ts === NaN || ts === '' ? 0 : parseInt(ts);
				ga = ta === null || ta === '' ? [] : ta.split(',');
				gt = tt === null || tt === '' ? [] : tt.split(',');
				us = tu === null || tu === '' ? ds : tu;
				S();
				bta.innerText = '✔';
				AC(us);
				NM();
			} catch (e) {
				//console.error(error.message);
				bta.innerText = '❌';
			}
		}
	}
})();