e621 Page Result Grabber

Adds buttons below the tag searchbar to copy queries for the page result post ID's to the clipboard.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         e621 Page Result Grabber
// @namespace    https://greasyfork.org/users/1582013
// @version      1.0
// @description  Adds buttons below the tag searchbar to copy queries for the page result post ID's to the clipboard.
// @author       FreekyCreep
// @license      GNU GPLv3
// @match        https://e621.net/posts?*
// @match        https://e926.net/posts?*
// @grant        GM_addStyle
// ==/UserScript==
(function () {
    //site elements
    const TAGS = document.getElementById("tags");
    const SEARCH_CONTROLS = document.querySelector(".search-controls");
    const THUMBNAILS = document.querySelectorAll(".posts-container > .thumbnail");

    //queries
    const tagLimit = 40;
    const queryCount = Math.ceil(THUMBNAILS.length / tagLimit);
    const queries = new Array(queryCount).fill("");

    //functions
    function buttonClickHandler(e) {
        let button = e.target;
        let queryIndex = Number(button.dataset.index);
        button.disabled = true;
        if (!queries[queryIndex]) {
            const start = queryIndex * tagLimit;
            const end = (queryIndex === queryCount) ? THUMBNAILS.length : start + tagLimit;
            for (let i = start; i < end; i++) queries[queryIndex] += "~id:" + THUMBNAILS[i].dataset.id + " ";
        }
        navigator.clipboard.writeText(queries[queryIndex]).then(() => button.classList.add("active")).catch(err => console.error("Failed to copy query:", err));
        setTimeout(() => {
            button.classList.remove("active");
            button.disabled = false;
        }, 500);
    }
    function thumbnailClickHandler(e) {
        if (!e.ctrlKey || !e.shiftKey || e.button || e.target === e.currentTarget || e.target.tagName === "HR") return;
        e.preventDefault();
        let target = e.target;
        while (target.parentNode !== e.currentTarget) target = target.parentNode;
        TAGS.value += " -id:" + target.dataset.id;
        const scrollY = window.scrollY;
        TAGS.style.height = 0;
        TAGS.style.height = TAGS.scrollHeight + "px";
        window.scrollBy(0, scrollY - window.scrollY);
    }

    //DOM+events
    const buttonText = (queryCount < 5) ? ",'{:') " : "";
    for (let i = 0; i < queryCount; i++) {
        let button = document.createElement("button");
        button.className = "st-button freeky-button";
        button.textContent = buttonText + (i + 1);
        button.title = "Copy query " + (i + 1);
        button.dataset.index = i;
        button.addEventListener("click", buttonClickHandler);
        SEARCH_CONTROLS.appendChild(button);
    }
    for (let i = tagLimit; i < THUMBNAILS.length; i += tagLimit) THUMBNAILS[i].before(document.createElement("hr"));
    document.querySelector(".posts-container").addEventListener("click", thumbnailClickHandler);

    //styles
    GM_addStyle(`
        .freeky-button {
            min-width: 34px;
            align-items: center;
            font-size: 13.6px;
            letter-spacing: -1px;
        }
        .posts-container > hr {
            grid-column: 1 / -1;
            margin: 15px;
            border-style: dashed;
        }
        #mode-box {
            margin-top: 20px !important;
        }
        body[data-st-fullscreen='true'] .posts-index-stats {
            margin-right: ${43 * queryCount + 80}px !important;
        }
    `);
    if (queryCount < 5) return;
    GM_addStyle(`
        body[data-st-fullscreen='false'] .freeky-button {
            min-width: 24px !important;
        }
        body[data-st-fullscreen='false'] .search-controls {
            gap: 2px !important;
        }
    `);
})();