PlayList

下载你的歌单列表

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         PlayList
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  下载你的歌单列表
// @author       2222234
// @match        https://music.163.com/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=163.com
// @grant        none
// @require      https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js
// @license      GPL-3.0-only
// ==/UserScript==

(function() {
    var btn = document.createElement("input");
    btn.id = "btn";
    btn.type = "button";
    btn.value = "读取歌单";
    btn.style.position="absolute";
    btn.style.top="100px";
    document.body.appendChild(btn);

    btn.onclick=function(){
        var iframe = document.getElementsByTagName("iframe")[0];
        var iframeDocument = iframe.contentWindow.document;
        var url = iframe.contentWindow.location.href;
        const trs = iframeDocument.getElementsByTagName("tr");
        const name = iframeDocument.getElementsByClassName("f-ff2 f-thide")[0];
        if(trs.length == 0){
            alert("未在"+name.textContent+"中读取到歌曲");
            return;
        }
        var cf = confirm("在"+name.textContent+"中读取到"+(trs.length-1)+"首歌曲 是否下载歌曲列表");
        if(!cf){
            return;
        }
        const author = iframeDocument.getElementsByClassName("s-fc7");
        const real = author[author.length-1];
        const time = iframeDocument.getElementsByClassName("time s-fc4")[0];

        const titleElements = iframeDocument.getElementsByTagName("b");
        const timeElements = iframeDocument.getElementsByClassName("u-dur candel");
        const textElements = iframeDocument.getElementsByClassName("text");
        const txtElements = iframeDocument.getElementsByClassName("txt");

        var text = new Array();
        text.push(name.textContent+"\n"+
                  "由"+real.textContent+"于"+time.textContent+"\n\n");
        for(var i = 0;i<trs.length-1;i++){
            text.push((i+1)+"."+titleElements[i].getAttribute("title")+"\n"+
                      "  "+timeElements[i].textContent+"\n"+
                      "  "+textElements[i*2].getAttribute("title")+"\n"+
                      "  "+textElements[i*2+1].children[0].getAttribute("title")+"\n"+
                      "  "+txtElements[i].children[0].href+"\n\n");
        }
        text.push("链接:"+url);
        const data = new Blob(text, { type: 'text/plain' });
        saveAs(data, name.textContent+".txt");
    }
})();