Greasy Fork

来自缓存

Greasy Fork is available in English.

咪咕音乐歌词下载助手

咪咕音乐歌词下载

当前为 2022-01-09 提交的版本,查看 最新版本

// ==UserScript==
// @name         咪咕音乐歌词下载助手
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  咪咕音乐歌词下载
// @author       jiajun
// @include      https://music.migu.cn/v3*
// @icon         https://www.google.com/s2/favicons?domain=migu.cn
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let _div = document.createElement('_div');
    // 生成面板内容
    let key_select = `<span>关键词:</span><input id="key_input" style="width:100px" value="" />
    <button id="search_btn">搜索</button></br>
    <span>选择歌曲:</span>
    <select id="search_select">
        <option value="123" selected="selected">请先搜索歌曲</option>
    </select>
    <button id="download_btn">下载歌词</button>`
    _div.innerHTML = '<div style="margin-top:20px">' + key_select + '</div>';
    // 生成控制面板
    let _groot = document.getElementsByTagName('html')[0];
    _groot.style.position = 'relactive';
    _div.style.backgroundColor = '#ffeaa7';
    _div.style.width = '250px';
    _div.style.height = '100px';
    _div.style.borderTopRightRadius = '10px';
    _div.style.borderBottomRightRadius = '10px';
    _div.style.position = 'fixed';
    _div.style.top = '40%';
    _div.style.left = '-230px';
    _div.style.zIndex = '999999';
    _div.style.transitionDuration = '0.6s';
    _div.style.transitionTimingFunction = 'easy-in-out';
    _div.onmouseover = () => {
        _div.style.left = "0";
    }
    _div.onmouseout = () => {
        _div.style.left = '-230px';
    }
    // 插入控制面板
    _groot.appendChild(_div);
    let key_input = document.getElementById('key_input');
    let search_select = document.getElementById('search_select');
    // 封装Xml
    const request = (type, url, params) => {
        return new Promise((resolve, reject) => {
            let paramsStr = "?";
            const newXml = new XMLHttpRequest();
            type.toLowerCase() === 'post' ? newXml.setRequestHeader('Content-type', 'application/x-www-form-urlencoded') : '';
            if (Object.keys(params).length > 0) {
                const keys = Object.keys(params);
                const values = Object.values(params);
                keys.map((value, index) => {
                    paramsStr += value + "=" + values[index];
                });
            }
            const newUrl = url + paramsStr;
            // 异步调用接口
            newXml.open(type, newUrl, true);
            newXml.send(paramsStr);
            newXml.onreadystatechange = () => {
                if (newXml.readyState === 4 && newXml.status === 200) {
                    resolve(JSON.parse(newXml.responseText));
                }
            }
        })
    };
    // 搜索关键词
    document.getElementById('search_btn').onclick = () => {
        let keyword = key_input.value;
        request('get', '/v3/api/search/suggest', { keyword }).then((res) => {
            let songsDom = '';
            res.data.songs.map((value, index, arr) => {
                let { copyrightId, name, singerName } = value;
                const songsName = name + '-' + singerName;
                songsDom += "<option value=" + copyrightId + ">" + songsName + "</option>";
            });
            search_select.innerHTML = songsDom;
        });
    };
    // 下载文件方法
    const downLoadFile = (fileName, fileContent) => {
        const tag_a = document.createElement('a');
        const blob = new Blob([fileContent]);
        const objUrl = URL.createObjectURL(blob);
        tag_a.href = objUrl;
        tag_a.download = fileName;
        tag_a.click();
        URL.revokeObjectURL(objUrl);
    };
    // 根据下拉所选下载歌词
    document.getElementById('download_btn').onclick = () => {
        //获取选中的下拉选项的index
        const index = search_select.selectedIndex;
        const copyrightId = search_select.options[index].value;
        const songsName = search_select.options[index].text;
        request('get', '/v3/api/music/audioPlayer/getLyric', { copyrightId }).then((res) => {
            // console.log(res.lyric);
            downLoadFile(songsName, res.lyric);
        })
    };
})();