Greasy Fork is available in English.

download163musiclist

用于下载网易云音乐网页版的歌单

// ==UserScript==
// @name         download163musiclist
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  用于下载网易云音乐网页版的歌单
// @author       xueque
// @license      MIT
// @match        *://music.163.com/*
// @require      https://cdn.bootcdn.net/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @run-at       document-start
// @connect      *
// ==/UserScript==
!function () {
    'use strict';

    // 获取歌单信息
    function get_playlist_information(dom) {
        const song_sheet = []
        // 获得歌单 table 的全部 tr 行
        const rows = dom.querySelector('tbody').rows
        //遍历取得歌曲信息,并格式化
        for (let i = 0; i < rows.length; i++) {
            const info = {}
            const td_list = rows[i].getElementsByTagName('td')
            info.id = td_list[0]?.innerText
            info.title = td_list[1]?.querySelector('b')?.title
            info.duration = td_list[2]?.querySelector('span')?.innerText
            info.singer = td_list[3]?.querySelector('div')?.title
            info.album = td_list[4]?.querySelector('a')?.title
            song_sheet.push(info)
        }
        return song_sheet
    }

    // 保存到文件
    function saveAsText(name, str) {
        // 定义保存的文件格式
        const blob = new Blob([str], {type: "text/plain;charset=utf-8"});
        // 保存到文件
        saveAs(blob, `${name}.text`);
    }

    // 构建歌单
    function download_playlist(dom) {
        // 定义歌单格式转换方案
        const format = (info) => `${info.id}: ${info.title} - ${info.singer}`
        // 转换歌单信息为字符串
        const list_str = get_playlist_information(dom).map(item => format(item)).join('')
        // console.log(list_str)
        // 得到歌单名
        const sheet_name = dom.querySelector('.f-ff2').innerText
        // console.log(sheet_name)
        saveAsText(sheet_name, list_str)
    }
    // 注入按钮
    function inject_button(dom) {
        // 得到元素
        let element = dom.querySelector('.u-title,.u-title-1.f-cb')
        // 注入一个下载歌单的按钮
        const download = document.createElement("button")
        download.innerHTML = '下载歌单'
        download.addEventListener('click', (e)=>{
            download_playlist(dom)
            e.preventDefault()
        })
        download.setAttribute('id', `my_download_button`)
        download.setAttribute('class', `more s-fc3 s-fc6`)
        download.setAttribute('style', `margin-right:15px`)
        element.appendChild(download)
    }
    function init(){
        // 定时检查页面
        let timer = setInterval(() => {
            // 未进入 playlist 页面等待下次执行
            const is_right_page = window.location.href.includes('playlist?')
            if (!is_right_page)return
            // 如果没有 iframe 的 window 对象,等待下次执行
            const topWin = window.top.document.getElementById('g_iframe').contentWindow
            if (topWin===null)return
            // 检查按钮是否存在
            let button = topWin.document.getElementById('my_download_button')
            // 不存在就添加一个按钮
            if (button===null) inject_button(topWin.document)
        }, 1000)
    }
    init()
}();