TJUPT Music JSON Uploady

import json from red/ops/dic

当前为 2024-04-10 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         TJUPT Music JSON Uploady
// @namespace    http://tjupt.org/
// @version      0.0.11
// @description  import json from red/ops/dic
// @author       Colder
// @match        https://*.tjupt.org/upload.php
// @grant        none
// @require      https://cdn.jsdelivr.net/npm/[email protected]/he.js
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function replaceUnsupportedBBCode(BB) {
        BB = BB.replace(/\[artist\](.*?)\[\/artist\]/g, '$1');
        BB = BB.replace(/\[plain\](.*?)\[\/plain\]/g, '$1');
        BB = BB.replace(/\[align=center\](.*?)\[\/align\]/g, '[center]$1[/center]');
        BB = BB.replace(/\[align(.*?)\]/g, '').replace(/\[\/align\]/g, '');
        BB = BB.replace(/\[pad(.*?)\]/g, '').replace(/\[\/pad\]/g, '');

        return BB;
    }

    function unicodeConvert(string) {
        string = string.replace(/\\u([\d\w]{4})/gi, (match, grp) => String.fromCharCode(parseInt(grp, 16)));
        string = string.replace(/&#(\d+);/g, (match, dec) => String.fromCharCode(dec));
        return string;
    }

    function insertButtons() {
        const uploadButton = document.createElement('input');
        uploadButton.type = 'file';
        uploadButton.id = 'jsonFileUploader';
        uploadButton.accept = 'application/json';
        uploadButton.addEventListener('change', () => selectMusicCategory());

        const goButton = document.createElement('button');
        goButton.textContent = 'Go';
        goButton.type = 'button';
        goButton.addEventListener('click', processJsonFile);

        const targetLocation = document.evaluate('/html/body/table[2]/tbody/tr[2]/td/form/table/tbody/tr[1]/td', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        if (targetLocation) {
            targetLocation.appendChild(uploadButton);
            targetLocation.appendChild(goButton);
        }
    }

    function processJsonFile() {
        const fileInput = document.getElementById('jsonFileUploader');
        if (fileInput.files.length > 0) {
            const file = fileInput.files[0];
            const reader = new FileReader();
            reader.onload = (e) => {
                try {
                    const data = JSON.parse(e.target.result);
                    fillForm(data);
                } catch(error) {
                    console.error('Error processing JSON file:', error);
                }
            };
            reader.readAsText(file);
        }
    }

    function fillForm(data) {
        const albumTitle = unicodeConvert(String(data.response?.group?.name || ''));
        const remasterYear = String(data.response?.torrent?.remasterYear || '');
        const artistNames = data.response?.group?.musicInfo?.artists?.map(artist => unicodeConvert(artist.name)).join(' & ') || '';
        const media = unicodeConvert(data.response?.torrent?.media || '');
        const format = data.response?.torrent?.format || '';
        //encoding
        let encoding = unicodeConvert(data.response?.torrent?.encoding || '');
        if(encoding === "24bit Lossless") {
            encoding = "Hi-Res";
        } else if(encoding === "Lossless") {
            encoding = "无损";
        }
        //subHeading
        let subHeadingArray = [
            unicodeConvert(data.response?.torrent?.remasterRecordLabel),
            unicodeConvert(data.response?.torrent?.remasterCatalogueNumber),
            unicodeConvert(data.response?.torrent?.remasterTitle)
        ];
        if (data.response?.torrent?.hasLog && data.response?.torrent?.logScore) {
            subHeadingArray.push(`log (${data.response.torrent.logScore}%)`);
        }
        if (data.response?.torrent?.hasCue) {
            subHeadingArray.push("Cue");
        }
        if (data.response?.torrent?.scene) {
            subHeadingArray.push("Scene");
        }
        const subHeading = subHeadingArray.filter(info => info).join(' / ');
        //description
        let descr = String('[img]'+(data.response?.group?.wikiImage || '').replace(/\\\//g, '/')+'[/img]\n');
        //TODO: parse wikiBody. bbBody is RED-only; wikiBBcode is OPS-only; currently not support DIC
        const bbBody = replaceUnsupportedBBCode(he.decode(unicodeConvert(data.response?.group?.bbBody || '')));
        const wikiBBcode = replaceUnsupportedBBCode(he.decode(unicodeConvert(data.response?.group?.wikiBBcode || '')));
        if (bbBody) {descr += bbBody;}
        else if (wikiBBcode) {descr += wikiBBcode;}
        //else descr += wikiBody;

        document.getElementById('hqname').value = albumTitle;
        document.getElementById('issuedate').value = remasterYear;
        document.getElementById('artist').value = artistNames;
        document.getElementById('specificcat').value = media;
        document.getElementById('format').value = format;
        document.getElementById('hqtone').value = encoding;
        const smallDescr = document.querySelector('input[type="text"][name="small_descr"]');
        if(smallDescr) {
            smallDescr.value = subHeading;
        }
        document.getElementById('descr').value = descr;
    }

    function selectMusicCategory() {
        const categorySelect = document.getElementById('browsecat');
        if (categorySelect && categorySelect.value !== "406") {
            categorySelect.value = "406";
            categorySelect.dispatchEvent(new Event('change'));
        }
    }

    insertButtons();
})();