Greasy Fork

Greasy Fork is available in English.

网易云音乐高音质下载 改自 网易云音乐下载,网易云音乐高音质支持

歌单里好多歌用以前的脚本都404了,于是简单修改了下。

当前为 2016-09-15 提交的版本,查看 最新版本

// ==UserScript==
// @name        网易云音乐高音质下载 改自 网易云音乐下载,网易云音乐高音质支持 
// @description  歌单里好多歌用以前的脚本都404了,于是简单修改了下。
// @version   20160916
// @namespace      
// @author         糖果君
// @include     http://music.163.com/*
// @require      https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/core-min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64-min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/md5-min.js
// @grant       none

// ==/UserScript==
//直接copy 网易云音乐高音质支持 这个脚本的代码 就把cdn从m换成p 因为m的我有很多歌都404了,p的只是把响应类型伪装为图片,
// 我的forefox要引入cryptojs才能用,不知道为什么。。但chrome没这问题。
function getTrackURL(dfsId) {
    var byte1 = '3go8&$8*3*3h0k(2)2';
    var byte2 = dfsId + '';
    var byte3 = [];
    for (var i = 0; i < byte2.length; i++) {
        byte3[i] = byte2.charCodeAt(i) ^ byte1.charCodeAt(i % byte1.length);
    }
    byte3 = byte3.map(function (i) {
        return String.fromCharCode(i);
    }) .join('');
    var results = CryptoJS.MD5(byte3) .toString(CryptoJS.enc.Base64) .replace(/\//g, '_') .replace(/\+/g, '-');
    var url = 'http://p2.music.126.net/' + results + '/' + byte2 + '.mp3';
    return url;
}
//直接copy 网易云音乐下载 这个脚本的代码  增加96k 160k 320k多个品质的下载。
var api = {
    detailUrl: function (songIds) {
        var tpl = 'http://music.163.com/api/song/detail?ids=[${songIds}]';
        return tpl.replace('${songIds}', songIds.join(','));
    },
    detail: function (songIds, callback) {
        var req = new XMLHttpRequest();
        req.open('GET', this.detailUrl(songIds), true);
        req.onload = function () {
            callback(JSON.parse(this.responseText));
        };
        req.send();
    },
    mediaUrl: function (songId) {
        return 'http://music.163.com/api/song/media?id=' + songId;
    },
    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 pages = [
    {
        url: 'http://music.163.com/#/song?id=',
        handler: function () {
            var songId = location.href.match(/id=([0-9]+)/) [1];
            var downloadLine = this.createDownloadLine(songId);
            var innerFrameDoc = innerFrame.contentWindow.document;
            var albumNode = innerFrameDoc.querySelectorAll('p.des.s-fc4') [1];
            var parentNode = albumNode.parentNode;
            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.createLink('歌曲默认');
            var mp3Linkl = this.createLink('低品质');
            var mp3Linkm = this.createLink('中品质');
            var mp3Linkh = this.createLink('高品质');
            var lyricLink = this.createLink('歌词');
            api.detail([songId], function (result) {
                var song = result.songs[0];
                var setUrlandSize = function (mp3Link, Music) {
                    if (Music&&Music.dfsId) {
                        mp3Link.href = getTrackURL(Music.dfsId);
                        mp3Link.text += (Music.size / 1024 / 1024) .toFixed(1) + 'M';
                    } else {
                        disableStyle(mp3Link);
                    }
                };
                if (song.mp3Url && !song.mp3Url.endsWith('==/0.mp3')) {
                    mp3Link.href = song.mp3Url;
                } else {
                    disableStyle(mp3Link);
                }
                setUrlandSize(mp3Linkl, song.lMusic);
                setUrlandSize(mp3Linkm, song.mMusic);
                setUrlandSize(mp3Linkh, song.hMusic);
            });
            api.media(songId, function (result) {
                if (result.lyric) {
                    lyricLink.href = 'data:text/plain;charset=utf-8,' +
                        encodeURIComponent(result.lyric);
                } else {
                    disableStyle(lyricLink);
                }
            });
            var container = this.createLineContainer('下载');
            container.appendChild(mp3Link);
            container.appendChild(mp3Linkl);
            container.appendChild(mp3Linkm);
            container.appendChild(mp3Linkh);
            container.appendChild(lyricLink);
            return container;
        },
        createLink: function (label) {
            var link = document.createElement('a');
            link.innerHTML = label;
            link.className = 's-fc7';
            link.style.marginRight = '10px';
            link.href = 'javascript:void(0);';
            link.target='_blank';
            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;
        },
    },
];
if (innerFrame) {
    innerFrame.addEventListener('load', function () {
        var i,
            page;
        for (i = 0; i < pages.length; i += 1) {
            page = pages[i];
            if (location.href.indexOf(page.url) === 0) {
                page.handler();
            }
        }
    });
}