TJUPT Music JSON Uploady

import json from red/ops/dic

目前為 2024-04-10 提交的版本,檢視 最新版本

// ==UserScript==
// @name         TJUPT Music JSON Uploady
// @namespace    http://tjupt.org/
// @version      0.0.12
// @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;
        }
        const currentDesc = document.getElementById('descr');
        if(currentDesc && currentDesc.value == ""){
            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();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址