Novel Ranking Filter [No GM_API Version]

Novel Ranking Filtering Bookmarklet for Mobile Browser

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name           Novel Ranking Filter [No GM_API Version]
// @name:ja        小説ランキングフィルター [GM_APIなし]
// @namespace      https://greasyfork.org/en/users/1264733
// @version        2024-03-24
// @description    Novel Ranking Filtering Bookmarklet for Mobile Browser
// @description:ja モバイルブラウザ用小説ランキングフィルタリングブックマークレット
// @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          none
// ==/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 = JSON.parse(localStorage.getItem(a_k));
	// gs: ShowType [0 Blur 1 Hide(Default)]
	let gs = G !== null ? G.ST : 1;
	// ga: DislikedAuthorList
	let ga = G !== null ? G.AL : [];
	// gt: DislikedTagList;
	let gt = G !== null ? G.TL : [];
	// us: Custom StyleSheet
	let us = G !== null ? G.CS : ds;
	function S() {
		const O = { ST:gs, AL:ga, TL:gt, CS:us };
		localStorage.setItem(a_k, JSON.stringify(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);
			S();
			bta.innerText = ga;
		} else if (e.target.closest(t_n)) {
			const kd = e.target.textContent;
			UT(e, kd, gt);
			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;
		}
		S();
		bta.innerText = gs;
		NM();
	}
	// OptionSave
	function OS() {
		if (!bta.innerText.startsWith('S')) {
			bta.innerText = 'SHOW=' + gs + '\r\nAIDS=' + ga + '\r\nTAGS=' + gt + '\r\nUCSS=' + us;
		} else {
			try {
				const tc = bta.innerText.split('\n', 4);
				const ts = tc[0].slice(5);
				const ta = tc[1].slice(5);
				const tt = tc[2].slice(5);
				const tu = tc[3].slice(5);
				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;
				// Save to localstorage
				S();
				// Save to clipboard
				//navigator.clipboard.writeText(bta.innerText);
				bta.innerText = 'v';
				AC(us);
				NM();
			} catch (e) {
				//console.error(error.message);
				bta.innerText = 'x';
			}
		}
	}
})();