Greasy Fork

Shined up real nice.

Greasy Fork is available in English.

网易云下载

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// ==UserScript==
// @name        网易云下载 
// @description  歌单里好多歌用以前的脚本都404了,于是简单修改了下。 
// @version   20171115
// @author         糖果君
// @include     http*://music.163.com/*
// @grant       unsafeWindow
// @grant        GM_xmlhttpRequest
// @grant        GM.xmlHttpRequest
// @require  https://greasyfork.org/scripts/26727-网易云音乐歌曲封面下载/code/网易云音乐歌曲封面下载.user.js
// @require https://greasyfork.org/scripts/34555-greasemonkey-4-polyfills/code/Greasemonkey%204%20Polyfills.js

// @namespace 
// ==/UserScript==
//参考 https://greasyfork.org/zh-CN/scripts/10548-网易云音乐下载
//以及更早的 https://greasyfork.org/zh-CN/scripts/1099-netease-music-download
var window=unsafeWindow||window.wrappedJSObject;
var api = {
    //参考 https://greasyfork.org/zh-CN/scripts/10582-网易云音乐高音质支持
    getTrackURL: function(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 = window.CryptoJS.MD5(byte3).toString(window.CryptoJS.enc.Base64).replace(/\//g, '_').replace(/\+/g, '-');
        var url = 'http://p2.music.126.net/' + results + '/' + byte2;
        return url;
    },
    autoSign: function (type, cookie) {
        if (document.cookie.indexOf(cookie) == - 1) {
            api.sign(type, function (result) {
                if (result.code == - 2 || result.code == 200) {
                    var cookieime = new Date();
                    cookieime.setTime(24 * 60 * 60 * 1000 + new Date(new Date() .toDateString()) .getTime() - 1);
                    document.cookie = cookie+ ';expires=' + cookieime.toGMTString();
                }
            });
        };
    },
    encrypt_request: function(callback, url, data) {
        // 这个好像不重要。var token = window.document.cookie.split('__csrf') [1].split(';') [0].substring(1); 
        var token = '';
        data.csrf_token = token;
        var req = new XMLHttpRequest();
        req.open('POST', window.location.origin+url+ token, true);
        req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        req.setRequestHeader('X-Real-IP', '27.38.4.87');
        //解决海外问题。
        req.onload = function() {
            callback(JSON.parse(this.responseText));
        };
        //参考  https://github.com/darknessomi/musicbox/wiki/网易云音乐新版WebAPI分析。
        var pubKey = '010001';
        var modulus = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7';
        var nonce = '0CoJUm6Qyw8W8jud';
        var result = window.asrsea(JSON.stringify(data), pubKey, modulus, nonce);
        req.send('params=' + encodeURIComponent(result.encText) + '&encSecKey=' + encodeURIComponent(result.encSecKey));
    },
    detail: function(songId, callback) {
        var url = '/weapi/v3/song/detail';
        var data = {
            c:
            JSON.stringify([{
                id: songId
            }
                           ])
        };
        this.encrypt_request(callback, url, data);
    },
    album: function (albumId, callback) {
        var url = '/api/album/' + albumId;
        var data = {
        };
        this.encrypt_request(callback, url, data);
    },
    lrc: function(songId, callback) {
        var url = '/weapi/song/lyric';
        var data = {
            id: songId,
            lv: -1,
            tv: -1
        };
        this.encrypt_request(callback, url, data);
    },
    newsong: function(songId, callback) {
        var url = '/weapi/song/enhance/player/url';
        var data = {
            ids: [songId],
            br: 999000,
        };
        this.encrypt_request(callback, url, data);
    },
    mv: function(mvId, callback) {
        var url = '/weapi/mv/detail/';
        var data = {
            id: mvId,
        };
        this.encrypt_request(callback, url, data);
    },
    search: function(songinfo, callback) {
        var url = '/weapi/search/pc';
        var data = {
            s: songinfo,
            limit: 1,
            type: 1,
            offset: 0,
        };
        this.encrypt_request(callback, url, data);
    },
    sign: function (type, callback) {
        var url = '/weapi/point/dailyTask';
        var data = {
            type: type
        };
        this.encrypt_request(callback, url, data);
    },
    xiamisearch: function (key, callback) {
        GM.xmlHttpRequest({
            method: 'GET',
            url: 'http://music-api-jwzcyzizya.now.sh/api/search/song/xiami?&limit=1&page=1&key=' + key,
            onload: function (response) {
                callback(JSON.parse(response.responseText));
            }
        });
    },
};
var innerFrame = document.querySelector('iframe');
var pages = [
    {
        url: '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 setUrlAndSize = function (mp3Link, Music, albumId,songinfo) {
                if (Music) {
                    var href = Music.url;
                    if (href) {
                        mp3Link.href = href;
                        mp3Link.text += (Music.size / 1024 / 1024) .toFixed(1) + 'M';
                        return ;
                    } 
                    else {
                    api.album(albumId, function (result) {
                            if (result.album && result.album.songs)
                            {
                                var songs = result.album.songs;
                                for (var i = 0; i < songs.length; i++)
                                {
                                    if (songs[i].id == songId) {
                                        var song = songs[i];
                                        var music = song.hMusic || song.mMusic || song.lMusic || song.bMusic;
                                        var mp3url;
                                        var dfsId = music.dfsId_str || music.dfsId;
                                        if (music && dfsId != 0) {
                                            mp3url = api.getTrackURL(dfsId) + '.mp3';
                                            mp3Link.text += (music.size / 1024 / 1024) .toFixed(1) + 'M(旧)';
                                        } else if (!song.mp3Url.endsWith('==/0.mp3')) {
                                            mp3url = song.mp3Url;
                                        }
                                    if (mp3url) {
                                            mp3Link.href = mp3url;
                                        }
                                    else {
                                    api.xiamisearch(songinfo, function (result) {
                                    if (result.songList) {
                                        console.log(result);
                                        mp3Link.href = result.songList[0].file;
                                        mp3Link.text += '(虾米)';
                                    } else {
                                        disableStyle(mp3Link);
                                    }
                                });
                            }
                                    }
                                }
                            } 

                        });
                    }
                }
            };
            var setLyric = function(LycLink, result) {
                var LrC = '';
                var lrc = result.lrc;
                var tlrc = result.tlyric;
                var num = 0;
                if (lrc && lrc.lyric) {
                    LrC += lrc.lyric + '\n';
                    num+=1;}
                if (tlrc && tlrc.lyric) {
                    LrC += tlrc.lyric;
                    num+=2;}


                if (num != 0) {
                    var html = '';
                    switch (num) {
                        case 1:html="(原)";break;
                        case 2:html="(译)";break;
                        case 3:html="(合)";break;
                    }
                    LycLink.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(LrC);
                    LycLink.innerHTML += html;
                } else {
                    disableStyle(LycLink);
                }
            };

            var newMp3Link = this.createLink('歌曲');
            var lyricLink = this.createLink('歌词');
            var mvLink = this.createLink('mv');
            var picLink = this.createLink('封面');

            api.detail(songId, function (result) {
                var song = result.songs[0];
                if (song.mv) {
                    api.mv(song.mv, function (result) {
                        var mv = result.data.brs;
                        mvLink.href = mv[720] || mv[480] || mv[240];
                    });
                } 
                else {
                    disableStyle(mvLink);
                };

                if (song.al.pic_str || song.al.pic) {
                    var img = innerFrame.contentWindow.document.querySelector(".j-img");
                    picLink.href = img.dataset["src"] = api.getTrackURL(song.al.pic_str || song.al.pic);
                    img.src = img.dataset["src"] + "?param=130y130";
                } else {
                    disableStyle(picLink);
                }; 
                var alia = song.alia[0] ? '(' + song.alia + ')' : '';
                var ars = '-';
                for (var i = 0; i < song.ar.length; i++) {
                    ars += (song.ar) [i].name + '/';
                }
                var songinfo = song.name + alia + ars;
                var alid = song.al.id;
                api.newsong(songId, function (result) {
                    var song = result.data[0];
                    setUrlAndSize(newMp3Link, song, alid,songinfo);
                });
            });
            api.lrc(songId, function (result) {
                setLyric(lyricLink, result);
            });
            var container = this.createLineContainer('下载');
            container.appendChild(newMp3Link);
            container.appendChild(lyricLink);
            container.appendChild(mvLink);
            container.appendChild(picLink);

            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) != -1) {
                page.handler();
            }
        }
    });
}
document.cookie = 'os=linux';
api.autoSign(0,'appsign=true');
api.autoSign(1,'websign=true');