百度云直接下载助手

直接下载百度云的文件,可以避免下载大文件和exe文件时调用云管家;只支持单个文件,不支持文件夹和多文件下载

Stan na 01-10-2016. Zobacz najnowsza wersja.

// ==UserScript==
// @name         百度云直接下载助手
// @namespace    undefined
// @version      0.1
// @description  直接下载百度云的文件,可以避免下载大文件和exe文件时调用云管家;只支持单个文件,不支持文件夹和多文件下载
// @author       Roger Zhu
// @match        http://pan.baidu.com/*
// @match        https://pan.baidu.com/*
// @match        http://yun.baidu.com/*
// @match        https://yun.baidu.com/*
// @require      http://code.jquery.com/jquery-latest.js
// @run-at       document-end
// @grant        none
// ==/UserScript==

var $ = $ || window.$;
var sign,fidlist=null,timestamp,tempurl,realurl,dir,bdstoken,fileid=null;
var listUrl = "https://pan.baidu.com/api/list";
var downloadUrl = "https://pan.baidu.com/api/download";

(function() {
    'use strict';
    $.ajaxSetup({async:false});
    addButton();
    createIframe();
    console.log('Baidu Yun Helper load successful!');
})();

//添加下载按钮
function addButton(){
    var $a = $('<a class="g-button" data-button-id="b200" data-button-index="200" href="javascript:void(0);"></a>');
    var $span = $('<span class="g-button-right"><em class="icon icon-download-gray" title="直接下载"></em><span class="text" style="width: auto;">直接下载</span></span>');
    $a.append($span);
    $a.click(function(){
        console.log('Direct download button clicked.');
        console.log('yunData:');
        console.log(window.yunData);
        
        dir = getDir();
        console.log("current dir:" + dir);
        
        sign = getSign();
        console.log('sign:'+sign);
        
        timestamp = getTimestamp();
        console.log('timestamp:'+timestamp);
        
        bdstoken = getBDStoken();
        console.log('bdstoken:'+bdstoken);
        
        logid = getLogid();
        console.log('logid:'+logid);
        
        fileid = getSelectFileId(dir,bdstoken,logid);
        console.log("fileid:"+fileid);
        
        if (fileid === null) {
            return;
        }
        
        fidlist = getFidList(fileid);
        console.log("fidlist:"+fidlist);
        
        if (fidlist.length === 0) {
            alert("获取文件ID失败,请重试!");
            return;
        }
        
        fidlist = "["+fidlist+"]";
        
        var link = getDownloadLink(downloadUrl,sign,timestamp,fidlist,bdstoken,logid);
        execDownload(link);
    });
    $($('div.list-header-operate div').get(1)).append($a);
}

function getSign(){
    var signFnc;
    try{
        signFnc = new Function("return " + yunData.sign2)();
    } catch(e){
        throw new Error(e.message);
    }
    return base64Encode(signFnc(yunData.sign5,yunData.sign1));
}

//获取当前目录
function getDir(){
    var url = window.location.href;
    return decodeURIComponent(url.substring(url.lastIndexOf('=')+1));
}

//获取当前目录的文件列表
function getFileList(listUrl,dir,bdstoken,logid){
    var filelist = null;
    var params = {
        dir:dir,
        bdstoken:bdstoken,
        logid:logid,
        num:100,
        order:'size',
        desc:0,
        clienttype:0,
        showempty:'0',
        web:1,
        page:1,
        channel:'chunlei',
        appid:250528
    };
    $.get(listUrl,params,function(result){
        filelist = result;
    });
    return filelist;
}

//获取选中文件的fileid
function getSelectFileId(dir,bdstoke,logid){
    var $dd = $('div.list-view dd.item-active');
    var fileid = null;
    if ($dd.length === 0) {
        alert("没有选中文件!");
        return null;
    } else if ($dd.length>1){
        alert("不要选中多个文件,只支持单个下载!");
        return null;
    }
    var $div = $($dd[0]).children('div');
    var type = $($div[0]).attr('class').split(" ");
    if(type[1] == "dir-small"){
        alert("不支持目录下载!");
        return null;
    }
    var $a = $('div.list-view dd.item-active div.file-name a.filename');
    var filename = $a[0].text;
    console.log("filename:"+filename);
    var filelist = getFileList(listUrl,dir,bdstoken,logid);
    if(filelist === null || filelist.errno !== 0){
        alert("获取文件列表失败,请重试!");
        return null;
    } else {
        for(var i=0;i<filelist.list.length;i++){
            if (filelist.list[i].server_filename === filename){
                fileid = filelist.list[i].fs_id;
            }
        }
    }
    return fileid;
}

function getFidList(fileid){
    var list = [];
    
    if(fileid !== null){
        list.push(fileid);
    }
    
    return list;
}

function getTimestamp(){
    return yunData.timestamp;
}

function getBDStoken(){
    return yunData.MYBDSTOKEN;
}

function getLogid(){
    var name = "BAIDUID";
    function getCookie(e) {
        var o, t;
        var n = window.document,c=window.decodeURI;
        return n.cookie.length > 0 && (o = n.cookie.indexOf(e + "="),-1 != o) ? (o = o + e.length + 1,t = n.cookie.indexOf(";", o),-1 == t && (t = n.cookie.length),c(n.cookie.substring(o, t))) : "";
    }
    var u = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/~!@#¥%……&";
    var d = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
    var f = String.fromCharCode;
    function l(e){
        if (e.length < 2) {
            var n = e.charCodeAt(0);
            return 128 > n ? e : 2048 > n ? f(192 | n >>> 6) + f(128 | 63 & n) : f(224 | n >>> 12 & 15) + f(128 | n >>> 6 & 63) + f(128 | 63 & n);
        }
        var n = 65536 + 1024 * (e.charCodeAt(0) - 55296) + (e.charCodeAt(1) - 56320);
        return f(240 | n >>> 18 & 7) + f(128 | n >>> 12 & 63) + f(128 | n >>> 6 & 63) + f(128 | 63 & n);
    }
    function g(e){
        return (e + "" + Math.random()).replace(d, l);
    }
    function m(e){
        var n = [0, 2, 1][e.length % 3];
        var t = e.charCodeAt(0) << 16 | (e.length > 1 ? e.charCodeAt(1) : 0) << 8 | (e.length > 2 ? e.charCodeAt(2) : 0);
        var o = [u.charAt(t >>> 18), u.charAt(t >>> 12 & 63), n >= 2 ? "=" : u.charAt(t >>> 6 & 63), n >= 1 ? "=" : u.charAt(63 & t)];
        return o.join("");
    }
    function h(e){
        return e.replace(/[\s\S]{1,3}/g, m);
    }
    function p(){
        return h(g((new Date()).getTime()));
    }
    function w(e,n){
        return n ? p(String(e)).replace(/[+\/]/g, function(e) {
            return "+" == e ? "-" : "_";
        }).replace(/=/g, "") : p(String(e));
    }
    return w(getCookie(name));
}

//获取下载地址。这个地址不是直接下载地址,访问这个地址会返回302,response header中的location才是真实下载地址
//暂时没有找到提取方法
function getDownloadLink(downloadUrl,sign,timestamp,fidlist,bdstoken,logid){
    var link = null;
    var params= {
        sign:sign,
        timestamp:timestamp,
        fidlist:fidlist,
        type:'dlink',
        channel:'chunlei',
        web:1,
        app_id:250528,
        bdstoken:bdstoken,
        logid:logid,
        clienttype:0
    };
    $.get(downloadUrl,params,function(result){
        link = result.dlink[0].dlink;
    });
    return link;
}

function execDownload(link){
    $('#helperdownloadiframe').attr('src',link);
}

function createIframe(){
    var $div = $('<div class="helper-hide" style="padding:0;margin:0;display:block"></div>');
    var $iframe = $('<iframe src="javascript:void(0)" id="helperdownloadiframe" style="display:none"></iframe>');
    $div.append($iframe);
    $('body').append($div);

}

function base64Encode(t){
    var a, r, e, n, i, s, o = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    for (e = t.length,r = 0,a = ""; e > r; ) {
        if (n = 255 & t.charCodeAt(r++),r == e) {
            a += o.charAt(n >> 2);
            a += o.charAt((3 & n) << 4);
            a += "==";
            break;
        }
        if (i = t.charCodeAt(r++),r == e) {
            a += o.charAt(n >> 2);
            a += o.charAt((3 & n) << 4 | (240 & i) >> 4);
            a += o.charAt((15 & i) << 2);
            a += "=";
            break;
        }
        s = t.charCodeAt(r++);
        a += o.charAt(n >> 2);
        a += o.charAt((3 & n) << 4 | (240 & i) >> 4);
        a += o.charAt((15 & i) << 2 | (192 & s) >> 6);
        a += o.charAt(63 & s);
    }
    return a;
}