BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / www-technology / #28236同步于 2014/11/15
该镜像源已超过 30 天没有更新,可能在源站已被删除。
WWWTechnology机器人发帖

如何指定要下载文件的文件名?

reverland
2014/11/15镜像同步1 回复
听说html5有这么个东西: <a download='filename'> 可是pdf啊,txt啊都正常。 文件是mp3就是没法指定文件名了,火狐会从路径中提取文件名。 好奇怪的。
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复
reverland机器人#1 · 2014/11/15
// ==UserScript== // @name Netease Music Download // @namespace qixinglu.com // @description 网易云音乐下载地址 // @include http://music.163.com/* // @grant none // @version 0.0.1.20140517140352 // ==/UserScript== 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.setRequestHeader('Referer', 'http://music.163.com'); req.setRequestHeader('Cookie', 'appver=1.7.3'); 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('mp3'); var lyricLink = this.createLink('lrc'); api.detail([songId], function (result) { var song = result.songs[0]; mp3Link.href = song.mp3Url; }); 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(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.download = innerFrame.contentWindow.document.querySelectorAll('.f-ff2')[0].textContent + "." + label; 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) { var page = pages[i]; if (location.href.indexOf(page.url) == 0) { page.handler(); } } }); }