MB: Artist Credit Splitter

TODO_WRITE_DESCRIPTION

当前为 2022-08-30 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        MB: Artist Credit Splitter
// @version     1.0
// @description TODO_WRITE_DESCRIPTION
// @namespace   https://rinsuki.net/
// @author      rinsuki
// @match       https://musicbrainz.org/*
// @grant       none
// ==/UserScript==

(function () {
    'use strict';

    function getReactContainer(elem) {
        const properties = Object.getOwnPropertyNames(elem);
        const name = properties.find(x => x.startsWith("__reactContainer$"));
        if (name != null)
            return elem[name];
    }

    function waitDOMByObserve(root, check, options) {
        const firstRes = check();
        if (firstRes != null)
            return Promise.resolve(firstRes);
        return new Promise(resolve => {
            const observer = new MutationObserver(() => {
                const res = check();
                if (res != null) {
                    observer.disconnect();
                    resolve(res);
                }
            });
            observer.observe(root, {
                childList: true,
                subtree: options.subtree
            });
        });
    }

    const LOCALSTORAGE_KEY_COPIED_ARTIST_CREDIT = "copiedArtistCredit";
    (async () => {
        const bubble = await waitDOMByObserve(document.body, () => document.querySelector("#artist-credit-bubble"), { subtree: false });
        const buttons = await waitDOMByObserve(bubble, () => bubble.querySelector(".buttons"), { subtree: false });
        const button = document.createElement("button");
        button.type = "button";
        button.style.float = "left";
        button.textContent = "Split Automatically";
        button.addEventListener("click", async () => {
            const container = getReactContainer(bubble);
            if (container == null)
                return alert("Failed to get React container");
            const tbody = bubble.querySelector("tbody");
            if (tbody == null)
                return alert("Failed to get tbody");
            const props = container.memoizedState.element.props;
            console.log(props);
            const currentCredit = props.artistCredit.names.map(name => name.name + name.joinPhrase).join("");
            const RE = /([  ]*(\((CV[.:.:] *)?(?=[^)]{3,})|(?<=[^(]{3})\)\/?|、| [&&] | feat[.: .: ] *)[  ]*)+/g;
            let splittedCredits = [];
            let lastIndex = 0;
            for (const match of currentCredit.matchAll(RE)) {
                splittedCredits.push([currentCredit.slice(lastIndex, match.index), match[0]]);
                lastIndex = match.index + match[0].length;
            }
            if (currentCredit.slice(lastIndex).length > 0)
                splittedCredits.push([currentCredit.slice(lastIndex), ""]);
            if (!confirm("次のように指定します。よろしいですか?\n\n" + JSON.stringify(splittedCredits, null, 4)))
                return;
            for (let i = props.artistCredit.names.length; i < splittedCredits.length; i++) {
                props.addName();
                await waitDOMByObserve(tbody, () => tbody.childNodes.item(i), { subtree: false });
            }
            // localStorage.removeItem(LOCALSTORAGE_KEY_COPIED_ARTIST_CREDIT)
            // let p = waitLocalStorage(LOCALSTORAGE_KEY_COPIED_ARTIST_CREDIT)
            // props.copyArtistCredit()
            // await p
            // const stubArtistCredit = JSON.parse(localStorage.getItem(LOCALSTORAGE_KEY_COPIED_ARTIST_CREDIT)!)
            localStorage.setItem(LOCALSTORAGE_KEY_COPIED_ARTIST_CREDIT, JSON.stringify(splittedCredits.map(([name, joinPhrase], i) => {
                return {
                    joinPhrase,
                    name,
                    // artist: {
                    //     entityType: "artist",
                    //     uniqueID: stubArtistCredit[i].artist.uniqueID,
                    //     name,
                    // }
                };
            })));
            props.pasteArtistCredit();
            alert("finish!");
        });
        buttons.appendChild(button);
    })();

}());