Gelbooru sort by hotkeys

This script allows you to automatically fill a search input with predefined keywords and trigger the search button using keyboard shortcuts.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Gelbooru sort by hotkeys
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  This script allows you to automatically fill a search input with predefined keywords and trigger the search button using keyboard shortcuts.
// @author       Peepo
// @match        *://gelbooru.com/*
// @license MIT
// ==/UserScript==

(function() {
    const keyToTextMap = {
        'r': 'sort:random',
        's': 'sort:score'
    };

    const input = document.getElementById('tags-search');
    const searchButton = document.querySelector('input.searchList[type="submit"]');

    if (!input || !searchButton) return;

    function updateInputValue(newKey) {
        const newText = keyToTextMap[newKey];
        if (!newText) return;

        let foundKey = null;
        for (const k in keyToTextMap) {
            if (input.value.includes(keyToTextMap[k])) {
                foundKey = k;
                break;
            }
        }

        if (foundKey === newKey) {
            return;
        } else if (foundKey) {
            input.value = input.value.replace(keyToTextMap[foundKey], newText);
        } else {
            if (input.value.length > 0) {
                input.value += " " + newText;
            } else {
                input.value = newText;
            }
        }

        searchButton.click();
    }

    document.addEventListener('keydown', function(event) {
        const activeTag = document.activeElement.tagName.toUpperCase();
        if (activeTag === 'INPUT' || activeTag === 'TEXTAREA') return;

        if (keyToTextMap[event.key]) {
            event.preventDefault();
            updateInputValue(event.key);
        }
    });
})();