Arrow Key Navigation (Generic)

Navigate Next/Prev pages with Left/Right arrow keys. Tries to detect common pagination patterns.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name        Arrow Key Navigation (Generic)
// @namespace   Violentmonkey Scripts
// @match       *://*/*
// @grant       none
// @version     1.0
// @author      Gemini
// @description Navigate Next/Prev pages with Left/Right arrow keys. Tries to detect common pagination patterns.
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Configuration: Add specific CSS selectors here if the generic ones don't work for a specific site.
    const NEXT_SELECTORS = [
        'a[rel="next"]',                // Standard HTML5
        '.next a',                      // Common class structure
        'a.next',                       // Common class
        '.pagination .next',            // Common pagination
        'a:contains("Next")',           // jQuery-ish pseudo (implemented manually below)
        'a:contains("›")',
        'a:contains(">>")'
    ];

    const PREV_SELECTORS = [
        'a[rel="prev"]',                // Standard HTML5
        '.prev a',
        'a.prev',
        '.previous a',
        'a.previous',
        '.pagination .prev',
        'a:contains("Previous")',
        'a:contains("Prev")',
        'a:contains("‹")',
        'a:contains("<<")'
    ];

    // Helper to find a link based on selectors or text content
    function findLink(selectors, directionText) {
        for (let selector of selectors) {
            // Handle text-based search specifically
            if (selector.includes(':contains')) {
                const textToFind = selector.match(/"(.*?)"/)[1].toLowerCase();
                const links = document.querySelectorAll('a');
                for (let link of links) {
                    if (link.innerText.toLowerCase().includes(textToFind)) {
                        return link;
                    }
                }
            } else {
                // Handle standard CSS selectors
                const element = document.querySelector(selector);
                if (element) return element;
            }
        }
        return null;
    }

    // Event Listener
    document.addEventListener('keydown', function(e) {
        // 1. Safety Check: Don't trigger if user is typing in a text box
        const tag = e.target.tagName.toLowerCase();
        const isEditable = e.target.isContentEditable;
        if (tag === 'input' || tag === 'textarea' || isEditable) {
            return;
        }

        // 2. logic for Arrow Keys
        let linkToClick = null;

        if (e.key === 'ArrowLeft') {
            linkToClick = findLink(PREV_SELECTORS, "prev");
        } else if (e.key === 'ArrowRight') {
            linkToClick = findLink(NEXT_SELECTORS, "next");
        }

        // 3. Navigate if link found
        if (linkToClick) {
            console.log(`[ArrowNav] Navigating via:`, linkToClick);
            // Visual feedback (optional: highlights the button briefly)
            linkToClick.style.border = "2px solid red";
            
            // Click the link
            linkToClick.click();
        }
    });

})();