☰

Copy Danbooru Tags

Copy post tags from Danbooru

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Copy Danbooru Tags
// @namespace    http://tampermonkey.net/
// @version      2.5
// @description  Copy post tags from Danbooru
// @author       narr3309
// @match        https://danbooru.donmai.us/posts/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function createCopyButton(tagBox) {
        const li = document.createElement('li');
        li.id = 'post-option-copy-tags';

        const link = document.createElement('a');
        link.href = '#';
        link.textContent = 'Copy tags';
        link.title = 'Copy all tags to clipboard';
        link.style.cursor = 'pointer';

        link.addEventListener('click', function(e) {
            e.preventDefault();
            e.stopPropagation();

            const currentTagList = document.querySelector('.tag-list');
            if (!currentTagList) return;

            const tags = Array.from(currentTagList.querySelectorAll('li[data-tag-name]'))
            .map(el => el.textContent.trim())
            .filter(Boolean);

            if (!tags.length) return;

            const regex = new RegExp('^[^[a-z]--[\\n]]*', 'vgi');
            const regex2 = new RegExp('\\s\\d+\\w?(\\.\\d+\\w)?', 'gi');
            tags.forEach((value, index, arr) => {
                arr[index] = value.replace(regex, '').replace(regex2, '');
            });

            const text = tags.join(', ');

            if (navigator.clipboard && navigator.clipboard.writeText) {
                navigator.clipboard.writeText(text).then(console.log('tags copied!')).catch(() => fallbackCopy(text));
            } else {
                fallbackCopy(text);
            }
        });

        function fallbackCopy(text) {
            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');
                console.log('tags copied!');
            } catch (err) {
                console.log('copy failed!')();
            }
            document.body.removeChild(textarea);
        }

        li.appendChild(link);
        return li;
    }

    function injectButton() {
        const postOptions = document.querySelector('#post-options');
        const ul = postOptions?.querySelector('ul');

        if (!ul || document.querySelector('#post-option-copy-tags')) {
            return;
        }

        const newLi = createCopyButton();

        const favGroup = ul.querySelector('#post-option-resize-to-window');
        if (favGroup && favGroup.parentNode === ul) {
            favGroup.insertAdjacentElement('beforebegin', newLi);
        } else {
            ul.appendChild(newLi);
        }

        console.log("button injected!");
    }

    injectButton();

    const observer = new MutationObserver(() => {
        if (!document.querySelector('#post-option-copy-tags')) {
            injectButton();
        }
    });

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

})();