// ==UserScript== // @name 百度网盘空间占用分析 // @version 1 // @description 百度网盘磁盘占用分析,可视化图表的方式显示出来 // @include https://pan.baidu.com/disk/home* // @grant none // @namespace http://blog.formatfa.top //引入jquery // @require https://code.jquery.com/jquery-latest.js // @require https://cdnjs.cloudflare.com/ajax/libs/echarts/4.3.0/echarts.min.js // @downloadURL none // ==/UserScript== //这里用这个是匿名函数 (function(){ 'use strict'; // 字节转换为文件大小 function getSize(value) { let size = "unknow" if (value < 1024) { size = value + "b" } else if (value < 1024 * 1024) { size = Math.round(value / 1024) + "k" } else if (value < 1024 * 1024 * 1024) { size = Math.round(value / 1024 / 1024) + 'm' } else if (value < 1024 * 1024 * 1024 * 1024) { size = Math.round(value / 1024 / 1024 / 1024) + 'g ' } return size; } //下载文件,来自https://blog.csdn.net/weixin_33863087/article/details/93177956 function download(filename, result) { console.log("下载:") console.log(result) var text=JSON.stringify(result); /* result.forEach(function(value,index){ text+=(value+"\n") })*/ var element = document.createElement('a'); element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); element.setAttribute('download', filename); element.style.display = 'none'; document.body.appendChild(element); element.click(); document.body.removeChild(element); } //遍历获取文件列表,路径,文件夹名字,结果容器,当前深度, 扫描的最大深度 function collectFiles(path,name,result,nowDepth,maxDepth) { $("#process_text").text(path) //最大深度没定义时 不会判断,会一直扫描, 大于最大深度,不再扫描 if( maxDepth>0&& nowDepth>maxDepth) return //目录的名字 console.log("扫描:"+path) //同步请求,一次请求1000个文件,就不用处理翻页,除非有某个文件夹文件数量大于1000的 let res = $.ajax({ url:"https://pan.baidu.com/api/list?num=1000&order=time&desc=1&clienttype=0&showempty=0&web=1&page=1&channel=chunlei&web=1&app_id=250528", data:{ dir:path }, type:"get", //同步请求,接受数据 async : false }) let files = res.responseJSON.list //目录总大小,echarts的图,每个都要有value let dir_size = 0; let children=[]; //数量,用于显示进度 let count = files.length; files.forEach(function(value,index){ if(nowDepth==0) $("#analysis").text("扫描:"+index+"/"+count+" "+value.path) if(value.isdir==0) //添加到二级目录 { children.push( { name:value.server_filename, path:value.path, value:value.size }) dir_size+=value.size; } //文件夹的话,递归 if(value.isdir==1) { //添加到总的文件夹大小 dir_size += collectFiles(value.path,value.server_filename,children,nowDepth+1,maxDepth) } }) if(nowDepth==0)$("#analysis").text("空间占用分析") result.push({ name:name, path:path, children:children, value:dir_size }) return dir_size; } //插入图表,显示图表 function showChart(result) { console.log("显示图表...") console.log(result) //如果没有这个div,就插入,图表大小等 if($("#chartcontainer").length==0) $("body").prepend("