Greasy Fork is available in English.

网易云音乐直接下载

在单曲页面显示歌词、翻译、封面、歌曲下载链接并以高音质试听,同时支持歌单、专辑、等页面直接下载单曲。该脚本受 GPL V2.0 许可证保护。

Versione datata 2020-10-07 04:15:42 UTC. Vedi la nuova versione l'ultima versione.

// ==UserScript==
// @name        网易云音乐直接下载
// @namespace   https://www.ocrosoft.com/
// @description 在单曲页面显示歌词、翻译、封面、歌曲下载链接并以高音质试听,同时支持歌单、专辑、等页面直接下载单曲。该脚本受 GPL V2.0 许可证保护。
// @match       *://music.163.com/*
// @grant       none
// @version     1.7
// @require     http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js
// ==/UserScript==
/**
 * 修改自 https://greasyfork.org/zh-CN/scripts/10548-%E7%BD%91%E6%98%93%E4%BA%91%E9%9F%B3%E4%B9%90%E4%B8%8B%E8%BD%BD
 * API 源码: https://github.com/Ocrosoft/NetEase_OutChain
 * 此脚本开源,API开源,但请不要在未经允许的情况下在其他程序中调用此脚本中的API,在非程序调用且小流量的个人使用场景下可以使用。
**/

var api = {

    detailUrl: function(songIds) {
        var tpl = (location.href.indexOf('https')==-1?'http://music.ocrosoft.com/GetMusicLink.php?id=${songIds}':'https://www.ocrosoft.com/outchain/GetMusicLink.php?id=${songIds}');
        return tpl.replace('${songIds}', songIds.join(','));
    },

    detail: function(songIds, callback) {
        $.ajax({
            type: "get",
            url: this.detailUrl(songIds),
            dataType: "jsonp",
            success: function(json){
                callback(json);
            }
        });
    },

    mediaUrl: function(songId) {
        return (location.href.indexOf('https')==-1?'http':'https') + '://music.163.com/api/song/lyric?os=pc&id=' + songId + '&lv=-1&kv=-1&tv=-1';//503207093
    },

    media: function(songId, callback) {
        var req = new XMLHttpRequest();
        req.open('GET', this.mediaUrl(songId), true);
        req.onload = function() {
            callback(JSON.parse(this.responseText));
        };
        req.send();
    },

};

var innerFrame = document.querySelector('iframe');
var tit, cov, dl;

var pages = [
    {
        url: 'http://music.163.com/#/song?id=',
        handler: function() {
            var innerFrameDoc = innerFrame.contentWindow.document;
            var albumNode = innerFrameDoc.querySelectorAll('p.des.s-fc4')[1];
            tit = innerFrameDoc.querySelectorAll('.tit');
            cov = $(innerFrameDoc).find('.u-cover').children('img');
            dl = $(innerFrameDoc).find('.u-btni-dl');
            var parentNode = albumNode.parentNode;

            var songId = location.href.match(/id=([0-9]+)/)[1];
            var downloadLine = this.createDownloadLine(songId);

            parentNode.insertBefore(downloadLine, albumNode.nextElementSibling);
        },
        createDownloadLine: function(songId) {
            var disableStyle = function(link) {
                link.text += '(无)';
                link.style.color = 'gray';
                link.style.textDecoration = 'none';
                link.style.cursor = 'auto';
            };

            var mp3Link = this.createMP3Link();
            var mp3Help = this.createLink('如何下载歌曲?');
            mp3Help.href = 'javascript:;';
            $(mp3Help).click(function(){alert("点击下方播放器最右侧的下载按钮");});
            var mp3Error = this.createLink('歌曲下载错误?');
            mp3Error.href = 'javascript:;';
            $(mp3Error).click(function(){alert("下方播放器如果不显示播放时长说明无法下载");});
            var lyricLink = this.createLink('歌词');
            var tlyricLink = this.createLink('翻译');
            var coverLink = this.createLink('封面');
            $(coverLink).addClass('des covdl');
            coverLink.download = $(tit).children('.f-ff2')[0].innerText;
            coverLink.href = cov.attr('data-src') + ';';
            var coverHelp = this.createLink('封面问题?');
            coverHelp.href = 'javascript:;';
            $(coverHelp).click(function(){alert("浏览器限制,下载完成后需要手动添加.jpg后缀名;\n可安装“网易云音乐歌曲封面下载”脚本");});

            api.detail([songId], function(result) {
                mp3Link.src = result.mp3Url;
                dl.attr({'data-res-action':'','download':'mp3','href': result.mp3Url + ';'});
            });
            api.media(songId, function(result) {
                if (result.lrc.lyric) {
                    lyricLink.download = $(tit).children('.f-ff2')[0].innerText + '.lrc';
                    lyricLink.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(result.lrc.lyric);
                } else {
                    disableStyle(lyricLink);
                }
                if(result.tlyric.lyric){
                    tlyricLink.download = $(tit).children('.f-ff2')[0].innerText + '(中).lrc';
                    tlyricLink.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(result.tlyric.lyric);
                } else {
                    disableStyle(tlyricLink);
                }
            });

            var container = this.createLineContainer('下载');
            container.appendChild(lyricLink);
            container.appendChild(tlyricLink);
            container.appendChild(coverLink);
            container.appendChild(mp3Help);
            container.appendChild(mp3Error);
            container.appendChild(coverHelp);
            container.appendChild(document.createElement('br'));
            container.appendChild(mp3Link);
            return container;
        },
        createLink: function(label) {
            var link = document.createElement('a');
            link.innerHTML = label;
            link.className = 's-fc7';
            link.style.marginRight = '10px';
            link.href = 'javascript:;';
            return link;
        },
        createMP3Link: function() {
            var link = document.createElement('audio');
            link.controls = 'true';
            link.id = 'wyyyydd';
            link.innerHTML = '浏览器不支持';
            return link;
        },
        createLineContainer: function(label) {
            var container = document.createElement('p');
            container.className = 'desc s-fc4';
            container.innerHTML = label + ':';
            container.style.margin = '10px 0';
            return container;
        },
    },
    {
        url: ['http://music.163.com/#/playlist?id=', // 歌单
              'http://music.163.com/#/artist?id=', // 歌手
              'http://music.163.com/#/discover/toplist', // 榜单
              'http://music.163.com/#/album?id=', // 专辑
              'http://music.163.com/#/discover/recommend/taste' // 每日推荐
             ],
        handler: function(){
            var innerFrameDoc = innerFrame.contentWindow.document;
            var downloadButtons = $(innerFrameDoc).find('span.icn-dl');

            this.replaceAction(innerFrameDoc, downloadButtons);
        },
        replaceAction: function(innerFrameDoc, downloadButtons){
            for(var i=0;i<downloadButtons.length;i++){
                if($(downloadButtons[i]).attr('data-res-action') == 'download'){
                    $(downloadButtons[i]).attr('data-res-action','downloadDirect');
                    $(downloadButtons[i]).click(function(){
                        var id = $(this).attr('data-res-id');
                        api.detail([id], function(res){
                            var audio = document.createElement('audio');
                            audio.src = res.mp3Url;
                            audio.id = 'wyyyydda';
                            $(audio).bind('canplay',function(){
                                var a = document.createElement('a');
                                a.href = this.src;
                                a.download = 'mp3';
                                a.click();
                            });
                            $(audio).bind('error',function(){
                                alert('加载歌曲错误,请反馈相关信息。');
                            });
                        });
                    });
                }
            }
        }
    },
];

function matchPagesURL(href,urls){
    var ret = false;
    var t = location.href.split('://')[1];
    if(Array.isArray(urls)){
        urls.forEach(function(ele){
            if(t.indexOf(ele.split('://')[1]) === 0){
                ret = true;
                return;
            }
        });
    } else {
        if(t.indexOf(urls.split('://')[1]) === 0){
            ret = true;
        }
    }
    return ret;
}

if (innerFrame) {
    innerFrame.addEventListener('load', function() {
        var i, page;
        for (i = 0; i < pages.length; i += 1) {
            page = pages[i];
            if (matchPagesURL(location.href, page.url)) {
                page.handler();
                setTimeout(function(){
                    var covp = $(innerFrame.contentWindow.document).find('.covdl').parent();
                    var covs = covp.children('.des');
                    if(covs.length != 2) return;
                    $(covs[1]).css('margin-right','10px');
                    $(covs[1]).addClass('covdl');
                    covp[0].replaceChild(covs[1],covs[0]);
                },55);
                setTimeout(function(){
                    var covp = $(innerFrame.contentWindow.document).find('.covdl').parent();
                    var covs = covp.children('.des');
                    $(covs[1]).remove();
                },2222);
            }
        }
    });
}