Random Prompt from Related Tags.

Press Ctrl+Y to create a random prompt from the results of Danbooru's RelatedTags and copy it to the clipboard. If you press Alt, it will be 20, if you press Shift, it will be 30, and if you press both Alt and Shift, it will be 50.

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Random Prompt from Related Tags.
// @namespace    https://www6.notion.site/dc99953d5f04405c893fba95dace0722
// @version      1
// @description  Press Ctrl+Y to create a random prompt from the results of Danbooru's RelatedTags and copy it to the clipboard. If you press Alt, it will be 20, if you press Shift, it will be 30, and if you press both Alt and Shift, it will be 50.
// @author       SenY
// @match        https://danbooru.donmai.us/related_tag*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=donmai.us
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    const generateTags = (tagsLimit) => {
        let tags = document.getElementById("search_query").value.split(" ").map(x => x.trim().replace(/_/g, " "));
        let order = document.getElementById("search_order")
        if(order && order.value){
            order = order.value.toLowerCase();
            while (tags.length < tagsLimit){
                document.querySelectorAll('[href^="/posts?tags="]').forEach(a => {
                    let tr = a.closest("tr");
                    let tag = a.textContent.trim().replace(/_/g, " ");
                    let num = tr.querySelector("."+order+"-column span").textContent.replace("%", "") - 0;
                    let randomNum = Math.floor(Math.random() * 101);
                    if (num > randomNum && tags.length < tagsLimit && !tags.includes(tag) ) {
                        tags.push(tag);
                    }
                })
            }
            console.log(tags.join(", "));
            navigator.clipboard.writeText(tags.join(", "));
        }else{
            alert("Please select any value on Order property.");
        }
    }
    document.addEventListener("keyup", (e) => {
        if (e.ctrlKey && e.code === 'KeyY') {
            if (!e.altKey && !e.shiftKey) {
                generateTags(10);
            } else if (e.altKey && !e.shiftKey) {
                generateTags(20);
            } else if (!e.altKey && e.shiftKey) {
                generateTags(30);
            } else if (e.altKey && e.shiftKey) {
                generateTags(50);
            }
        }
    });
})();