Highlight Unread Novel

Add new color to novel which have more unread counts On Kakuyomu / Narou / Alphapolis's Favorite Page

目前為 2024-05-05 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name           Highlight Unread Novel
// @name:ja        未読小説をハイライトする
// @namespace      https://greasyfork.org/en/users/1264733
// @version        2024-05-05
// @description    Add new color to novel which have more unread counts On Kakuyomu / Narou / Alphapolis's Favorite Page
// @description:ja アルファポリス・カクヨム・なろうの気に入りページで、未読数が多い小説に色分けを追加する。
// @author         LE37
// @license        MIT
// @include        https://kakuyomu.jp/my/antenna/works*
// @include        /^https:\/\/kakuyomu\.jp\/works\/[0-9]+\/episodes\/[0-9]+(#[^\/]+)?$/
// @include        https://syosetu.com/favnovelmain/list/*
// @include        /^https:\/\/ncode\.syosetu\.com\/[A-z0-9]+\/[0-9]+\/$/
// @include        https://www.alphapolis.co.jp/mypage/notification/index/110000*
// @include        /^https:\/\/www\.alphapolis\.co\.jp\/novel\/[0-9]+/[0-9]+/episode/[0-9]+$/
// @grant          GM_getValue
// @grant          GM_setValue
// @grant          GM_registerMenuCommand
// ==/UserScript==

(()=>{
	'use strict';
	
	// Settings
	// Unread count
	const sCount = 3;
	// Unread item color
	const sColor = "red";
	// Auto Cheering: 0 Off 1 On
	const sCheer = 1;

	let gMk;
	switch (location.host) {
		case "kakuyomu.jp":
			gMk = "HUN_K";
			break;
		case "ncode.syosetu.com":
		case "syosetu.com":
			gMk = "HUN_N";
			break;
		case "www.alphapolis.co.jp":
			gMk = "HUN_A";
			break;
	}

	// GM Menu
	GM_registerMenuCommand("AddFavAuthor", SVM);
	
	// Read List
	const URD = GM_getValue(gMk);
	let tlo = URD ? URD : { FAL:[] };
	const tal = tlo.FAL;

	// Save List
	function USV() {
		tlo = { FAL:tal };
		GM_setValue(gMk, tlo);
	}

	let cSv = 0;
	const uRi = location.href;
	let fAuthor;
	if ( uRi.includes("/my/") || uRi.includes("/favnovelmain/") || uRi.includes("/mypage/") ) {
		FAV();
		CBT();
	} else {
		EPI();
	}

	// Episode Page
	function EPI() {
		let eAuthorSource, eCheerButton, eCheerButtonTag, eIsCheering;
		let rMf = false;
		switch (gMk) {
			case "HUN_K":
				eAuthorSource = document.title;
				eCheerButton = "button#episodeFooter-action-cheerButton";
				eCheerButtonTag = document.querySelector(eCheerButton);
				eIsCheering = parseInt(eCheerButtonTag.getAttribute("data-is-cheering"));
				if ( eIsCheering === 0 && tal.some(name => eAuthorSource.includes('(' + name + ') -')) ) {
					rMf = true;
				}
				break;
			case "HUN_N":
				eAuthorSource = document.querySelector('div.contents1 a:nth-child(2)').textContent;
				eCheerButton = "a.js-novelgood_change";
				eCheerButtonTag = document.querySelector(eCheerButton);
				eIsCheering = document.querySelector("div.is-empty") ? 0 : 1;
				if ( eIsCheering === 0 && tal.some(name => eAuthorSource.includes(name)) ) {
					rMf = true;
				}
				break;
			case "HUN_A":
				eAuthorSource = uRi;
				eCheerButton = "div#contentMangaLikeBtnCircle";
				eCheerButtonTag = document.querySelector(eCheerButton);
				eIsCheering = parseInt(document.querySelector("div.content-manga-my-current-like-count").textContent);
				if ( eIsCheering === 0 && tal.some(name => eAuthorSource.includes(name)) ) {
					rMf = true;
				}
				break;
		}
		const ioc = new IntersectionObserver((entries) => {
			if (entries[0].intersectionRatio <= 0) return;
			ioc.disconnect();
			if (rMf) {
				eCheerButtonTag.style.backgroundColor = sColor;
				if (sCheer === 1) {
					eCheerButtonTag.click();
					//console.log("===いいね===");
				}
			}
		});
		ioc.observe(eCheerButtonTag);
	}

	// Favorite Page
	function FAV() {
		let fNode, fUnreadCount;
		switch (gMk) {
			case "HUN_K":
				fAuthor = "p.widget-antennaList-author";
				fNode = "li.widget-antennaList-item";
				fUnreadCount = "li.widget-antennaList-unreadEpisodeCount";
				break;
			case "HUN_N":
				fAuthor = "div.p-up-bookmark-item__author>a";
				fNode = "li.p-up-bookmark-item";
				fUnreadCount = "span.p-up-bookmark-item__unread-num";
				break;
			case "HUN_A":
				fAuthor = "h2.title>a";
				fNode = "div.content-main";
				fUnreadCount = "a.disp-order";
				break;
		}
		const tNode = document.querySelectorAll(fNode);
		for(let i = 0; i < tNode.length; i++) {
			const fAuthorTag = tNode[i].querySelector(fAuthor);
			const fAuthorName = (gMk === "HUN_A") ? fAuthorTag.href.match(/\d+$/)[0] : fAuthorTag.textContent;
			fAuthorTag.style.color = CHK(fAuthorTag, fAuthorName) ? sColor : "";
			const tUnreadCount = tNode[i].querySelector(fUnreadCount);
			const fCurrent = (gMk === "HUN_A") && tUnreadCount ? parseInt(tUnreadCount.textContent.match(/[0-9]+/)[0]) : 0;
			let tUnreadNum;
			if (tUnreadCount) {
				tUnreadNum = (gMk === "HUN_K") ? parseInt(tUnreadCount.textContent.match(/[0-9]+/)[0])
							: (gMk === "HUN_N") ? parseInt(tUnreadCount.textContent)
							: parseInt(tNode[i].querySelector("a.total").textContent.match(/[0-9]+/)[0]) - fCurrent;
			} else {
				tUnreadNum = 0;
			}
			const fUnreadColor = tUnreadCount && tUnreadNum > sCount ? sColor : "";
			if (tUnreadCount) {
				if (gMk === "HUN_K") {
					const resume = tNode[i].querySelector("a.widget-antennaList-continueReading").href;
					tUnreadCount.innerHTML = '<a href="' + resume + '" style="color:' + fUnreadColor + ';">' + tUnreadCount.textContent + '</a>';
				} else if (gMk === "HUN_N") {
					tNode[i].querySelector("span.p-up-bookmark-item__unread").style.color = fUnreadColor;
				} else {
					tNode[i].querySelector(fUnreadCount).style.color = fUnreadColor;
				}
			} else {
				if (gMk === "HUN_A") {
					tNode[i].querySelector(fAuthor).style.color = sColor;
				}
			}
		}
	}

	// Check Keyword
	function CHK(elem, s) {
		const result = tal.some((v) => s === v);
		if (cSv === 1) {
			elem.style.border = result ? "thin solid fuchsia" : "thin solid dodgerblue";
		} else {
			elem.style.border = "none";
		}
		return result;
	}

	// Select mode
	function SVM() {
		if (cSv === 0) {
			cSv = 1;
			// Disable default click
			document.addEventListener("click", PAC, true);
		} else {
			cSv = 0;
			// Enable default click
			document.removeEventListener("click", PAC, true);
			// Auto save list
			USV();
		}
		FAV();
	}

	// Prevent Anchor Change
	function PAC(e) {
		e.preventDefault();
		if (e.target.closest(fAuthor)) {
			if (gMk === "HUN_A") {
				UTL(e, e.target.href.match(/\d+$/)[0]);
			} else {
				UTL(e, e.target.textContent);
			}
			FAV();
		}
		return false;
	}

	// Update Temp List
	function UTL(e, s) {
		const i = tal.findIndex((v) => v === s);
		if (i !== -1) {
			tal.splice(i,1);
		} else {
			tal.push(s);
		}
		//console.log(tal);
		return tal;
	}

	// Create Float Button
	function CBT() {
		const cButton = document.body.appendChild(document.createElement("button"));
		// Button Style
		cButton.id = "cFbtn";
		cButton.textContent = "💟";
		cButton.style = "position: fixed; bottom: 20%; right: 10%; width: 44px; height: 44px; z-index: 9999; font-size: 200%; opacity: 50%;";
		cButton.type = "button";
		cButton.addEventListener("click", (e) => {
			SVM();
		});
	}
})();