-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaiduReplaceName.js
281 lines (267 loc) · 9.62 KB
/
BaiduReplaceName.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// ==UserScript==
// @name 百度网盘一键批量加广告&批量替换文件名
// @namespace dupanBatchRename
// @version 0.1.1
// @description 百度网盘一键批量加广告,批量替换文件名。【说明:批量改后缀强制改所有后缀,批量替换文件名可以替换一些垃圾版权信息】
// @author ding(AT)gong.si
// @match *://pan.baidu.com/disk/home*
// @match *://yun.baidu.com/disk/home*
// @require https://code.jquery.com/jquery-latest.min.js
// @run-at document-end
// @grant unsafeWindow
// @grant GM_addStyle
// @time 2018/08/12 11:00
// @author 二次修改 野草 www.jie668.cn qq1069422720
// @description 去掉了一健修改后缀 增加了一健加广告 文件和文件夹都可以。。。
// ==/UserScript==
//日志函数
var debug = false;
var log_count = 1;
function slog(c1,c2,c3){
c1 = c1?c1:'';
c2 = c2?c2:'';
c3 = c3?c3:'';
if(debug) console.log('#'+ log_count++ +'-ScriptLog:',c1,c2,c3);
}
var yunData,timestamp,bdstoken,logid;
var fileList=[];
var panAPIUrl = location.protocol + "//" + location.host + "/api/";
var restAPIUrl = location.protocol + "//pcs.baidu.com/rest/2.0/pcs/";
var clientAPIUrl = location.protocol + "//d.pcs.baidu.com/rest/2.0/pcs/";
var theext = 'mp4';
//获取当前目录
function getPath(){
var hash = location.hash;
var regx = /(^|&|\/)path=([^&]*)(&|$)/i;
var result = hash.match(regx);
return decodeURIComponent(result[2]);
}
function getCookie(e) {
var o, t;
var n = document,c=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))) : "";
}
function getLogID(){
var name = "BAIDUID";
var u = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/~!@#¥%……&";
var d = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
var f = String.fromCharCode;
function l(e){
var n;
if (e.length < 2) {
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);
}
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));
}
function getTimestamp(){
return yunData.timestamp;
}
function getBDStoken(){
return yunData.MYBDSTOKEN;
}
function getFileList(){
var filelist = [];
var listUrl = panAPIUrl + "list";
var path = getPath();
bdstoken = getBDStoken();
slog('path:',path);
logid = getLogID();
var params = {
dir:path,
bdstoken:bdstoken,
logid:logid,
order:'size',
desc:0,
clienttype:0,
showempty:0,
web:1,
channel:'chunlei',
appid:250528
};
$.ajax({
url:listUrl,
async:false,
method:'GET',
data:params,
success:function(response){
filelist = 0===response.errno ? response.list : [];
}
});
return filelist;
}
function rename(filelist){
// url = panAPIUrl + 'filemanager?opera=rename&async=2&channel=chunlei&web=1&app_id=250528&bdstoken='+ bdstoken +'&logid='+ logid +'&clienttype=0';
url = panAPIUrl + 'filemanager?opera=rename&async=2&onnest=fail&channel=chunlei&web=1&app_id=250528&bdstoken='+ bdstoken +'&logid='+ logid +'&clienttype=0';
var params = {
filelist:JSON.stringify(filelist),
};
$.ajax({
url:url,
method:'POST',
async:false,
data:params,
success:function(response){
slog('response :',response);
if(response.errno == 0){
alert('恭喜修改成功,共修改成功 ' + filelist.length + ' 个文件,刷新页面即可看到改变');
window.location.reload(true);
}else{
alert('修改失败,请重新登录再试。如果持续失败,可能是百度接口发生改变。');
}
}
});
}
// function do_rename(){
// var name = prompt("请输入要改成的后缀", "mp4");
// if (name){
// theext = name.split(".").pop();
// }else{
// return true;
// }
// if(fileList.length === 0){
// fileList = getFileList();
// }
// slog('fileList:',fileList);
// slog('fileList_Length:',fileList.length);
// if(fileList.length > 0){
// var toRename = [];
// $(fileList).each(function (i){
// slog('fileList '+ i +':',this.path);
// if(this.isdir == 1) return true; //跳过目录 2017-3-4 22:34:34
// fileNameArray = this.path.split(".");
// fileext = fileNameArray.pop();
// if(fileext == theext) return true;
// slog('FileExt :',fileext);
// fileNameArray.push(theext);
// newPath = fileNameArray.join('.');
// slog('newPath :',newPath);
// newName = newPath.split('/').pop();
// slog('newName :',newName);
// toRename.push({"path":this.path,"newname":newName});
// });
// slog('toRename :',toRename);
// if(toRename.length > 0){
// rename(toRename);
// }else{
// alert('好像你的后缀无需更改');
// }
// }else{
// alert('这个目录文件列表为空');
// }
// }
function do_replacename(){
var str_to_find = prompt("输入要替换的字符串(不能为空)", "");
if (!str_to_find){
return true;
}
var str_to_replace = prompt("替换成什么?(可以为空)", "");
if(fileList.length === 0){
fileList = getFileList();
}
slog('fileList:',fileList);
slog('fileList_Length:',fileList.length);
if(fileList.length > 0){
var toRename = [];
$(fileList).each(function (i){
slog('fileList '+ i +':',this.path);
//if(this.isdir == 1) return true; //跳过目录 2017-3-4 22:34:34
pathArray = this.path.split("/");
fileName = pathArray.pop();
var pattern=new RegExp(str_to_find);
if(pattern.test(fileName)){
newName = fileName.replace(str_to_find, str_to_replace);
slog('newName :',newName);
toRename.push({"path":this.path,"newname":newName});
}
});
slog('toRename :',toRename);
if(toRename.length > 0){
rename(toRename);
}else{
alert('好像没有替换');
}
}else{
alert('这个目录文件列表为空');
}
}
function do_addname() {
var add_to_text = prompt("文件加广告", "");
if(fileList.length === 0){
fileList = getFileList();
}
if (fileList.length > 0) {
var toRename = [];
$(fileList).each(function(i) {
slog('fileList ' + i + ':', this.path);
var pathArray = this.path.split("/");
var fileName = pathArray.pop();
if (this.isdir == 1) {
toRename.push({
"path": this.path,
"newname": fileName+add_to_text
});
} else {
var char = fileName.substring(0, fileName.lastIndexOf('.'));
var newName = fileName.replace(char, char + add_to_text);
toRename.push({
"path": this.path,
"newname": newName
});
}
});
console.log(toRename);
slog('toRename :', toRename);
if (toRename.length > 0) {
rename(toRename);
} else {
alert('好像没有替换');
}
} else {
alert('这个目录文件列表为空');
}
}
//入口函数
(function (){
//获取云盘数据
yunData = unsafeWindow.yunData;
slog('yunData:',yunData);
if(yunData === undefined){
slog('页面未正常加载,或者百度已经更新!');
return;
}
var q_sel = document.querySelectorAll('.icon-newfolder')[0].parentNode.parentNode.parentNode;
var ele_to_add2 = document.createElement('a');
ele_to_add2.className = "g-button";
ele_to_add2.onclick = do_replacename;
$(ele_to_add2).html('<span class="g-button-right"><em class="icon icon-edit" title="批量替换文件名"></em><span class="text" style="width: auto;">批量替换文件名</span></span>');
q_sel.appendChild(ele_to_add2);
var ele_to_add3 = document.createElement('a');
ele_to_add3.className = 'g-button';
$(ele_to_add3).html('<span class="g-button-right"><em class="icon icon-edit" title="批量广告"></em><span class="text" style="width: auto;">批量广告</span></span>');
q_sel.appendChild(ele_to_add3);
ele_to_add3.onclick = do_addname;
})();