BaiduPanFileList

统计百度盘文件(夹)数量大小. Thanks BaiduPanMD5Button

当前为 2014-10-15 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name       BaiduPanFileList
// @namespace  https://greasyfork.org/scripts/5128-baidupanfilelist/code/BaiduPanFileList.user.js
// @version    1.04
// @description  统计百度盘文件(夹)数量大小. Thanks BaiduPanMD5Button
// @match	http://pan.baidu.com/disk/home*
// @include	http://pan.baidu.com/disk/home*
// @grant GM_xmlhttpRequest
// @grant GM_setClipboard
// @run-at document-end
// @copyright  2014+, [email protected]
// ==/UserScript==

var url = document.URL;
var BTN_WAITING_TEXT = "統計檔案";
var BTN_RUNNING_TEXT = "處理中...";
var CLASS_NAME_APPEND_TO = "bar global-clearfix";
var BASE_URL_API = "http://pan.baidu.com/api/list?channel=chunlei&clienttype=0&web=1&dir=";
var REQ_HEADERS = {
    "Host" : document.domain,
    "Connection": "keep-alive",
    "Cache-Control" : "no-cache",
    "Pragma" : "no-cache",
    "Accept" : "application/json, text/javascript, */*; q=0.01",
    "X-Requested-With" : "XMLHttpRequest",
    "User-Agent" : navigator.userAgent,
    "Referer" : "http://pan.baidu.com/disk/home",
    "Accept-Encoding" : "gzip,deflate,sdch",
    "Accept-Language" : "zh,en;q=0.8,ja;q=0.6,ko;q=0.4,zh-CN;q=0.2",
    "Cookie" : document.cookie
}; // 备用

var global = (function() { return this || (1,eval)('(this)'); }()); //

// 按钮
var btn_curr = document.createElement("button");
btn_curr.type = "button";
btn_curr.style.cssText = 'margin: 0px 10px;height: 28px;';
btn_curr.innerHTML = BTN_WAITING_TEXT;
btn_curr.id = BTN_WAITING_TEXT;
btn_curr.disabled = false;
btn_curr.waiting_text = BTN_WAITING_TEXT;
btn_curr.running_text = BTN_RUNNING_TEXT;
btn_curr.error = false;
// 按钮单击
btn_curr.onclick = function(e){
    e = e||window.event;
    if(e.ctrlKey){
       showInfo(btn_curr, true);
    }else{
       showInfo(btn_curr, false);
    }
};

// 键盘, 确保在按钮添加失败时依旧可用
document.onkeydown = function(e){
    e = e||window.event;
    var key = e.keyCode||e.charCode;
    if(key == 81||key == 113){
        if(e.ctrlKey){
            showInfo(btn_curr, true);
        }else{
            showInfo(btn_curr, false);
        }
    }
}

// 自己的网盘添加按钮
if (url.indexOf("http://pan.baidu.com/disk/home") != -1) {
    var el_append_to = document.getElementsByClassName(CLASS_NAME_APPEND_TO)[0];
    if(!document.getElementById(BTN_WAITING_TEXT)){
        el_append_to.appendChild(btn_curr);
    }
}

// 处理按钮和快捷键
function showInfo(button, includeSubDir) {
    if(button.disabled||button.error){
        return;
    }
    showBtn(false);
    url = document.URL;
    while (url.indexOf("%25") != -1) {
        url = url.replace("%25", "%");
    }
    var listurl = BASE_URL_API; 
    var folder_access_times = 0;
    var currentDir = "";
    
    var str_alert = "";
    var num_all_files = 0;
    var num_all_folder = 0;
    var num_jpg = 0;
    var num_original = 0;
    var name_all = new Array();
    var size_all = 0;
    // 百度api
    // http://pan.baidu.com/api/list?channel=chunlei&clienttype=0&web=1&num=100&page=1&dir=<PATH>&order=time&desc=1&showempty=0&_=1404279060517&bdstoken=9c11ad34c365fb633fc249d71982968f&app_id=250528
    // 测试url
    // http://pan.baidu.com/disk/home#dir/path=<PATH>
    // http://pan.baidu.com/disk/home#from=share_pan_logo&path=<PATH>
    // http://pan.baidu.com/disk/home#key=<KEY>
    // http://pan.baidu.com/disk/home#path=<PATH>
    // http://pan.baidu.com/disk/home
    // http://pan.baidu.com/disk/home#path=<PATH>&key=<KEY>
    if (url.indexOf("path=") == -1) {
        listurl += "%2F";
        currentDir = "/";
        getList(listurl);
    } else if(url.indexOf("path=") != -1){
        var path = url.substring(url.indexOf("path=") + 5);
        if(path.indexOf("&") != -1){
            path = path.substring(0, path.indexOf("&"));
        }
        listurl += path;
        currentDir = decodeURIComponent(path);
        getList(listurl);
    }
    // 请求数据
    function getList(url) {
        if(button.error){
            return;
        }
        GM_xmlhttpRequest({
            method : 'GET',
            synchronous : false,
            url : url,
            timeout : 9999,
            onabort : function() {
                showError(decodeURIComponent(url.replace(BASE_URL_API, ""))
                          + "\n\n意外终止, 请刷新重试");
            },
            onerror : function() {
                showError(decodeURIComponent(url.replace(BASE_URL_API, ""))
                          + "\n\n未知错误, 请刷新重试");
            },
            ontimeout : function() {
                showError(decodeURIComponent(url.replace(BASE_URL_API, ""))
                          + "\n\n请求超时, 请刷新重试");
            },
            onload : function(reText) {
                var JSONobj = JSON.parse(reText.responseText);
                if (JSONobj.errno != 0) {
                    showError("读取目录: "
                              + decodeURIComponent(url.replace(BASE_URL_API, ""))
                              + "  失败, 错误码: " + JSONobj.errno)
                    return;
                }
                var size_list = JSONobj.list.length;
                var curr_item = null;
                for ( var i = 0; i < size_list; i++) {
                    curr_item = JSONobj.list[i];
                    if (curr_item.isdir == 1) {
                        num_all_folder++;
                        name_all.push(curr_item.path);
                        if (includeSubDir) {
                            folder_access_times++;
                            getList(BASE_URL_API
                                    + encodeURIComponent(curr_item.path));
                        }
                    } else {
                        num_all_files++;
                        if (curr_item.server_filename.indexOf(" (JPG).zip") != -1) {
                            num_jpg++;
                        } else if (curr_item.server_filename.indexOf(".zip") != -1) {
                            num_original++;
                        }
                        size_all += curr_item.size;
                        name_all.push(curr_item.path + "\t"
                                      + getReadableFileSizeString(curr_item.size)
                                      + "(" + curr_item.size + " Bytes)");
                    }
                }
                folder_access_times--;
                if (folder_access_times + 1 == 0) {
                    var CTL = "\r\n";
                    str_alert = currentDir + CTL + CTL
                    + "files: " + num_all_files + ", folders: " + num_all_folder + CTL
                    + "xxx (JPG).zip: " + num_jpg + CTL
                    + "xxx.zip: " + num_original + CTL 
                    + "size: " + getReadableFileSizeString(size_all) + "  ("+ size_all + " Bytes)" + CTL;
                    GM_setClipboard(str_alert + CTL + CTL
                                    + name_all.sort().join("\r\n") + "\r\n");
                    alert(str_alert.replace(/\r\n/g, "\n"));
                    showBtn(true);
                }
            }
        });
    }
    // 错误提示
    function showError(info){
        if(!button.error){
            button.error = true;
            alert(info);
        }
    }
    // 禁用启用按钮
    function showBtn(isDisplay) {
        if (isDisplay === true) {
            button.innerHTML = button.waiting_text;
            button.disabled = false;
        } else {
            button.disabled = true;
            button.innerHTML = button.running_text;
        }
    }
}

// 转换可读文件大小
function getReadableFileSizeString(fileSizeInBytes) {
    var i = 0;
    var byteUnits = [ ' Bytes', ' KiB', ' MiB', ' GiB', ' TiB', ' PiB', ' EiB',
                     ' ZiB', ' YiB' ];
    while (fileSizeInBytes >= 1024) {
        fileSizeInBytes = fileSizeInBytes / 1024;
        i++;
    }
    return fileSizeInBytes.toFixed(2) + byteUnits[i];
}