WN navigation

navigate sites that don't support left/right arrow keys

当前为 2021-10-15 提交的版本,查看 最新版本

// ==UserScript==
// @name         WN navigation
// @namespace    https://gf.qytechs.cn/en/scripts/430252-wn-navigation
// @version      1.3
// @description  navigate sites that don't support left/right arrow keys
// @author       Matt W
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function () {
	"use strict";
	const checkElement = (query, fuzzy, matchText) => {
		let elements = [...document.querySelectorAll(query)];
		let link = null;
		if (fuzzy) {
			link = elements.find((linkElement) => {
                const textContent = linkElement.textContent.toLowerCase();
                if (textContent.length > 20) return false;
                if (textContent === `${matchText} chapter`) return true;
                if (textContent.trim() === matchText) return true;
                if (textContent.includes(matchText) && textContent.includes("chapter")) return true
                if (textContent.includes(matchText) && textContent.includes("chap")) return true
                if (textContent.includes(matchText) && (textContent.includes("<<") || textContent.includes(">>"))) return true
            });
		} else if (elements) {
			elements.forEach((element) => {
				let linkElements = element.getElementsByTagName("a");
				if (linkElements) link = linkElements[0];
			});
		}
		return link;
	};
	const getActionButton = (matchText) => {
		let link = checkElement("a", true, matchText);
		if (link) return link;
		link = checkElement(`[id^='${matchText}']`, false, matchText);
		if (link) return link;
		link = checkElement(`[class^='${matchText}']`, false, matchText);
		if (link) return link;
	};
    const fallbackMatch = (matchOptions) => {
		let elements = [...document.querySelectorAll("a")];
		let link = null;
        link = elements.find((linkElement) => {
            const textContent = linkElement.textContent.toLowerCase();
            for (let i = 0; i < matchOptions.length; i++) {
                const option = matchOptions[i];
                if (textContent.includes(option)) return true
            }
        });
        return link
    }
	let nextButton = getActionButton("next");
    if(!nextButton) {
        const matchOptions = ["→"]
        nextButton = fallbackMatch(matchOptions);
    }
	let prevButton = getActionButton("previous") || getActionButton("prev");
    if(!prevButton) {
        const matchOptions = ["←"]
        prevButton = fallbackMatch(matchOptions);
    }
	const navFunction = (keyEvent) => {
		switch (keyEvent.key) {
			case "ArrowRight":
				if (nextButton) nextButton.click();
				break;
			case "ArrowLeft":
				if (prevButton) prevButton.click();
				break;
		}
	};
	document.addEventListener("keyup", navFunction);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址