Arrow Key Navigation (Generic)

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

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==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();
        }
    });

})();