forked from jchuang1977/Files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
百度网盘直接下载助手.user.js
2057 lines (1915 loc) · 88.3 KB
/
百度网盘直接下载助手.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==
// @name 百度网盘直接下载助手
// @namespace undefined
// @version 0.9.24
// @description 直接下载百度网盘和百度网盘分享的文件,避免下载文件时调用百度网盘客户端,获取网盘文件的直接下载地址
// @author ivesjay
// @match *:https://pan.baidu.com/disk/home*
// @match *:https://yun.baidu.com/disk/home*
// @match *:https://pan.baidu.com/s/*
// @match *:https://yun.baidu.com/s/*
// @match *:https://pan.baidu.com/share/link*
// @match *:https://yun.baidu.com/share/link*
// @require https://code.jquery.com/jquery-latest.js
// @run-at document-start
// @grant unsafeWindow
// @grant GM_setClipboard
// ==/UserScript==
(function() {
'use strict';
var $ = $ || window.$;
var log_count = 1;
var wordMapHttp = {
'list-grid-switch':'yvgb9XJ',
'list-switched-on':'ksbXZm',
'grid-switched-on':'tch6W25',
'list-switch':'lrbo9a',
'grid-switch':'xh6poL',
'checkbox':'EOGexf',
'col-item':'Qxyfvg',
'check':'fydGNC',
'checked':'EzubGg',
'list-view':'vdAfKMb',
'item-active':'ngb9O6',
'grid-view':'JKvHJMb',
'bar-search':'OFaPaO',
'default-dom':'xpX2PV',
'bar':'qxnX2G5',
'list-tools':'QDDOQB'
};
var wordMapHttps = {
'list-grid-switch':'qobmXB1q',
'list-switched-on':'ewXm1e',
'grid-switched-on':'kxhkX2Em',
'list-switch':'rvpXm63',
'grid-switch':'mxgdJgwv',
'checkbox':'EOGexf',
'col-item':'Qxyfvg',
'check':'fydGNC',
'checked':'EzubGg',
'list-view':'vdAfKMb',
'item-active':'pcamXBRX',
'grid-view':'JKvHJMb',
'bar-search':'OFaPaO',
'default-dom':'nyztJqWE',
'bar':'mkseJqKQ',
'list-tools':'QDDOQB'
};
var wordMap = location.protocol == 'http:' ? wordMapHttp : wordMapHttps;
//console.log(wordMap);
function slog(c1,c2,c3){
c1 = c1?c1:'';
c2 = c2?c2:'';
c3 = c3?c3:'';
console.log('#'+ log_count++ +'-BaiDuNetdiskHelper-log:',c1,c2,c3);
}
$(function(){
switch(detectPage()){
case 'disk':
var panHelper = new PanHelper();
panHelper.init();
return;
case 'share':
case 's':
var panShareHelper = new PanShareHelper();
panShareHelper.init();
return;
default:
return;
}
});
//网盘页面的下载助手
function PanHelper(){
var yunData,sign,timestamp,bdstoken,logid,fid_list;
var fileList=[],selectFileList=[],batchLinkList=[],batchLinkListAll=[],linkList=[],
list_grid_status='list';
var observer,currentPage,currentPath,currentCategory,dialog,searchKey;
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/";
this.init = function(){
yunData = unsafeWindow.yunData;
slog('yunData:',yunData);
if(yunData === undefined){
slog('页面未正常加载,或者百度已经更新!');
return;
}
initParams();
registerEventListener();
createObserver();
addButton();
createIframe();
dialog = new Dialog({addCopy:true});
slog('网盘直接下载助手加载成功!');
};
function initParams(){
sign = getSign();
timestamp = getTimestamp();
bdstoken = getBDStoken();
logid = getLogID();
currentPage = getCurrentPage();
slog('Current display mode:',currentPage);
if(currentPage == 'list')
currentPath = getPath();
if(currentPage == 'category')
currentCategory = getCategory();
if(currentPage == 'search')
searchKey = getSearchKey();
refreshListGridStatus();
refreshFileList();
refreshSelectList();
}
function refreshFileList(){
if (currentPage == 'list') {
fileList = getFileList();
} else if (currentPage == 'category'){
fileList = getCategoryFileList();
} else if (currentPage == 'search') {
fileList = getSearchFileList();
}
}
function refreshSelectList(){
selectFileList = [];
}
function refreshListGridStatus(){
list_grid_status = getListGridStatus();
}
//获取当前的视图模式
function getListGridStatus(){
//return $('div.list-grid-switch').hasClass('list-switched-on')?'list':($('div.list-grid-switch').hasClass('grid-switched-on')?'grid':'list');
//return $('div.itiWzPY').hasClass('kudtWY46')?'list':($('div.itiWzPY').hasClass('nytAL9w')?'grid':'list');
return $('div.'+wordMap['list-grid-switch']).hasClass(wordMap['list-switched-on'])?'list':($('div.'+wordMap['list-grid-switch']).hasClass(wordMap['grid-switched-on'])?'grid':'list');
}
function registerEventListener(){
registerHashChange();
registerListGridStatus();
registerCheckbox();
registerAllCheckbox();
registerFileSelect();
}
//监视地址栏#标签的变化
function registerHashChange(){
window.addEventListener('hashchange',function(e){
refreshListGridStatus();
if(getCurrentPage() == 'list') {
if(currentPage == getCurrentPage()){
if(currentPath == getPath()){
return;
} else {
currentPath = getPath();
refreshFileList();
refreshSelectList();
}
} else {
currentPage = getCurrentPage();
currentPath = getPath();
refreshFileList();
refreshSelectList();
}
} else if (getCurrentPage() == 'category') {
if(currentPage == getCurrentPage()){
if(currentCategory == getCategory()){
return;
} else {
currentPage = getCurrentPage();
currentCategory = getCategory();
refreshFileList();
refreshSelectList();
}
} else {
currentPage = getCurrentPage();
currentCategory = getCategory();
refreshFileList();
refreshSelectList();
}
} else if(getCurrentPage() == 'search') {
if(currentPage == getCurrentPage()){
if(searchKey == getSearchKey()){
return;
} else {
currentPage = getCurrentPage();
searchKey = getSearchKey();
refreshFileList();
refreshSelectList();
}
} else {
currentPage = getCurrentPage();
searchKey = getSearchKey();
refreshFileList();
refreshSelectList();
}
}
});
}
//监视视图变化
function registerListGridStatus(){
//var $a_list = $('a[node-type=list-switch]');
//var $a_list = $('a[node-type=eepWzkk]');
var $a_list = $('a[node-type='+wordMap['list-switch']+']');
$a_list.click(function(){
list_grid_status = 'list';
});
//var $a_grid = $('a[node-type=grid-switch]');
//var $a_grid = $('a[node-type=ytnvWY7q]');
var $a_grid = $('a[node-type='+wordMap['grid-switch']+']');
$a_grid.click(function(){
list_grid_status = 'grid';
});
}
//文件选择框
function registerCheckbox(){
//var $checkbox = $('span.checkbox');
//var $checkbox = $('span.EOGexf');
var $checkbox = $('span.'+wordMap['checkbox']);
$checkbox.each(function(index,element){
$(element).bind('click',function(e){
var $parent = $(this).parent();
var filename;
if(list_grid_status == 'list') {
//filename = $('div.file-name div.text a',$parent).attr('title');
filename = $('div.file-name div.text a',$parent).attr('title');
}else if(list_grid_status == 'grid'){
//filename = $('div.file-name a',$parent).attr('title');
filename = $('div.file-name a',$parent).attr('title');
}
//if($parent.hasClass('item-active')){
//if($parent.hasClass('prWzXA')){
if($parent.hasClass(wordMap['item-active'])){
slog('取消选中文件:'+filename);
for(var i=0;i<selectFileList.length;i++){
if(selectFileList[i].filename == filename){
selectFileList.splice(i,1);
}
}
}else{
slog('选中文件:'+filename);
$.each(fileList,function(index,element){
if(element.server_filename == filename){
var obj = {
filename:element.server_filename,
path:element.path,
fs_id:element.fs_id,
isdir:element.isdir
};
selectFileList.push(obj);
}
});
}
});
});
}
function unregisterCheckbox(){
//var $checkbox = $('span.checkbox');
//var $checkbox = $('span.EOGexf');
var $checkbox = $('span.'+wordMap['checkbox']);
$checkbox.each(function(index,element){
$(element).unbind('click');
});
}
//全选框
function registerAllCheckbox(){
//var $checkbox = $('div.col-item.check');
//var $checkbox = $('div.Qxyfvg.fydGNC');
var $checkbox = $('div.'+wordMap['col-item']+'.'+wordMap['check']);
$checkbox.each(function(index,element){
$(element).bind('click',function(e){
var $parent = $(this).parent();
//if($parent.hasClass('checked')){
//if($parent.hasClass('EzubGg')){
if($parent.hasClass(wordMap['checked'])){
slog('取消全选');
selectFileList = [];
} else {
slog('全部选中');
selectFileList = [];
$.each(fileList,function(index,element){
var obj = {
filename:element.server_filename,
path:element.path,
fs_id:element.fs_id,
isdir:element.isdir
};
selectFileList.push(obj);
});
}
});
});
}
function unregisterAllCheckbox(){
//var $checkbox = $('div.col-item.check');
//var $checkbox = $('div.Qxyfvg.fydGNC');
var $checkbox = $('div.'+wordMap['col-item']+'.'+wordMap['check']);
$checkbox.each(function(index,element){
$(element).unbind('click');
});
}
//单个文件选中,点击文件不是点击选中框,会只选中该文件
function registerFileSelect(){
//var $dd = $('div.list-view dd');
//var $dd = $('div.vdAfKMb dd');
var $dd = $('div.'+wordMap['list-view']+' dd');
$dd.each(function(index,element){
$(element).bind('click',function(e){
var nodeName = e.target.nodeName.toLowerCase();
if(nodeName != 'span' && nodeName != 'a' && nodeName != 'em') {
slog('shiftKey:'+e.shiftKey);
if(!e.shiftKey){
selectFileList = [];
var filename = $('div.file-name div.text a',$(this)).attr('title');
slog('选中文件:' + filename);
$.each(fileList,function(index,element){
if(element.server_filename == filename){
var obj = {
filename:element.server_filename,
path:element.path,
fs_id:element.fs_id,
isdir:element.isdir
};
selectFileList.push(obj);
}
});
}else{
selectFileList = [];
//var $dd_select = $('div.list-view dd.item-active');
//var $dd_select = $('div.vdAfKMb dd.prWzXA');
var $dd_select = $('div.'+wordMap['list-view']+' dd.'+wordMap['item-active']);
$.each($dd_select,function(index,element){
var filename = $('div.file-name div.text a',$(element)).attr('title');
slog('选中文件:' + filename);
$.each(fileList,function(index,element){
if(element.server_filename == filename){
var obj = {
filename:element.server_filename,
path:element.path,
fs_id:element.fs_id,
isdir:element.isdir
};
selectFileList.push(obj);
}
});
});
}
}
});
});
}
function unregisterFileSelect(){
//var $dd = $('div.list-view dd');
//var $dd = $('div.vdAfKMb dd');
var $dd = $('div.'+wordMap['list-view']+' dd');
$dd.each(function(index,element){
$(element).unbind('click');
});
}
//监视文件列表显示变化
function createObserver(){
var MutationObserver = window.MutationObserver;
var options = {
'childList': true
};
observer = new MutationObserver(function(mutations){
unregisterCheckbox();
unregisterAllCheckbox();
unregisterFileSelect();
registerCheckbox();
registerAllCheckbox();
registerFileSelect();
});
//var list_view = document.querySelector('.list-view');
//var grid_view = document.querySelector('.grid-view');
//var list_view = document.querySelector('.vdAfKMb');
//var grid_view = document.querySelector('.JKvHJMb');
var list_view = document.querySelector('.'+wordMap['list-view']);
var grid_view = document.querySelector('.'+wordMap['grid-view']);
observer.observe(list_view,options);
observer.observe(grid_view,options);
}
//添加助手按钮
function addButton(){
//$('div.bar-search').css('width','18%');//修改搜索框的宽度,避免遮挡
//$('div.OFaPaO').css('width','18%');
$('div.'+wordMap['bar-search']).css('width','18%');
var $dropdownbutton = $('<span class="g-dropdown-button"></span>');
var $dropdownbutton_a = $('<a class="g-button" href="javascript:void(0);"><span class="g-button-right"><em class="icon icon-download" title="百度网盘下载助手"></em><span class="text" style="width: auto;">下载助手</span></span></a>');
var $dropdownbutton_span = $('<span class="menu" style="width:96px"></span>');
var $directbutton = $('<span class="g-button-menu" style="display:block"></span>');
var $directbutton_span = $('<span class="g-dropdown-button g-dropdown-button-second" menulevel="2"></span>');
var $directbutton_a = $('<a class="g-button" href="javascript:void(0);"><span class="g-button-right"><span class="text" style="width:auto">直接下载</span></span></a>');
var $directbutton_menu = $('<span class="menu" style="width:120px;left:79px"></span>');
var $directbutton_download_button = $('<a id="download-direct" class="g-button-menu" href="javascript:void(0);">下载</a>');
var $directbutton_link_button = $('<a id="link-direct" class="g-button-menu" href="javascript:void(0);">显示链接</a>');
var $directbutton_batchhttplink_button = $('<a id="batchhttplink-direct" class="g-button-menu" href="javascript:void(0);">批量链接(HTTP)</a>');
var $directbutton_batchhttpslink_button = $('<a id="batchhttpslink-direct" class="g-button-menu" href="javascript:void(0);">批量链接(HTTPS)</a>');
$directbutton_menu.append($directbutton_download_button).append($directbutton_link_button).append($directbutton_batchhttplink_button).append($directbutton_batchhttpslink_button);
$directbutton.append($directbutton_span.append($directbutton_a).append($directbutton_menu));
$directbutton.hover(function(){
$directbutton_span.toggleClass('button-open');
});
$directbutton_download_button.click(downloadClick);
$directbutton_link_button.click(linkClick);
$directbutton_batchhttplink_button.click(batchClick);
$directbutton_batchhttpslink_button.click(batchClick);
var $apibutton = $('<span class="g-button-menu" style="display:block"></span>');
var $apibutton_span = $('<span class="g-dropdown-button g-dropdown-button-second" menulevel="2"></span>');
var $apibutton_a = $('<a class="g-button" href="javascript:void(0);"><span class="g-button-right"><span class="text" style="width:auto">API下载</span></span></a>');
var $apibutton_menu = $('<span class="menu" style="width:120px;left:77px"></span>');
var $apibutton_download_button = $('<a id="download-api" class="g-button-menu" href="javascript:void(0);">下载</a>');
var $apibutton_link_button = $('<a id="httplink-api" class="g-button-menu" href="javascript:void(0);">显示链接</a>');
var $apibutton_batchhttplink_button = $('<a id="batchhttplink-api" class="g-button-menu" href="javascript:void(0);">批量链接(HTTP)</a>');
var $apibutton_batchhttpslink_button = $('<a id="batchhttpslink-api" class="g-button-menu" href="javascript:void(0);">批量链接(HTTPS)</a>');
$apibutton_menu.append($apibutton_download_button).append($apibutton_link_button).append($apibutton_batchhttplink_button).append($apibutton_batchhttpslink_button);
$apibutton.append($apibutton_span.append($apibutton_a).append($apibutton_menu));
$apibutton.hover(function(){
$apibutton_span.toggleClass('button-open');
});
$apibutton_download_button.click(downloadClick);
$apibutton_link_button.click(linkClick);
$apibutton_batchhttplink_button.click(batchClick);
$apibutton_batchhttpslink_button.click(batchClick);
var $outerlinkbutton = $('<span class="g-button-menu" style="display:block"></span>');
var $outerlinkbutton_span = $('<span class="g-dropdown-button g-dropdown-button-second" menulevel="2"></span>');
var $outerlinkbutton_a = $('<a class="g-button" href="javascript:void(0);"><span class="g-button-right"><span class="text" style="width:auto">外链下载</span></span></a>');
var $outerlinkbutton_menu = $('<span class="menu" style="width:120px;left:79px"></span>');
var $outerlinkbutton_download_button = $('<a id="download-outerlink" class="g-button-menu" href="javascript:void(0);">下载</a>');
var $outerlinkbutton_link_button = $('<a id="link-outerlink" class="g-button-menu" href="javascript:void(0);">显示链接</a>');
var $outerlinkbutton_batchlink_button = $('<a id="batchlink-outerlink" class="g-button-menu" href="javascript:void(0);">批量链接</a>');
$outerlinkbutton_menu.append($outerlinkbutton_download_button).append($outerlinkbutton_link_button).append($outerlinkbutton_batchlink_button);
$outerlinkbutton.append($outerlinkbutton_span.append($outerlinkbutton_a).append($outerlinkbutton_menu));
$outerlinkbutton.hover(function(){
$outerlinkbutton_span.toggleClass('button-open');
});
$outerlinkbutton_download_button.click(downloadClick);
$outerlinkbutton_link_button.click(linkClick);
$outerlinkbutton_batchlink_button.click(batchClick);
$dropdownbutton_span.append($directbutton).append($apibutton).append($outerlinkbutton);
$dropdownbutton.append($dropdownbutton_a).append($dropdownbutton_span);
$dropdownbutton.hover(function(){
$dropdownbutton.toggleClass('button-open');
});
//$('div.default-dom div.bar div.list-tools').append($dropdownbutton);
//$('div.irhW9pZ div.yqgR747 div.QDDOQB').append($dropdownbutton);
$('div.'+wordMap['list-tools']).append($dropdownbutton);
}
//暂时没用
// function addLoading(){
// var screenWidth = document.body.clientWidth;
// var screenHeight = document.body.scrollHeight;
// var left = (screenWidth-10)/2;
// var top = screenHeight/2;
// var $loading = $('<div id="dialog-loading" style="position:absolute;left:'+left+'px;top:'+top+'px;display:none;z-index:52;color:white;font-size:16px">处理中</div>');
// $('body').append($loading);
// }
function downloadClick(event){
slog('选中文件列表:',selectFileList);
var id = event.target.id;
var downloadLink;
if(id == 'download-direct'){
var downloadType;
if(selectFileList.length === 0) {
alert("获取选中文件失败,请刷新重试!");
return;
} else if (selectFileList.length == 1) {
if (selectFileList[0].isdir === 1)
downloadType = 'batch';
else if (selectFileList[0].isdir === 0)
downloadType= 'dlink';
//downloadType = selectFileList[0].isdir==1?'batch':(selectFileList[0].isdir===0?'dlink':'batch');
} else if(selectFileList.length > 1){
downloadType = 'batch';
}
fid_list = getFidList(selectFileList);
var result = getDownloadLinkWithPanAPI(downloadType);
if(result.errno === 0){
if(downloadType == 'dlink')
downloadLink = result.dlink[0].dlink;
else if(downloadType == 'batch'){
downloadLink = result.dlink;
if(selectFileList.length === 1)
downloadLink = downloadLink + '&zipname=' + encodeURIComponent(selectFileList[0].filename) + '.zip';
}
else{
alert("发生错误!");
return;
}
} else if(result.errno == -1){
alert('文件不存在或已被百度和谐,无法下载!');
return;
}else if(result.errno == 112){
alert("页面过期,请刷新重试!");
return;
}else{
alert("发生错误!");
return;
}
}else{
if(selectFileList.length === 0) {
alert("获取选中文件失败,请刷新重试!");
return;
} else if (selectFileList.length > 1) {
alert("该方法不支持多文件下载!");
return;
} else {
if(selectFileList[0].isdir == 1){
alert("该方法不支持目录下载!");
return;
}
}
if(id == 'download-api'){
downloadLink = getDownloadLinkWithRESTAPIBaidu(selectFileList[0].path);
} else if (id == 'download-outerlink'){
var result = getDownloadLinkWithClientAPI(selectFileList[0].path);
if(result.errno == 0){
downloadLink = result.urls[0].url;
}else if(result.errno == 1){
alert('文件不存在!');
return;
}else if(result.errno == 2){
alert('文件不存在或者已被百度和谐,无法下载!');
return;
}else{
alert('发生错误!');
return;
}
}
}
execDownload(downloadLink);
}
function linkClick(event){
slog('选中文件列表:',selectFileList);
var id = event.target.id;
var linkList,tip;
if(id.indexOf('direct') != -1){
var downloadType;
var downloadLink;
if(selectFileList.length === 0) {
alert("获取选中文件失败,请刷新重试!");
return;
} else if (selectFileList.length == 1) {
if (selectFileList[0].isdir === 1)
downloadType = 'batch';
else if (selectFileList[0].isdir === 0)
downloadType= 'dlink';
} else if(selectFileList.length > 1){
downloadType = 'batch';
}
fid_list = getFidList(selectFileList);
var result = getDownloadLinkWithPanAPI(downloadType);
if(result.errno === 0){
if(downloadType == 'dlink')
downloadLink = result.dlink[0].dlink;
else if(downloadType == 'batch'){
slog(selectFileList);
downloadLink = result.dlink;
if(selectFileList.length === 1)
downloadLink = downloadLink + '&zipname=' + encodeURIComponent(selectFileList[0].filename) + '.zip';
}
else{
alert("发生错误!");
return;
}
}else if(result.errno == -1){
alert('文件不存在或已被百度和谐,无法下载!');
return;
}else if(result.errno == 112){
alert("页面过期,请刷新重试!");
return;
}else{
alert("发生错误!");
return;
}
var httplink = downloadLink.replace(/^([A-Za-z]+):/,'http:');
var httpslink = downloadLink.replace(/^([A-Za-z]+):/,'https:');
var filename = '';
$.each(selectFileList,function(index,element){
if(selectFileList.length == 1)
filename = element.filename;
else{
if(index ==0)
filename = element.filename;
else
filename = filename + ',' + element.filename;
}
});
linkList = {
filename:filename,
urls:[
{url:httplink,rank:1},
{url:httpslink,rank:2}
]
};
tip = '显示模拟百度网盘网页获取的链接,可以使用右键迅雷下载,复制到下载工具需要传递cookie,多文件打包下载的链接可以直接复制使用';
dialog.open({title:'下载链接',type:'link',list:linkList,tip:tip});
}else{
if(selectFileList.length === 0) {
alert("获取选中文件失败,请刷新重试!");
return;
} else if (selectFileList.length > 1) {
alert("该方法不支持多文件下载!");
return;
} else {
if(selectFileList[0].isdir == 1){
alert("该方法不支持目录下载!");
return;
}
}
if(id.indexOf('api') != -1){
var downloadLink = getDownloadLinkWithRESTAPIBaidu(selectFileList[0].path);
var httplink = downloadLink.replace(/^([A-Za-z]+):/,'http:');
var httpslink = downloadLink.replace(/^([A-Za-z]+):/,'https:');
linkList = {
filename:selectFileList[0].filename,
urls:[
{url:httplink,rank:1},
{url:httpslink,rank:2}
]
};
httplink = httplink.replace('250528','266719');
httpslink = httpslink.replace('250528','266719');
linkList.urls.push({url:httplink,rank:3});
linkList.urls.push({url:httpslink,rank:4});
tip = '显示模拟APP获取的链接(使用百度云ID),可以使用右键迅雷下载,复制到下载工具需要传递cookie';
dialog.open({title:'下载链接',type:'link',list:linkList,tip:tip});
} else if (id.indexOf('outerlink') != -1){
var result = getDownloadLinkWithClientAPI(selectFileList[0].path);
if(result.errno == 0){
linkList = {
filename:selectFileList[0].filename,
urls:result.urls
};
}else if(result.errno == 1){
alert('文件不存在!');
return;
}else if(result.errno == 2){
alert('文件不存在或者已被百度和谐,无法下载!');
return;
}else{
alert('发生错误!');
return;
}
tip = '显示模拟百度网盘客户端获取的链接,可以直接复制到下载工具使用,不需要cookie';
dialog.open({title:'下载链接',type:'link',list:linkList,tip:tip,showcopy:true,showedit:true});
}
}
//dialog.open({title:'下载链接',type:'link',list:linkList,tip:tip});
}
function batchClick(event){
slog('选中文件列表:',selectFileList);
if(selectFileList.length === 0){
alert('获取选中文件失败,请刷新重试!');
return;
}
var id = event.target.id;
var linkType,tip;
linkType = id.indexOf('https') == -1 ? (id.indexOf('http') == -1 ? location.protocol+':' : 'http:') : 'https:';
batchLinkList = [];
batchLinkListAll = [];
if(id.indexOf('direct') != -1){
batchLinkList = getDirectBatchLink(linkType);
tip = '显示所有选中文件的直接下载链接,文件夹显示为打包下载的链接';
if(batchLinkList.length === 0){
alert('没有链接可以显示,API链接不要全部选中文件夹!');
return;
}
dialog.open({title:'批量链接',type:'batch',list:batchLinkList,tip:tip,showcopy:true});
} else if(id.indexOf('api') != -1){
batchLinkList = getAPIBatchLink(linkType);
tip = '显示所有选中文件的API下载链接,不显示文件夹';
if(batchLinkList.length === 0){
alert('没有链接可以显示,API链接不要全部选中文件夹!');
return;
}
dialog.open({title:'批量链接',type:'batch',list:batchLinkList,tip:tip,showcopy:true});
} else if(id.indexOf('outerlink') != -1){
batchLinkListAll = getOuterlinkBatchLinkAll();
batchLinkList = getOuterlinkBatchLinkFirst(batchLinkListAll);
tip = '显示所有选中文件的外部下载链接,不显示文件夹';
if(batchLinkList.length === 0){
alert('没有链接可以显示,API链接不要全部选中文件夹!');
return;
}
dialog.open({title:'批量链接',type:'batch',list:batchLinkList,tip:tip,showcopy:true,alllist:batchLinkListAll,showall:true});
}
//dialog.open({title:'批量链接',type:'batch',list:batchLinkList,tip:tip,showcopy:true});
}
function getDirectBatchLink(linkType){
var list = [];
$.each(selectFileList,function(index,element){
var downloadType,downloadLink,result;
if(element.isdir == 0)
downloadType = 'dlink';
else
downloadType = 'batch';
fid_list = getFidList([element]);
result = getDownloadLinkWithPanAPI(downloadType);
if(result.errno == 0){
if(downloadType == 'dlink')
downloadLink = result.dlink[0].dlink;
else if(downloadType == 'batch')
downloadLink = result.dlink;
downloadLink = downloadLink.replace(/^([A-Za-z]+):/,linkType);
}else{
downloadLink = 'error';
}
list.push({filename:element.filename,downloadlink:downloadLink});
});
return list;
}
function getAPIBatchLink(linkType){
var list = [];
$.each(selectFileList,function(index,element){
if(element.isdir == 1)
return;
var downloadLink;
downloadLink = getDownloadLinkWithRESTAPIBaidu(element.path);
downloadLink = downloadLink.replace(/^([A-Za-z]+):/,linkType);
list.push({filename:element.filename,downloadlink:downloadLink});
});
return list;
}
function getOuterlinkBatchLinkAll(){
var list = [];
$.each(selectFileList,function(index,element){
var result;
if(element.isdir == 1)
return;
result = getDownloadLinkWithClientAPI(element.path);
if(result.errno == 0){
//downloadLink = result.urls[0].url;
list.push({filename:element.filename,links:result.urls});
}else{
//downloadLink = 'error';
list.push({filename:element.filename,links:[{rank:1,url:'error'}]});
}
//list.push({filename:element.filename,downloadlink:downloadLink});
});
return list;
}
function getOuterlinkBatchLinkFirst(list){
var result = [];
$.each(list,function(index,element){
result.push({filename:element.filename,downloadlink:element.links[0].url});
});
return result;
}
function getSign(){
var signFnc;
try{
signFnc = new Function("return " + yunData.sign2)();
} catch(e){
throw new Error(e.message);
}
return base64Encode(signFnc(yunData.sign5,yunData.sign1));
}
//获取当前目录
function getPath(){
var hash = location.hash;
var regx = /(^|&|\/)path=([^&]*)(&|$)/i;
var result = hash.match(regx);
return decodeURIComponent(result[2]);
}
//获取分类显示的类别,即地址栏中的type
function getCategory(){
var hash = location.hash;
var regx = /(^|&|\/)type=([^&]*)(&|$)/i;
var result = hash.match(regx);
return decodeURIComponent(result[2]);
}
function getSearchKey(){
var hash = location.hash;
var regx = /(^|&|\/)key=([^&]*)(&|$)/i;
var result = hash.match(regx);
return decodeURIComponent(result[2]);
}
//获取当前页面(list或者category)
function getCurrentPage(){
var hash = location.hash;
return decodeURIComponent(hash.substring(hash.indexOf('#')+1,hash.indexOf('/')));
}
//获取文件列表
function getFileList(){
var filelist = [];
var listUrl = panAPIUrl + "list";
var path = getPath();
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 getCategoryFileList(){
var filelist = [];
var listUrl = panAPIUrl + "categorylist";
var category = getCategory();
logid = getLogID();
var params = {
category:category,
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.info : [];
}
});
return filelist;
}
function getSearchFileList(){
var filelist = [];
var listUrl = panAPIUrl + 'search';
logid = getLogID();
searchKey = getSearchKey();
var params = {
recursion:1,
order:'time',
desc:1,
showempty:0,
web:1,
page:1,
num:100,
key:searchKey,
channel:'chunlei',
app_id:250528,
bdstoken:bdstoken,
logid:logid,
clienttype:0
};
$.ajax({
url:listUrl,
async:false,
method:'GET',
data:params,
success:function(response){
filelist = 0===response.errno ? response.list : [];
}
});
return filelist;
}
//生成下载时的fid_list参数
function getFidList(list){
var fidlist = null;
if (list.length === 0)
return null;
var fileidlist = [];
$.each(list,function(index,element){
fileidlist.push(element.fs_id);
});
fidlist = '[' + fileidlist + ']';
return fidlist;
}
function getTimestamp(){
return yunData.timestamp;
}
function getBDStoken(){
return yunData.MYBDSTOKEN;
}
//获取直接下载地址
//这个地址不是直接下载地址,访问这个地址会返回302,response header中的location才是真实下载地址
//暂时没有找到提取方法
function getDownloadLinkWithPanAPI(type){
var downloadUrl = panAPIUrl + "download";
var result;
logid = getLogID();
var params= {
sign:sign,
timestamp:timestamp,
fidlist:fid_list,
type:type,
channel:'chunlei',
web:1,
app_id:250528,
bdstoken:bdstoken,
logid:logid,
clienttype:0
};
$.ajax({
url:downloadUrl,
async:false,
method:'GET',
data:params,
success:function(response){
result = response;
}
});
return result;
}
function getDownloadLinkWithRESTAPIBaidu(path){
var link = restAPIUrl + 'file?method=download&app_id=250528&path=' + encodeURIComponent(path);
return link;
}
function getDownloadLinkWithRESTAPIES(path){
var link = restAPIUrl + 'file?method=download&app_id=266719&path=' + encodeURIComponent(path);
return link;
}
function getDownloadLinkWithClientAPI(path){
var result;