TJUPT Music JSON Uploady

import json from red/ops/dic

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         TJUPT Music JSON Uploady
// @namespace    http://tjupt.org/
// @version      0.0.13
// @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');
        //add torrent description
        const torrent_desc = he.decode(unicodeConvert(data.response?.torrent?.description || '').replace(/\\\//g, '/'));
        if(torrent_desc !== "") {descr = descr + "\n[quote]" +torrent_desc + "[/quote]\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();
})();