// ==UserScript== // @name 百度音乐下载 // @namespace thunderhit@163.com // @description bdmusicdownload // @author thunderhit // @include http://music.baidu.com/song/* // @require http://code.jquery.com/jquery-2.1.1.min.js // @version 1.02 // @grant GM_xmlhttpRequest // @run-at document-end // @downloadURL none // ==/UserScript== $(document) .ready(function () { main = { _bdiduid: '', get_bdiduid: function () { if (this._bdiduid == '') { this._bdiduid = document.cookie.substring(8, 40); } }, getsongid: function () { if (location.pathname.indexOf('download') != - 1) { document.body.write('
BD音乐下载助手告诉你,暂时不支持本页面
'); } else { return location.pathname.substr(6); } }, getcollection: function () { return 'http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.favorite.getCollectSong&format=json&from=bmpc&version=1.0.0&version_d=9.0.4.7&bdiduid=' + this._bdiduid + '&pn=0&rn=50'; }, favorite: function (songid) { return 'http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.favorite.addSongFavorite&format=json&from=bmpc&version=1.0.0&version_d=9.0.4.7&baiduid=' + this.bdiduid + '&songId=' + songid + '&time=' + (Math.round(new Date() .getTime() / 1000)); }, downloadurl: function (songid, rate) { //假设无损码率为999 //if (rate == '999') { // return 'http://yinyueyun.baidu.com/data/cloud/downloadsongfile?songIds=' + songid + '&rate=' + rate + '&format=flac'; //} return 'http://yinyueyun.baidu.com/data/cloud/downloadsongfile?songIds=' + songid + '&rate=' + rate + '&format=mp3'; } }; main.get_bdiduid(); if (main._bdiduid == '') return //克隆 var dlbutton = $('.add-song-btn') .clone(); $('.btn.btn-b.down-song-btn') .after(dlbutton); $(dlbutton) .addClass('bdmdl'); //替换图标 $('.bdmdl i') .removeClass('icon btn-icon-add'); $('.bdmdl i') .addClass('icon btn-icon-down'); $('.addlayer', $('.bdmdl')) .addClass('bdmdl-hovermenu'); $('.txt', $('.bdmdl')) .html('下载'); //移除原来标签 $('a', $('.bdmdl-hovermenu')) .remove(); $('.bdmdl-hovermenu') .append('128kbps256kbps320kbps '); //无损' $('.bdmdl') .hover(function () { $('.bdmdl-hovermenu') .toggle(); }); $('.bdmdl-rate') .click(function () { var songid = main.getsongid(); var t = $(this); t.text('获取中...'); var rate = t.attr('id'); //add favorite GM_xmlhttpRequest({ method: 'GET', url: main.favorite(songid), onload: function (response) { //延时执行,避免被识别 setTimeout(function () { t.text(rate + 'kbps'); var json = eval('(' + response.responseText + ')'); if (json.error_code == '22322') { location.assign(main.downloadurl(songid, rate)); } else { alert('好像发生了什么错误。'); } //等待时间 }, 6540); }, onerror: function (d) { alert('获取失败 #0'); }, }); }); });