// ==UserScript== // @name 网易云音乐直接下载 // @namespace https://www.ocrosoft.com/ // @description 显示歌曲、歌词、翻译、封面的下载链接,并以高音质试听该曲目。支持新版(2017/9/9)网易云。该脚本受 GPL V2.0 许可证保护。 // @match *://music.163.com/* // @grant none // @version 1.4 // @require http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js // @downloadURL none // ==/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; 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'); //console.log(cov); 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.download = $(tit).children('.f-ff2')[0].innerText; coverLink.href = cov.attr('data-src') + ';'; api.detail([songId], function(result) { mp3Link.src = 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(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.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; }, }, ]; if (innerFrame) { innerFrame.addEventListener('load', function() { var i, page; for (i = 0; i < pages.length; i += 1) { page = pages[i]; if (location.href.split('://')[1].indexOf(page.url.split('://')[1]) === 0) { page.handler(); } } }); }