VGMdb 信息生成

VGMdb BBcode-style album information generator

当前为 2025-01-06 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         VGMdb Info Generator
// @name:zh-CN   VGMdb 信息生成
// @namespace    https://vgmdb.net/
// @version      0.1
// @description  VGMdb BBcode-style album information generator
// @description:zh-cn VGMdb BBcode 样式专辑信息生成
// @author       gkouen
// @license      MIT
// @homepage     https://blog.cya.moe/
// @match        *://vgmdb.net/album/*
// @icon         https://vgmdb.net/favicon.ico
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const discussSpan = document.querySelector('span.label.smallfont > span#albumtools');
    if (!discussSpan) return;

    const generateInfoButton = document.createElement('a');
    generateInfoButton.textContent = '生成信息';
    generateInfoButton.style.cursor = 'pointer';
    generateInfoButton.style.marginLeft = '0px';
    generateInfoButton.style.color = '#ceffff';
    generateInfoButton.addEventListener('click', (event) => {
        event.preventDefault();
        event.stopPropagation();
        generateInfo();
    });

    const separator = document.createTextNode(' | ');
    discussSpan.appendChild(separator);
    discussSpan.appendChild(generateInfoButton);

    function generateInfo() {
        try {
            const coverArt = document.querySelector('#coverart').style.backgroundImage.match(/url\("?(.*?)"?\)/)?.[1];
            const title = document.querySelector('.albumtitle[lang="ja"]').textContent.trim().replace(/^\s*\/|\/\s*$/g, '');
            const catalogNumber = getSiblingValue('Catalog Number');
            const releaseDate = getSiblingValue('Release Date');
            const formattedDate = formatDate(releaseDate);
            const tracklistRows = document.querySelectorAll('#tracklist tr.rolebit');
            const tracklist = Array.from(tracklistRows)
                .map(row => {
                    const trackNumber = row.querySelector('.label')?.textContent.trim();
                    const trackName = row.querySelector('td[width="100%"]')?.textContent.trim();
                    return `${trackNumber}\t${trackName}`;
                })
                .join('\n');

            const resultText = `[quote]
[img]${coverArt}[/img]
[b]Title:[/b]${title}
[b]Catalog number:[/b] ${catalogNumber}
[b]Release date:[/b] ${formattedDate}
[b]Tracklist[/b]:
[code]
${tracklist}
[/code]
[/quote]`;

            const modal = createModal(resultText);
            document.body.appendChild(modal);
        } catch (error) {
            console.error('生成信息失败:', error);
            alert('生成信息时出错,请查看控制台日志!');
        }
    }

    function getSiblingValue(labelText) {
        const labelCell = Array.from(document.querySelectorAll('#album_infobit_large td'))
            .find(td => td.textContent.trim() === labelText);
        return labelCell ? labelCell.nextElementSibling.textContent.trim() : '';
    }

    function formatDate(dateString) {
        const months = {
            Jan: '01', Feb: '02', Mar: '03', Apr: '04', May: '05', Jun: '06',
            Jul: '07', Aug: '08', Sep: '09', Oct: '10', Nov: '11', Dec: '12',
        };
        const [month, day, year] = dateString.split(' ');
        return `${year}/${months[month]}/${day.replace(',', '')}`;
    }

    function createModal(text) {
        const modal = document.createElement('div');
        modal.style.position = 'fixed';
        modal.style.top = '50%';
        modal.style.left = '50%';
        modal.style.transform = 'translate(-50%, -50%)';
        modal.style.backgroundColor = '#fff';
        modal.style.padding = '20px';
        modal.style.boxShadow = '0 2px 10px rgba(0,0,0,0.2)';
        modal.style.zIndex = '9999';

        const textarea = document.createElement('textarea');
        textarea.style.width = '500px';
        textarea.style.height = '300px';
        textarea.textContent = text;

        const closeButton = document.createElement('button');
        closeButton.textContent = 'Close';
        closeButton.style.marginTop = '10px';
        closeButton.addEventListener('click', () => {
            modal.remove();
        });

        modal.appendChild(textarea);
        modal.appendChild(closeButton);

        return modal;
    }
})();