Sleazy Fork is available in English.

e-hentai 搜索輔助 快速添加標籤

點擊按鈕快速添加指定標籤至搜索框

// ==UserScript==
// @name        e-hentai 搜索輔助 快速添加標籤
// @namespace   https://greasyfork.org/scripts/500650
// @version     0.3
// @description 點擊按鈕快速添加指定標籤至搜索框
// @author      fmnijk
// @match       https://e-hentai.org/*
// @match       https://exhentai.org/*
// @icon        https://www.google.com/s2/favicons?domain=e-hentai.org
// @grant       none
// @run-at      document-end
// @license     MIT
// ==/UserScript==

(function() {
    'use strict';

    var tags = {
        'language:chinese$': '中文',
        'other:"full color$"': '全彩',
        'other:uncensored$': '無碼'
    };
    var nextline = [-1];

    function createButtons(targetElement) {
        var style = document.createElement('style');
        style.textContent = `
            .custom-button {
                color: #f1f1f1;
                border-color: #1357df;
                background: radial-gradient(#1357df, #3377FF) !important;
                padding: 2px 5px;
                border-radius: 3px;
                text-decoration: none;
            }
        `;
        document.head.appendChild(style);

        var fragment = document.createDocumentFragment();
        Object.entries(tags).forEach(function([tag, buttonText], index) {
            var span = document.createElement('span');
            span.innerHTML = '<a href="javascript:void(0);" class="custom-button">' + buttonText + '</a>';
            if (nextline.includes(index + 1) || (nextline.includes(-1) && index === Object.keys(tags).length - 1)) {
                span.innerHTML += '<br>';
            } else {
                span.innerHTML += ' &nbsp;';
            }
            span.querySelector('a').onclick = function(e) {
                e.preventDefault();
                if (window.getSelection) {
                    window.getSelection().removeAllRanges();
                } else if (document.selection) {
                    document.selection.empty();
                }
                var searchInput = document.querySelector('#f_search');
                if (searchInput) {
                    var currentValue = searchInput.value;
                    var tagToCheck = tag.trim();
                    if (currentValue.includes(tagToCheck)) {
                        var regex = new RegExp("\\s?" + tagToCheck.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "\\s?");
                        searchInput.value = currentValue.replace(regex, ' ').trim();
                    } else {
                        searchInput.value = (currentValue ? currentValue + ' ' : '') + tagToCheck;
                    }
                }
            };
            fragment.appendChild(span);
        });
        targetElement.insertBefore(fragment, targetElement.firstChild);
    }

    function checkForTargetElement() {
        var targetElement = document.querySelector('#searchbox > form > div:nth-child(4)');
        if (targetElement) {
            createButtons(targetElement);
            observer.disconnect();
        }
    }

    var observer = new MutationObserver(function(mutations) {
        checkForTargetElement();
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // 初始檢查
    checkForTargetElement();
})();