Universal Keyboard Navigation

Enables Previous and Next page navigation using keyboard buttons on any site. Browse your favorite Manga, Manhwa, comic, novel sites with ease.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Universal Keyboard Navigation
// @namespace    http://tampermonkey.net/
// @version      202403301
// @description  Enables Previous and Next page navigation using keyboard buttons on any site. Browse your favorite Manga, Manhwa, comic, novel sites with ease.
// @author       Chief Legend
// @match        *://*/*
// @exclude      *://*.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=universalnavigation.com
// @license      MIT
// @grant        none
// @noframes
// ==/UserScript==

(function() {
    'use strict';

    // List of acceptable innerText values for previous and next pages or chapters
    const navigationTexts = {
        prev: ["Previous Chapter", "Prev Chapter", "Prev Page", "Prev"],
        next: ["Next Chapter", "Next Page", "Next"]
    };

    // Cache for the last found links to improve performance
    let lastFoundLinks = {
        prev: null,
        next: null
    };

    function findLink(textArray) {
        for (let text of textArray) {
            let lowerText = text.toLowerCase();

            // XPath to find case-insensitive match
            let xpath = `//a[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'${lowerText}')]`;

            let link = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
            if (link) return link;
        }
        return null;
    }

    // Function to simulate click on Prev or Next link
    function navigate(direction) {
        if (lastFoundLinks[direction]) {
            // If we have cached the link, use it
            lastFoundLinks[direction].click();
            return;
        }

        // Find the link using the list of acceptable values
        let link = findLink(navigationTexts[direction]);

        // Cache the link for subsequent use
        if (link) {
            lastFoundLinks[direction] = link;
            link.click();
        }
    }

    // Add keydown event listener to the document
    document.addEventListener('keydown', function(event) {
        // Check if the left arrow key (ArrowLeft) or right arrow key (ArrowRight) was pressed
        if (event.key === 'ArrowLeft') {
            navigate('prev');
        } else if (event.key === 'ArrowRight') {
            navigate('next');
        }
    });
})();