booru keybinds

Adds galllery page navigation keybinds to various booru sites

当前为 2025-12-11 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name            booru keybinds
// @namespace       861ddd094884eac5bea7a3b12e074f34
// @version         2.1
// @description     Adds galllery page navigation keybinds to various booru sites
// @author          Anonymous
// @match           https://rule34.xxx/index.php?page=post&s=list&*
// @include         /^https:\/\/yande\.re\/post(\?page=[1-9]+)?([&?]tags=[^&]+)?/
// @include         /^https:\/\/rule34\.us\/index\.php\?r=posts(%2F|\/)index.*/
// @match           https://gelbooru.com/index.php?page=post&s=list&*
// @grant           none
// @license         MIT-0
// ==/UserScript==

(function() {
    'use strict';

    const PAGE_DOMAINS = ['yande.re', 'rule34.us'];
    const PID_DOMAINS = {'rule34.xxx': 42, 'gelbooru.com': 42};

    const PARAMS = new URLSearchParams(window.location.search);
    const DOMAIN = window.location.hostname;

    function detectPaginationType() {
        if (PARAMS.has('s') || DOMAIN in PID_DOMAINS) {
            return 'pid';
        } else if (PARAMS.has('page') || PAGE_DOMAINS.indexOf(DOMAIN) > -1) {
            return 'page';
        }
        return null;
    }

    function getCurrentPage() {
        const type = detectPaginationType();
        if (type === 'pid') {
            return parseInt(PARAMS.get('pid')) || 0;
        } else if (type === 'page') {
            return parseInt(PARAMS.get('page')) || 1;
        }
        return null;
    }

    function calculatePreviousPage(type, current, pageLength) {
        if (type === 'pid') {
            if (current > 0) {
                return Math.max(0, current - pageLength);
            }
        } else if (type === 'page') {
            if (current > 1) {
                return (current - 1);
            }
        }
        return null;
    }

    function calculateNextPage(type, current, pageLength) {
        if (type === 'pid') {
            return (current + pageLength);
        } else if (type === 'page') {
            return (current + 1);
        }
        return null;
    }

    function navigate(direction) {
        const type = detectPaginationType();
        const current = getCurrentPage();

        let pageLength;null
        if (type == 'pid') {
            pageLength = PID_DOMAINS[DOMAIN];
        } else if (type == 'page') {
            pageLength = PAGE_DOMAINS[DOMAIN];
        }

        let dest;
        if (direction == 'previous') {
            dest = calculatePreviousPage(type, current, pageLength);
        } else if (direction = 'next') {
            dest = calculateNextPage(type, current, pageLength);
        }
        if (dest == null) return false;

        if (type === 'pid') {
            PARAMS.set('pid', dest);
        } else if (type === 'page') {
            PARAMS.set('page', dest);
        } else {
            console.log('Keybind caught. Navigation failed!');
            console.debug(`direction=${direction}, type=${type}, current=${current}, pageLength=${pageLength}`);
            return false;
        }

        window.location.href = `${window.location.pathname}?${PARAMS.toString()}`;
    }

    // Only activate if pagination is detected
    const paginationType = detectPaginationType();
    if (!paginationType) {
        return false;
    }

    // Add keyboard event listener
    document.addEventListener('keydown', function(e) {
        // Ignore if user is typing in an input field
        if (
            e.target.tagName === 'INPUT'
            || e.target.tagName === 'TEXTAREA'
        ) return;

        switch(e.key) {
            case 'ArrowLeft':
                e.preventDefault();
                navigate('previous');
                break;
            case 'ArrowRight':
                e.preventDefault();
                navigate('next');
                break;
        }
    });

    console.log(`Keybinds loaded (${paginationType} mode). Use ← → arrow keys to navigate gallery pages.`);
})();