lrcDownloader[typing-tube.net]

Typing Tubeの編集画面にlrcファイルのダウンロードリンクを追加します。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         lrcDownloader[typing-tube.net]
// @namespace    TyepingTubeLrcDownloader
// @version      1.4
// @description  Typing Tubeの編集画面にlrcファイルのダウンロードリンクを追加します。
// @author       aetenotnk
// @match        https://typing-tube.net/movie/edit/*
// @grant        none
// ==/UserScript==

function CreateDownloadElement(){
    var editTab = $("#edit");

    if(!editTab.length){
        return false;
    }

    var downloadDiv = $("<div>");
    downloadDiv.addClass("row");
    downloadDiv.addClass("ml-2");
    downloadDiv.addClass("w-100");
    downloadDiv.attr("id", "lrcDownLoader");
    editTab.append(downloadDiv);

    var downloadLink = $("<a>");
    downloadLink.attr("id", "lrcDwonloadLink");
    downloadLink.attr("download", $("#title").attr("value") + ".lrc");
    downloadLink.attr("href", "dummy");
    downloadLink.addClass("col-2");
    downloadLink.text("lrcファイルをダウンロード");
    downloadLink.click(GetLyrics);

    downloadDiv.append(downloadLink);

    return true;
}

function SetDownloadLinkFile(text){
    var downloadLink = $("#lrcDwonloadLink");
    var file = new Blob([text], {type: "text/plain"});
    downloadLink.attr("href", URL.createObjectURL(file));
}

function GetLyrics(){
    var lyrics = $("#subtitles_table > tbody > tr");
    var lyricList = [];

    for(var i = 1; i < lyrics.length - 1; i++){
        var tds = $(lyrics[i]).find("td");
        var time = $(tds[0]).text().replace(" ", "");
        var lyric = $(tds[1]).text();

        lyricList.push({
            second: time,
            lyric: lyric
        });
    }

    SetDownloadLinkFile(FormatLRC(lyricList));
}

function FormatLRC(lyricList){
    var rowFormat = "[mm:ss.xx]lyric";
    var lines = [];

    for(var i = 0; i < lyricList.length; i++){
        var time = lyricList[i].second;
        var timeParts = time.split(".");
        var mm = ("00" + parseInt(parseInt(time) / 60)).slice(-2);
        var ss = ("00" + (parseInt(time) - mm * 60)).slice(-2);
        var xx = timeParts.length > 1 ? ("00" + timeParts[timeParts.length - 1]).slice(-2) : "00";

        lines.push(
            rowFormat
                .replace("mm", mm)
                .replace("ss", ss)
                .replace("xx", xx)
                .replace("lyric", lyricList[i].lyric));
    }

    return lines.join("\n");
}

CreateDownloadElement();