// ==UserScript== // @name BaiduPanFileList // @namespace http://use.i.E.your.homepage/ // @version 0.1 // @description 统计百度盘文件(夹)数量大小. Thanks BaiduPanMD5Button, http://userscripts.org/scripts/show/156906 // @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 2012+, You // @downloadURL none // ==/UserScript== var url = document.URL; var OP_CURR = "統計檔案"; var OP_CURR_SUB = "統計檔案(含子目錄)"; var CLASS_NAME_APPEND_TO = "bar global-clearfix"; 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 btn_curr = document.createElement("button"); btn_curr.innerHTML = OP_CURR; btn_curr.type = "button"; btn_curr.onclick = showInfoCurr; btn_curr.style.cssText = 'margin: 0px 10px;height: 28px;'; btn_curr.op = OP_CURR; var btn_curr_sub = document.createElement("button"); btn_curr_sub.innerHTML = OP_CURR_SUB; btn_curr_sub.type = "button"; btn_curr_sub.onclick = showInfoCurrSub; btn_curr_sub.style.cssText = 'margin: 0px 10px;height: 28px;'; btn_curr_sub.op = OP_CURR_SUB; // 自己的網盤 if (url.indexOf("http://pan.baidu.com/disk/home") != -1) { var el_append_to = document.getElementsByClassName(CLASS_NAME_APPEND_TO)[0]; el_append_to.appendChild(btn_curr); el_append_to.appendChild(btn_curr_sub); } function showInfoCurr() { showInfo(btn_curr); } function showInfoCurrSub() { showInfo(btn_curr_sub); } /** * 點擊按鈕時執行的動作 */ function showInfo(button) { url = document.URL; var listurl = ""; // 請求檔案列表的URL while (url.indexOf("%25") != -1) { url = url.replace("%25", "%"); } // 自己的網盤 if (url.indexOf("http://pan.baidu.com/disk/home#") != -1) { if (url.indexOf("#dir/path=") != -1) { // http://pan.baidu.com/disk/home#dir/path= url = url.replace("dir/", ""); } else if (url.indexOf("&path=") != -1) { // http://pan.baidu.com/disk/home#from=share_pan_logo&path= // http://pan.baidu.com/disk/home#from=share_pan_logo&key=&path= url = url.substring(0, url.indexOf("#") + 1) + url.substring(url.indexOf("&path=") + 1); } else if (url.indexOf("path=") == -1) { // http://pan.baidu.com/disk/home#from=share_pan_logo // http://pan.baidu.com/disk/home#key= url = url.substring(0, url.indexOf("#") + 1); } else { // http://pan.baidu.com/disk/home#path= } } else { // http://pan.baidu.com/disk/home } 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; var base_url = "http://pan.baidu.com/api/list?channel=chunlei&clienttype=0&web=1&page=1&dir="; var CTL = "\r\n"; var folder_access_times = 0; var currentDir = ""; // http://pan.baidu.com/api/list?channel=chunlei&clienttype=0&web=1&num=100&page=1&dir=&order=time&desc=1&showempty=0&_=1404279060517&bdstoken=9c11ad34c365fb633fc249d71982968f&app_id=250528 if (url == "http://pan.baidu.com/disk/home" || url == "http://pan.baidu.com/disk/home#path=%2F" || url == "http://pan.baidu.com/disk/home#" || url.indexOf("path=") == -1) { listurl += base_url; listurl += "%2f"; currentDir = "/"; showBtn(button, false); getList(listurl); } else if (url.indexOf("pan.baidu.com/disk/home#path=") != -1 || url.indexOf("path=") != -1) { listurl += base_url; listurl += url.substring(url.indexOf("path=") + 5); currentDir = decodeURIComponent(url.substring(url.indexOf("path=") + 5)); showBtn(button, false); getList(listurl); } // 取得檔案列表 function getList(url) { GM_xmlhttpRequest({ method : 'GET', synchronous : false, url : url, timeout : 16666, onabort : function() { alert(decodeURIComponent(url.replace(base_url, "")) + "\n\n意外终止, 请刷新重试"); return; }, onerror : function() { alert(decodeURIComponent(url.replace(base_url, "")) + "\n\n未知错误, 请刷新重试"); return; }, ontimeout : function() { alert(decodeURIComponent(url.replace(base_url, "")) + "\n\n请求超时, 请刷新重试"); return; }, onload : function(reText) { var JSONobj = JSON.parse(reText.responseText); if (JSONobj.errno != 0) { alert("读取目录: " + decodeURIComponent(url.replace(base_url, "")) + " 失败, 错误码: " + 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 (button.op == OP_CURR_SUB) { folder_access_times++; getList(base_url + 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) { str_alert = currentDir + 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)"; GM_setClipboard(str_alert + "\r\n\r\n" + CTL + name_all.sort().join("\r\n") + "\r\n"); alert(str_alert.replace("\r\n", "\n\n").replace(/\r\n/g, "\n")); showBtn(button, true); } } }); } } function showBtn(button, isDisplay) { if (isDisplay === false) { button.disabled = true; button.innerHTML = "处理中..."; } else { button.innerHTML = button.op; button.disabled = false; } } 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]; }