Danbooru

[UserScript] shortening child tags text

Posted under Bugs & Features

Exmaples
UserScript
// ==UserScript==
// @name         shortening child tags text
// @namespace    http://tampermonkey.net/
// @version      2024-03-28
// @description  Hide duplicate text in child tags (post pages only)
// @author       anonbl (https://danbooru.donmai.us/users/473183)
// @match        https://*.donmai.us/posts/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=donmai.us
// @grant        none
// @run-at       document-start
// @homepage     https://danbooru.donmai.us/forum_topics/26781
// @supportURL   https://danbooru.donmai.us/dmails/new?dmail%5Bto_id%5D=473183
// ==/UserScript==

(function() {
    'use strict';
    addEventListener("DOMContentLoaded", doMagick);

    function doMagick() {
        let tags = {};
        const tagLiList = document.querySelectorAll("#tag-list li[data-tag-name]");
        for(let tagLi of tagLiList) {
            tags[tagLi.parentElement.className] ??= [];
            if(tagLi.parentElement.className == "general-tag-list") continue;
            if(tagLi.parentElement.className == "meta-tag-list") continue;
            const tagLink = tagLi.querySelector("a.search-tag");
            const tagText = tagLink?.textContent;
            tagLink.title = tagText;
            const level = parseInt(tagLi?.getAttribute("class")?.match(/tag-nesting-level-(\d+)/)?.at(1) ?? 0);
            const prev = tags[tagLi.parentElement.className]?.at(-1) ?? [];
            let current = prev.slice(0, level);
            current.push(tagText);
            let newText = tagLink.innerHTML;
            for(let i = level-1; i >= 0 && level > 0; i--) { // Removing parent text
                newText = newText.replace(current[i], '').trim();
                newText = newText.replace(/^\(|\)$/g, '');
            }

            if(tagLi.parentElement.className == 'character-tag-list') { // Removing copytight text
                for(let copyright of tags['copyright-tag-list'].at(-1)) {
                    newText = newText.replaceAll(`(${copyright.replace(" (series)", '')})`, '').trim();
                }
            }
            tagLink.innerHTML = newText;
            tags[tagLi.parentElement.className].push(current);
        }
        //console.log(tags);
    }
})();
1