Danbooru Tag Copy

一键复制 Danbooru 标签,按 meta / rating / character+copyright / @artist / general 分类输出

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name                Danbooru Tag Copy
// @namespace           https://github.com/XiaoLinXiaoZhu/JavaScriptTools
// @version             0.1
// @description         一键复制 Danbooru 标签,按 meta / rating / character+copyright / @artist / general 分类输出
// @author              XLXZ
// @license             MIT
// @match               https://danbooru.donmai.us/posts/*
// @grant               none
// @run-at              document-idle
// ==/UserScript==

"use strict";
(() => {
  var __defProp = Object.defineProperty;
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });

  // src/danbooru-tag-copy/index.ts
  function waitForEl(selector, timeout = 5e3) {
    return new Promise((resolve, reject) => {
      const el = document.querySelector(selector);
      if (el) return resolve(el);
      const observer = new MutationObserver(() => {
        const el2 = document.querySelector(selector);
        if (el2) {
          observer.disconnect();
          resolve(el2);
        }
      });
      observer.observe(document.body, { childList: true, subtree: true });
      setTimeout(() => {
        observer.disconnect();
        reject(new Error(`Timeout waiting for "${selector}"`));
      }, timeout);
    });
  }
  __name(waitForEl, "waitForEl");
  function getTags(tagList, listClass) {
    const ul = tagList.querySelector(`ul.${listClass}`);
    if (!ul) return [];
    const items = ul.querySelectorAll("li[data-tag-name]");
    return Array.from(items).map((li) => {
      const name = li.getAttribute("data-tag-name") ?? "";
      return name.replace(/_/g, " ").toLowerCase();
    });
  }
  __name(getTags, "getTags");
  function getRating() {
    const el = document.getElementById("post-info-rating");
    if (!el) return "";
    const match = el.textContent?.match(/Rating:\s*(.+)/i);
    return match ? match[1].trim().toLowerCase() : "";
  }
  __name(getRating, "getRating");
  function trailing(line) {
    return line ? line + "," : "";
  }
  __name(trailing, "trailing");
  function copyToClipboard(text) {
    if (navigator.clipboard?.writeText) {
      return navigator.clipboard.writeText(text);
    }
    return new Promise((resolve, reject) => {
      const textarea = document.createElement("textarea");
      textarea.value = text;
      textarea.style.position = "fixed";
      textarea.style.opacity = "0";
      document.body.appendChild(textarea);
      textarea.select();
      try {
        document.execCommand("copy");
        resolve();
      } catch (e) {
        reject(e);
      }
      document.body.removeChild(textarea);
    });
  }
  __name(copyToClipboard, "copyToClipboard");
  async function main() {
    try {
      await waitForEl("#tag-list");
    } catch {
      return;
    }
    const tagList = document.getElementById("tag-list");
    const container = document.createElement("div");
    container.style.marginBottom = "10px";
    container.innerHTML = `
    <button id="danbooru-tag-copy-btn" style="
      padding:6px 14px;background:#1a73e8;color:#fff;border:none;
      border-radius:4px;cursor:pointer;font-size:13px;font-family:sans-serif;
    ">\u{1F4CB} Copy Tags</button>
    <span id="danbooru-tag-copy-msg" style="
      margin-left:10px;font-size:12px;color:#4caf50;display:none;
    ">Copied!</span>`;
    tagList.insertBefore(container, tagList.firstChild);
    const btn = document.getElementById("danbooru-tag-copy-btn");
    const msg = document.getElementById("danbooru-tag-copy-msg");
    btn.addEventListener("click", async () => {
      const metaTags = getTags(tagList, "meta-tag-list");
      const artistTags = getTags(tagList, "artist-tag-list");
      const copyrightTags = getTags(tagList, "copyright-tag-list");
      const characterTags = getTags(tagList, "character-tag-list");
      const generalTags = getTags(tagList, "general-tag-list");
      const rating = getRating();
      const lines = [
        trailing(metaTags.join(", ")),
        // meta
        trailing(rating),
        // rating
        trailing([...characterTags, ...copyrightTags].join(", ")),
        // character + copyright
        trailing(artistTags.map((t) => "@" + t).join(", ")),
        // @artist
        trailing(generalTags.join(", "))
        // general
      ];
      const text = lines.join("\n");
      try {
        await copyToClipboard(text);
        msg.style.display = "inline";
        setTimeout(() => {
          msg.style.display = "none";
        }, 1500);
      } catch {
        alert("Copy failed");
      }
    });
    console.log("[Danbooru Tag Copy] Ready");
  }
  __name(main, "main");
  main();
})();