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.

Stan na 13-03-2024. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Universal Keyboard Navigation
// @namespace    http://tampermonkey.net/
// @version      2024-03-03
// @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        *://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=universalnavigation.com
// @license      MIT
// @grant        none
// ==/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", "← Previous"],
        next: ["Next Chapter", "Next Page", "Next →"]
    };

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

    // Function to find a link with the given texts
    function findLink(textArray) {
        for (let text of textArray) {
            let link = document.evaluate(`//a[contains(text(),'${text}')]`,  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');
        }
    });
})();