booru keybinds

Adds galllery page navigation keybinds to various booru sites

目前為 2025-12-11 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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.`);
})();