// ==UserScript== // @name BaiduPanFileList // @namespace https://greasyfork.org/scripts/5128-baidupanfilelist/code/BaiduPanFileList.user.js // @version 1.01 // @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, icgeass // @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 global = (function() { return this || (1,eval)('(this)'); }()); // // 按钮 var btn_curr = document.createElement("button"); btn_curr.innerHTML = OP_CURR; btn_curr.id = OP_CURR; btn_curr.type = "button"; btn_curr.onclick = showInfoCurr; btn_curr.style.cssText = 'margin: 0px 10px;height: 28px;'; btn_curr.op = OP_CURR; btn_curr.error=false; var btn_curr_sub = document.createElement("button"); btn_curr_sub.innerHTML = OP_CURR_SUB; btn_curr_sub.id = 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; btn_curr_sub.error=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_sub); }else{ showInfo(btn_curr); } } } // 自己的网盘添加按钮 if (url.indexOf("http://pan.baidu.com/disk/home") != -1) { if(!document.getElementById(OP_CURR)){ 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) { if(button.disabled||button.error){ return; } showBtn(button, false); url = document.URL; var listurl = ""; while (url.indexOf("%25") != -1) { url = url.replace("%25", "%"); } // 分析处理当前网页url 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 = "/"; 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)); getList(listurl); } // 请求数据 function getList(url) { GM_xmlhttpRequest({ method : 'GET', synchronous : false, url : url, timeout : 9999, onabort : function() { showError(decodeURIComponent(url.replace(base_url, "")) + "\n\n意外终止, 请刷新重试"); }, onerror : function() { showError(decodeURIComponent(url.replace(base_url, "")) + "\n\n未知错误, 请刷新重试"); }, ontimeout : function() { showError(decodeURIComponent(url.replace(base_url, "")) + "\n\n请求超时, 请刷新重试"); }, onload : function(reText) { var JSONobj = JSON.parse(reText.responseText); if (JSONobj.errno != 0) { showError("读取目录: " + 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 showError(info){ if(!button.error){ alert(info); button.error=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]; }