forked from ywzhaiqi/userscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaiduPanDownloadHelper.user.js
1327 lines (1159 loc) · 45.3 KB
/
BaiduPanDownloadHelper.user.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @id baidupan@[email protected]
// @name BaiduPanDownloadHelper
// @version 3.7.9
// @namespace https://github.com/ywzhaiqi
// @author ywzhaiqi
// @description 批量导出百度盘的下载链接
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_addStyle
// @grant GM_setClipboard
// @grant GM_openInTab
// @grant GM_xmlhttpRequest
// @grant GM_registerMenuCommand
// @grant GM_deleteValue
// homepageURL http://userscripts.org/scripts/show/162138
// updateURL http://userscripts.org/scripts/source/162138.meta.js
// downloadURL http://userscripts.org/scripts/source/162138.user.js
// @homepageURL https://greasyfork.org/scripts/294/
// @require http://code.jquery.com/jquery-2.1.1.min.js
// 兼容 GM 1.x, 2.x
// @require https://greasyfork.org/scripts/2599/code/gm2_port_v102.js
// @include http*://yun.baidu.com/share/link*
// @include http*://pan.baidu.com/share/link*
// @include http*://yun.baidu.com/s/*
// @include http*://pan.baidu.com/s/*
// @include http*://yun.baidu.com/share/home*
// @include http*://yun.baidu.com/pcloud/album/info*
// @include http*://yun.baidu.com/pcloud/album/file*
// @include http*://pan.baidu.com/disk/home*
// @include http*://yun.baidu.com/share/init?*
// @include http*://pan.baidu.com/share/init?*
// @exclude http*://yun.baidu.com/share/home*&view=follow
// @run-at document-end
// ==/UserScript==
var isChrome = !!this.chrome;
var Config = { // 默认的设置
debug: false,
trim_titles: [ // Share Home 标题移除的文字广告
"[v.BDpan.COM]"
],
YAAW_UA: "user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.73.11 (KHTML, like Gecko) Version/7.0.1 Safari/537.73.11",
YAAW_REF: "Referer:http://pan.baidu.com/disk/home",
YAAW_COOKIE: "Cookie:" + document.cookie.replace(/; /g, ';'),
lineBreak: isChrome ? '\r' : '\n',
};
var TPLS = {
normal: '{dlink}',
aria2c: 'aria2c -c -x 10 -s 10 --out "{server_filename}" "{dlink}" --user-agent="netdisk" ' +
'--header="Cookie:' + document.cookie.replace(/; /g, ';') + '"',
};
var debug = Config.debug ? console.debug.bind(console) : function() {};
// 最新的改版,在个人主页页面已失效,改用下面的 require
var FileUtils = unsafeWindow.FileUtils,
Utilities = unsafeWindow.Utilities,
disk = unsafeWindow.disk,
Page = unsafeWindow.Page;
// 个人主页存在,其它页面可能不存在
var require = unsafeWindow.require;
var Utils = {
r1: function(reg, str) {
var m = str.match(reg);
return m ? m[1] : null;
},
getParam: function(name, url) {
var regexp = new RegExp("(?:^|\\?|#|&)" + name + "=([^&#]*)(?:$|&|#)", "i"),
matches = regexp.exec(url || location.href);
return matches ? decodeURIComponent(matches[1]) : ""
},
};
var mHome = (function(){ // 个人主页
if (!require) return;
var
dataCenter = require("common:widget/data-center/data-center.js"),
commonService = require("common:widget/commonService/commonService.js"),
Toast = require("common:widget/toast/toast.js"),
panel
;
var Test = {
getList: function (dir, callback) { // Search: disk-home.js
var API_URL = '/api/list?channel=chunlei&clienttype=0&web=1&num=100&order=time&desc=1',
restUrl = API_URL + '&dir=' + (dir ? dir : Utils.getParam('path'));
$.get(restUrl, function(result) {
if (result && result.errno == 0 && result.list) {
callback(result.list);
} else {
console.error("获取数据出错, " + restUrl);
}
});
},
getDlink: function(fs_ids) { // not finished
if (fs_ids.length === 0) {
Toast.obtain.setVisible(false);
return;
}
var restUrl = 'http://pan.baidu.com/api/download?channel=chunlei&clienttype=0&web=1&bdstoken=' + FileUtils.bdstoken,
data = {
sign: FileUtils.base64Encode(FileUtils.sign2(FileUtils.sign3, FileUtils.sign1)),
fidlist: "[" + fs_ids.join(',') + "]",
type: 'dlink',
timestamp: FileUtils.timestamp
};
$.post(restUrl, data, function(result){
if (result.errno == 0) {
var dlinkMap = {};
result.dlink.forEach(function(i){
dlinkMap[i.fs_id] = i.dlink;
});
debug("全部获取完成, result=", result, ", dlinkMap=", dlinkMap, ", checkedItems=", self.checkedItems);
self.showPanel(self.checkedItems, dlinkMap);
self.toast.setVisible(false);
} else {
console.error('POST 方式获取错误', result);
Utilities.useToast({
toastMode: disk.ui.Toast.MODE_CAUTION,
msg: disk.util.shareErrorMessage[result.errno],
sticky: false
});
}
});
},
test: function() {
// var diskHome = require("clouddisk-ui:widget/list-view/disk-home.js");
// diskHome.download(["550160357"]);
// diskHome.move(["550160357"], callback);
// diskHome.unZip(["550160357"]);
// dataCenter.get('selectedItemList'); // [s.fn.s.init[1], s.fn.s.init[1]]
/*
可设置 disk.DEBUG = true
*/
/* 最新的改版已失效
关键函数
移动:FileUtils.sendMoveCopyFileMessage
FileUtils.switchToDir(PATH) 主界面切换到某个文件夹
*/
},
// 脱离百度盘内部的调用
// $container: $('#yao-main > div > div.module-list-view > div.list-view-home > div.list'),
// getCheckedItems: function() {
// var e = [],
// s = this;
// return this.$container.find(".item").each(function() {
// var t = $(this);
// if (t.find(".chk").hasClass("chked") === !0) {
// e.push(t.attr('data-id'));
// }
// }), e
// },
};
var setMaxSize = function() {
// firefox 自带开发工具搜索 @download
// 可恶的百度,刚写好第二天就变了。需要提前加载 downloadProxyPcs。
var commonService = require("common:widget/commonService/commonService.js")
commonService.getWidgets('common:widget/downloadProxyPcs/downloadProxyPcs.js', function () {
var downloadManager = require("common:widget/downloadManager/downloadManager.js");
downloadManager.SIZE_THRESHOLD = Number.MAX_VALUE;
});
};
var downloadAll = function() {
// 得到选中的条目,过滤文件夹
// var fs_ids = dataCenter.get('selectedList'), // ["812801091852241", "860551491728460"]
var fs_ids = [],
fileList = [];
var nodes = dataCenter.get('selectedItemList')
$(nodes).each(function(i, div){
var $div = $(div);
if ($div.data('extname') === 'dir') return;
var fs_id = $div.data('id');
fs_ids.push(fs_id);
fileList.push({
fs_id: fs_id,
server_filename: $div.find('.name').attr('title'),
})
});
if (fileList.length === 0) return;
Toast.obtain.useToast(cloneInto({
toastMode: Toast.obtain.MODE_LOADING,
msg: "正在获取中,请稍后...",
sticky: true,
}, unsafeWindow));
var isSimplePanel = prefs.getIsSimplePanel();
// 获取下载地址
var fidlist = "[" + fs_ids.join(',') + "]",
type = 'dlink',
callback = function(result) {
// console.log(result);
// console.log(fileList)
if (!result.dlink) return;
if (!isSimplePanel) { // 以前的导出方式
result.dlink.forEach(function(info){
for (var i = fileList.length - 1, file; i >= 0; i--) {
file = fileList[i];
if (file.fs_id == info.fs_id) {
file.dlink = info.dlink;
break;
}
}
});
Pan.checkedItems = fileList;
Pan.showPanel(fileList);
} else { // 简易的导出下载链接的方式
var dlinks = result.dlink.map(function(i) { return i.dlink; });
if (!panel) {
panel = new SimplePanel('BDH');
}
panel.show(dlinks.join('\n'));
}
Toast.obtain.setVisible(false);
delete unsafeWindow.gm_pan_callback;
};
// 兼容 Greasemonkey 2.0+
exportFunction(callback, unsafeWindow, {
defineAs: "gm_pan_callback"
});
commonService.getDlink(fidlist, type, unsafeWindow.gm_pan_callback);
};
var setDocumentTitle = function() { // 设置页面标题,根据 hash 变化而变化,方便历史记录检索
var path = Utils.getParam('dir/path') || Utils.getParam('path');
if (path === "") {
var key = Utils.getParam('key')
if (key) {
path = "搜索:" + key;
}
}
if (path) {
// 不知道是否是百度盘的问题,需要 2 次 decodeURIComponent
document.title = '百度云 网盘-' + decodeURIComponent(path);
}
};
var addButton = function() {
$('<a class="btn" style="padding-left:10px"><span class="btn-val">批量下载</span></a>')
.insertAfter('a.btn.download-btn')
.click(downloadAll);
};
var init = function() {
addButton();
setDocumentTitle();
window.addEventListener('hashchange', setDocumentTitle, false);
UI.addQuickLinks();
// 修正添加后,放大状态可能错位的情况
GM_addStyle('.module-aside .remaining{ top:auto !important; bottom: 5% !important; }');
setTimeout(function(){
injectScript(setMaxSize);
}, 2000)
};
return {
init: init
}
})();
var Pan = {
fetchCount: 0,
init: function() {
var pageType = this.determineCurrentPageType();
debug('pageType is ', pageType);
if (pageType !== null) {
this.pageType = pageType;
this.processPage(pageType);
}
},
determineCurrentPageType: function() {
var pageType = null;
var loc = window.location.href.toLowerCase();
if (loc.indexOf('/disk/home') != -1)
{
pageType = 'diskHome';
}
else if (loc.indexOf('/share/link') != -1 || loc.indexOf('/s/') != -1)
{
var type = unsafeWindow.yunData.SHAREPAGETYPE;
if (type == 'multi_file') {
pageType = 'shareDir';
} else if (type == 'single_file_page') {
pageType = 'shareOne';
}
}
else if (loc.indexOf('/share/home') != -1)
{
pageType = 'shareHome';
}
else if (loc.indexOf('/pcloud/album/info') != -1)
{
pageType = 'albumInfo';
}
else if (loc.indexOf('/pcloud/album/file') != -1)
{
pageType = 'albumFile';
} else if (loc.indexOf('/share/init?') != -1) { // 分享的初始页面,用于输入密码等
pageType = 'shareInit';
}
return pageType;
},
processPage: function(pageType) {
if (pageType !== null) {
var pageProcessor = pageType + 'PageProcessor';
this.pageType = pageType;
if (typeof(this[pageProcessor]) == 'function') {
this.allPageProcessor();
this[pageProcessor]();
}
}
},
allPageProcessor: function() {
GM_addStyle(Res.panelCSS);
this.registerControls();
// 下面的去除云管家,会对上传插件无法显示上传文件夹
// 故对此进行修正
if (typeof GM_getValue("removeYunGuanjia") != 'undefined') {
Utilities.useToast({
toastMode: disk.ui.Toast.MODE_CAUTION,
msg: '<b>全局去除云管家</b>会造成主页的上传文件夹功能失效,故默认禁用。',
sticky: true
});
GM_deleteValue("removeYunGuanjia");
}
// 去掉云管家提示,来自 Crack Url Wait Code Login For Chrome
if (prefs.getRemoveYunGuanjia()) {
unsafeWindow.navigator.__defineGetter__('platform', function(){ return ''; });
}
// 去除大文件云管家限制,来自 http://userscripts.org/scripts/show/159911
if (disk && disk.util && disk.util.DownloadManager && disk.util.DownloadManager.SIZE_THRESHOLD) {
disk.util.DownloadManager.SIZE_THRESHOLD = Number.MAX_VALUE;
}
},
diskHomePageProcessor: function() { // 个人主页
mHome.init();
},
shareDirPageProcessor: function() {
// 已失效。
// 写于 2014-5-12
// 只能一个个链接的获取下载地址
return;
var shareId, uk,
self = this;
// 添加批量下载按钮
$('<a class="bbtn" style="padding-left:10px"><b>批量下载</b></a>')
.appendTo('#file_action_buttons')
[0].onclick = function(e) {
self.downloadAll();
};
shareId = FileUtils.share_id || disk.getParam['shareid'];
uk = FileUtils.sysUK || disk.getParam['uk'];
this.API_URL = '/share/list?channel=chunlei&clienttype=0&web=1&page=1&shareid=' + shareId + '&uk=' + uk;
// 添加右键 "复制" 菜单
var observer = new MutationObserver(function(mutations){
mutations.forEach(function(mutation){
for (var i = mutation.addedNodes.length - 1; i >= 0; i--) {
if(mutation.addedNodes[i].id == "right-context-menu"){
self.addRightContextMenu();
observer.disconnect();
}
}
});
});
observer.observe(document.body, { childList: true });
},
shareOnePageProcessor: function() {
if ($('#share_nofound_des').size() > 0) { // 文件不存在
// 失效页面添加分享主页链接
var m = document.location.href.match(/uk=(\d+)/);
if (m) {
var homeUrl = "home?uk=" + m[1] + "#category/type=0";
$('<a>')
.attr('href', homeUrl)
.attr('style', 'margin-left: 10px;')
.text('个人主页')
.appendTo('#share_nofound_des');
}
return;
}
var downInfo = null;
// 获取下载链接,函数 getDlinkShare 和 ajaxGetDlinkShare,2014-9-5
var yunData = unsafeWindow.yunData;
var dlinkService = require('common:widget/dlinkService/dlinkService.js');
var info = {
isForBatch: false,
list: yunData.FILEINFO,
product: "share",
share_id: yunData.SHARE_ID,
share_uk: yunData.SHARE_UK,
sign: yunData.SIGN,
timestamp: yunData.TIMESTAMP
};
var callback = function(result) {
downInfo = result.list[0];
// console.log(downInfo, result);
var link = document.getElementById('downFileButton');
link.setAttribute('href', downInfo.dlink);
link.children[1].style.color = 'red';
delete unsafeWindow.gm_pan_callback;
};
// 兼容 Greasemonkey 2.0+
exportFunction(callback, unsafeWindow, {
defineAs: "gm_pan_callback"
});
dlinkService.getDlinkShare(cloneInto(info, unsafeWindow), unsafeWindow.gm_pan_callback);
// 增加 YAAW 按钮
$('<a class="new-dbtn" href="javascript:;" hidefocus="true">')
.css('padding-left', '10px')
.html('<b>YAAW</b>')
.click(function(){
if (downInfo) {
Pan.addToYAAW(downInfo);
} else {
Utilities.useToast(cloneInto({
toastMode: disk.ui.Toast.MODE_LOADING,
msg: "正在获取链接,请等待",
sticky: false
}, unsafeWindow));
}
})
.insertAfter('#downFileButton');
},
shareHomePageProcessor: function() {
// http://yun.baidu.com/share/home?uk=53993635&view=share#category/type=0
var self = this;
$('section.flag10-fns').attr('title', '双击复制所有链接')
[0].addEventListener('dblclick', function(){
var urls = $.map($('.file-item, .sharegrid-des'), function(elem){
var url = $(elem).attr('_link') || $(elem).attr('_href'),
title = $(elem).find('div[title], .sharegrid-des-title').attr('title');
if (url) {
$.map(Config.trim_titles, function(s) { title = title.replace(s, ''); });
}
return title + Config.lineBreak + url;
});
Pan.copy(urls, false);
}, false);
return;
// 批量保存
// 分享页面:http://yun.baidu.com/s/1mqxnN,查找 doTransferVideo,doTransferFiles 函数
var parseDirPath = function(path) {
return path.substring(path.indexOf(":/") + 1);
};
var doTransferVideo = function(urls) {
var url = urls.shift();
if (!url) return;
$.get(url, function(data) {
var path = Utils.r1(/startTransferVideo\("(.*?)"\)/, data),
share_uk = Utils.r1(/FileUtils.share_uk="(.*?)"/, data), // FileUtils.share_uk
share_id = Utils.r1(/FileUtils.share_id="(.*?)"/, data); // FileUtils.share_id
var postData = {
path: "",
filelist: $.stringify([parseDirPath(path)]),
type: 1
};
// disk.api.RestAPI.TRANSFER
$.post("/share/transfer?channel=chunlei&clienttype=0&web=1" + "&from=" + encodeURIComponent(share_uk) + "&shareid=" + share_id, postData, function(data) {
var info = null;
try {
info = $.parseJSON(data);
} catch (C) {
info = null;
}
console.log(info);
doTransferVideo(urls);
});
});
};
setTimeout(function(){
$('#top_menu_other')
.attr('id', 'mTransferVideos')
.html('批量保存')
.click(function(){
var urls = $.map($('.file-item, .sharegrid-des'), function(elem){
return $(elem).attr('_link') || $(elem).attr('_href');
});
doTransferVideo(urls);
return false;
});
}, 1000);
},
albumInfoPageProcessor: function() {
var self = this;
var _mAlbumId, _mUk, _mPage;
_mAlbumId = (disk.ui.album.albuminfo && disk.ui.album.albuminfo.album_id) || disk.getParam("album_id");
_mUk = (disk.ui.album.uinfo && disk.ui.album.uinfo.uk) || disk.getParam("uk") || disk.getParam("query_uk");
_mPage = {count: 0,totalPage: 0,nowPage: 1,limit: 60, handle: false};
var getList = function() {
var nowPage = $('#albumPage .page-input-wrap > input').val();
_mPage.nowPage = parseInt(nowPage);
var restUrl = "/pcloud/album/listfile?album_id=" + _mAlbumId + "&query_uk=" + _mUk +
"&start=" + (_mPage.nowPage - 1) * 60 + "&limit=" + _mPage.limit;
$.get(restUrl, function(result){
if (result && result.errno == 0 && result.list) {
Pan.showPanel(result.list);
Pan.toast.setVisible(false);
} else {
Pan.useToast("获取数据出错, " + restUrl);
}
});
};
// 内容由 js 生成
var clicked = function(e){
var $quickFileSave = $('#quickFileSave');
if ($quickFileSave[0]) {
$('<a class="bbutton" style="margin-left:10px;padding-left:5px;">\
<b style="padding-right: 5px;">批量下载</b></a>')
.insertAfter($quickFileSave)
[0].onclick = getList;
$('body').unbind('click');
}
};
$('body').bind('click', clicked);
},
albumFilePageProcessor: function() {
var data = { fid_list: "[" + disk.util.ViewShareUtils.fsId + "]" };
// 增加 YAAW 按钮
$('<a class="new-dbtn" href="javascript:;" hidefocus="true">')
.css('padding', '0px 5px')
.html('YAAW')
.click(function(){
if (data) {
Pan.addToYAAW(data);
} else {
Utilities.useToast({
toastMode: disk.ui.Toast.MODE_LOADING,
msg: "正在获取链接,请等待",
sticky: false
});
}
})
.insertAfter('#downFileButtom');
},
shareInitPageProcessor: function() {
// 来自 https://greasyfork.org/scripts/1002-网盘自动填写提取密码
// 因为 Scriptish 0.1.11 的 bug,同一个作者的2个脚本只能安装一个
var sCode = location.hash.slice(1).trim(); // 抓取提取码
if (!/^[a-z0-9]{4}$/.test(sCode)) // 检查是否为合法格式
return;
// console.log('抓取到的提取码: ', sCode);
setTimeout(function() {
$('#accessCode').val(sCode);
$('#submitBtn').click();
}, 100);
},
// -------------------------
registerControls: function() {
debug('registerControls');
if (this.pageType == 'diskHome') {
setTimeout(function(){
// 右上角的更多菜单中添加设置按钮
$('<span class="li"><a href="javascript:;">设置</a></span>')
.click(UI.preferencesShow.bind(UI))
.appendTo('.pulldown.more-info .content');
// 扩展菜单的高度
GM_addStyle('.module-header .info .more-info .content { height: auto; }');
}, 1000);
}
if (typeof GM_registerMenuCommand != 'undefined') {
GM_registerMenuCommand('BaiduPanDownloadHelper 设置', UI.preferencesShow.bind(UI));
}
},
// --------------------
downloadAll: function() {
this.toast = Utilities.useToast({
toastMode: disk.ui.Toast.MODE_LOADING,
msg: "正在获取中,请稍后...",
sticky: true
});
this.getCheckedItems();
},
addRightContextMenu: function(){
var self = this;
// 右键复制
$('<li><a href="javascript:;" style="padding-left: 17px; text-align: left; line-height: 22px;">复制</a></li>')
.appendTo("#right-context-menu")
.get(0).onclick = function(){
self.copyDlinks(FileUtils.getListViewCheckedItems());
};
// 右键复制 aria2c
$('<li><a href="javascript:;" style="padding-left: 17px; text-align: left; line-height: 22px;">复制 Aria2</a></li>')
.appendTo("#right-context-menu")
.get(0)
.addEventListener("click", function(){
self.copyDlinks(FileUtils.getListViewCheckedItems(), 'aria2c');
}, false);
},
getCheckedItems: function() {
var fetchURL,
self = this;
if (!FileUtils.getListViewICheckedtems) {
return;
}
this.checkedItems = FileUtils.getListViewCheckedItems();
this.fetchCount = 0;
this.checkedItems.forEach(function(item) {
if (parseInt(item.isdir, 10) === 1) {
self.fetchCount += 1;
self.getList(item);
}
});
// 没有文件夹的情况
this.handleResult();
},
getList: function(item) {
var self = this;
var dir, restUrl;
dir = item && item.path || disk.getParam('path');
restUrl = this.API_URL + (dir ? '&dir=' + encodeURIComponent(dir) : '');
debug('获取数据', restUrl);
$.get(restUrl, function(result) {
self.fetchCount -= 1;
if (result && result.errno == 0 && result.list) {
if (item) {
item.children = result.list;
} else { // 专辑获取到的结果为 checkedItems
self.checkedItems = result.list;
}
self.handleResult(result.list);
} else {
Pan.useToast("获取数据出错, " + restUrl);
}
});
},
handleResult: function(){ // 全部获取完成
if (this.fetchCount > 0) return;
var self = this;
debug('得到选择的数据', this.checkedItems);
},
showPanel: function(checkedItems, dlinkMap) {
if (!this.panel) {
this.panel = this.createPanel();
}
var linksHTML = this.createDLinksHtml(checkedItems, dlinkMap);
$("#mDownload-links").html(linksHTML);
this.panel.style.display = "block";
},
createPanel: function() {
var self = this;
var container = document.createElement("div");
container.id = "mDownload-container";
var links_div = document.createElement("div");
links_div.id = "mDownload-links";
var closeButton = document.createElement("button");
closeButton.id = "mDownload-close-button";
closeButton.innerHTML = "关闭";
closeButton.onclick = function() {
container.style.display = "none";
};
var exportButton = document.createElement("button");
exportButton.id = "mDownload-export-button";
exportButton.innerHTML = "导出";
exportButton.onclick = function() {
GM_openInTab('data:text/html;charset=utf-8,' + encodeURIComponent(links_div.innerHTML));
};
var copyButton = document.createElement("button");
copyButton.id = "mDownload-copy-button";
copyButton.innerHTML = "复制";
copyButton.onclick = function() {
self.copyDlinks(null);
};
var copyAria2Button = document.createElement("button");
copyAria2Button.id = "mDownload-copy-aria2-button";
copyAria2Button.innerHTML = "复制 Aria2";
copyAria2Button.onclick = function() {
self.copyDlinks(null, 'aria2c');
};
var yaawButton = document.createElement("button");
yaawButton.id = "mDownload-copy-yaaw-button";
yaawButton.innerHTML = "YAAW";
yaawButton.onclick = function() {
self.addToYAAW(self.checkedItems, true);
};
container.appendChild(closeButton);
container.appendChild(exportButton);
container.appendChild(copyButton);
container.appendChild(copyAria2Button);
container.appendChild(yaawButton);
container.appendChild(links_div);
document.body.appendChild(container);
return container;
},
dir_tpl: "<b style='padding-left:{padding_left}'>{server_filename}</b>",
dlinks_tpl: "<a class='dlinks' href={dlink} style='padding-left:{padding_left}'>{server_filename}</a>",
createDLinksHtml: function(checkedItems, dlinkMap) {
var self = this,
htmls = [],
isAdded = false;
checkedItems.forEach(function(item) {
item.padding_left = "0px";
if (item.isdir == 1) {
htmls.push(template(self.dir_tpl, item));
if (Array.isArray(item.children)) {
item.children.forEach(function(i) {
i.padding_left = "15px";
if (dlinkMap) {
i.dlink = dlinkMap[i.fs_id];
}
var tpl = i.dlink ? self.dlinks_tpl : self.dir_tpl;
htmls.push(template(tpl, i));
});
}
} else {
if (!isAdded) {
htmls.push("<b>---------------</b>");
isAdded = true;
}
if (dlinkMap) {
item.dlink = dlinkMap[item.fs_id];
}
htmls.push(template(self.dlinks_tpl, item));
}
});
return htmls.join("<br>");
},
copyDlinks: function(items, type) {
items = items || this.checkedItems;
var tpl = TPLS[type];
if (!tpl) {
tpl = TPLS['normal'];
}
var arr = [];
items.forEach(function(item) {
if (Array.isArray(item.children)) {
item.children.forEach(function(i) {
arr.push(template(tpl, i));
});
} else {
arr.push(template(tpl, item));
}
});
this.copy(arr);
},
copy: function(arr, isDlink) {
if (typeof isDlink == 'undefined') isDlink = true;
GM_setClipboard(arr.join(Config.lineBreak), 'text');
Pan.useToast('已经复制 <b>' + arr.length + '</b> 条' + (isDlink ? '下载' : '') + '链接到剪贴板');
},
addToYAAW: function(items, addCookie){
var aria2 = new ARIA2(prefs.getAria2RPC());
if (!Array.isArray(items)) {
items = [items];
}
var aria2Header = [ Config.YAAW_UA, Config.YAAW_REF];
if (addCookie) {
aria2Header.push(Config.YAAW_COOKIE);
}
items.forEach(function(item){
if (!item.dlink && Array.isArray(item.children)) {
item.children.forEach(function(i){
aria2.addUri(i.dlink, {out: i.server_filename, header: aria2Header });
});
} else {
aria2.addUri(item.dlink, {out: item.server_filename, header: aria2Header });
}
});
Pan.useToast('添加中...到YAAW界面查看是否添加成功');
},
useToast: function(msg, sticky){
// disk.ui.Toast.MODE_FAILURE 错误
// disk.ui.Toast.MODE_CAUTION 警告
// disk.ui.Toast.MODE_LOADING 载入
// disk.ui.Toast.MODE_SUCCESS 正常
if (Utilities) {
return Utilities.useToast(cloneInto({
toastMode: disk.ui.Toast.MODE_CAUTION,
msg: msg,
sticky: sticky || false
}, unsafeWindow));
} else if (require) {
var Toast = require("common:widget/toast/toast.js");
Toast.obtain.useToast(cloneInto({
toastMode: Toast.obtain.MODE_CAUTION,
msg: msg,
sticky: sticky || false,
}, unsafeWindow));
}
}
};
var prefs = {
getRemoveYunGuanjia: function() { // 是否去掉云管家提示
return this._getBooleanConfig('removeYunGuanjiaFix', false);
},
setRmoveYunGuanjia: function(bool) {
GM_setValue('removeYunGuanjiaFix', bool);
},
getIsSimplePanel: function() {
return this._getBooleanConfig('isSimplePanel', false);
},
setIsSimplePanel: function(bool) {
GM_setValue('isSimplePanel', bool);
},
getAria2RPC: function() {
return GM_getValue('aria2_jsonrpc') || 'http://localhost:6800/jsonrpc';
},
setAria2RPC: function(val) {
GM_setValue('aria2_jsonrpc', val);
},
getQuickLinks: function() {
var quickLinks = GM_getValue('quickLinks');
return quickLinks || '';
},
setQuickLinks: function(val) {
GM_setValue('quickLinks', val);
},
_getBooleanConfig : function(configName, defaultValue) {
var config = GM_getValue(configName);
if (config === undefined) {
GM_setValue(configName, defaultValue);
config = defaultValue;
}
return config;
}
};
var UI = {
preferencesShow: function() {
if (this.$prefs) {
this.$prefs.show();
return;
}
this.$style = $('<style>').text(Res.preferencesCSS).appendTo('head');
this.$prefs = $(Res.preferencesHTML).appendTo('body');
this.preferencesLoadHandler();
},
preferencesHide: function() {
if (this.$prefs) this.$prefs.remove();
if (this.$style) this.$style.remove();
this.$prefs = null;
this.$style = null;
},
preferencesLoadHandler: function() {
this.$prefs.find('.cancel').click(this.preferencesHide.bind(UI));
this.$prefs.find('.okay').click(this.preferencesSaveHandler.bind(UI));
this.$prefs.find('#removeYunGuanjia').attr('checked', prefs.getRemoveYunGuanjia());
this.$prefs.find('#isSimplePanel').attr('checked', prefs.getIsSimplePanel());
this.$prefs.find('#aria2RPC').val(prefs.getAria2RPC());
this.$prefs.find('#quickLinks').val(prefs.getQuickLinks());
this.$prefs.find('#quickLinks').attr('placeholder', 'Books=/Books\n小说=/Books/小说');
},
preferencesSaveHandler: function() {
prefs.setRmoveYunGuanjia(this.$prefs.find('#removeYunGuanjia')[0].checked);
prefs.setIsSimplePanel(this.$prefs.find('#isSimplePanel')[0].checked);
prefs.setAria2RPC(this.$prefs.find('#aria2RPC').val());
// quick links
prefs.setQuickLinks(this.$prefs.find('#quickLinks').val());
this.addQuickLinks();
this.preferencesHide();
},
// 个人主页增加自定义快捷目录
addQuickLinks: function() {
if (location.pathname != '/disk/home') return;
var $after = $('.b-list-3');
if ($after.length === 0) {
console.error('没有找到自定义快捷目录的插入位置');
return;
}
$after.find('> .quickLink').remove();
var html = '';
var lines = prefs.getQuickLinks().split('\n');
lines.forEach(function(line){
var offset = line.indexOf('=');
if (offset > 0) {
var name = line.substr(0, offset).trim(),
url = line.substr(offset + 1).trim();
html += '<li class="quickLink b-list-item"><a href="#dir/path=' + url + '" unselectable="on" hidefocus="true" class="sprite2">' +
'<span class="text1 drop-list-trigger">' + name + '</span></a></li>';
}
});