-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·960 lines (957 loc) · 47.2 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>vList--加载中...</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="manifest" href="manifest.json" />
<style>
body{
overflow: hidden;
margin: 0;
}
::-webkit-scrollbar {
border-radius: 1rem;
background-color: #d8d7d733;
width: .5rem;
}
::-webkit-scrollbar-thumb {
background-color: #c9c6c680;
border-radius: 1rem;
}
::-webkit-scrollbar-thumb:hover{
background-color: #ece9e9cc;
}
::-webkit-scrollbar:hover{
width: 1rem;
}
* {
scrollbar-track-color: none;
}
iframe{
width: 100vw;height: 100vh;
border: none;
}
#view,div.layer{
position: fixed;z-index: 4;
top: 0;bottom: 0;left: 0;right: 0;
background-color: rgba(0,0,0,0.2);
}
#view{
z-index: 3;
display: flex;align-items: center;justify-content: center;
}
#view > *{
max-width: 100vw;max-height: 100vh;
}
*[hidden]{
display: none !important;
}
#box,.btns{
display: flex;justify-content: center;flex-wrap: wrap;
}
#box{
background-color: rgb(179 223 244 / 42%);
position: fixed;
bottom: 0;left: 0;right: 0;
z-index: 2;
}
#box > button{
border: none;outline: none;
background: none;
}
#box > *,.btns > *{
cursor: pointer;
padding: 0.5rem;
margin: 0.5rem 0.25rem;
border-radius: 2rem;
transition: background 0.1s;
}
#box > *:hover,.btns > *:hover,#box > *:focus{
background-color: rgb(43 253 253 / 20%);
}
#box svg,#box img,.btns svg,.btns img{
width: 1.5rem;
height: 1.5rem;
display: block;
}
#box > *.view{
position: relative;
}
#box > *.view::before{
content: '';
background-color: #27e52d;
position: absolute;
top: 2.5rem;left: 0;right: 0;
display: block;
height: 0.4rem;
border-radius: 0.2rem;
width: 0.4rem;
margin: auto;
transition: width 0.2s;
}
#box > *.view:hover::before{
width: 100%;
}
div.ctx{
position: fixed;
padding: .5rem 0;
border-radius: 0.4rem;
background-color: rgb(223 223 223 / 80%);
max-width: 100vw;min-width: 10rem;
z-index: 1;
}
div.ctx > p{
display: block;
padding: 0.5rem 1rem;
transition: background 0.1s;
margin: 0.2rem 0;
}
div.ctx > p:hover{
background-color: rgb(128 128 128 / 50%);
color: white;
}
.btn-close{
content: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 16 16"><path d="M2.146 2.854a.5.5 0 1 1 .708-.708L8 7.293l5.146-5.147a.5.5 0 0 1 .708.708L8.707 8l5.147 5.146a.5.5 0 0 1-.708.708L8 8.707l-5.146 5.147a.5.5 0 0 1-.708-.708L7.293 8 2.146 2.854Z"/></svg>');
height: 1em;
width: 1em;
transform: scale(1.2) translateY(0.2rem);
background-color: var(--light-2);
float: right;
margin-right: 0.5rem;
}
#msg{
position: fixed;
top: 1rem;
right: 2rem;
max-width: 20rem;
width: 100%;
z-index: 10;
}
#msg > *{
width: 100%;
box-sizing: border-box;
padding: 0.5rem;
border-radius: 2rem;
display: flex;
background-color: rgb(245 245 245 / 60%);
margin: .5rem 0;
}
#msg b{
margin-right: 0.5rem;
min-width:2em;
}
#msg > *::before{
height: 1em;width: 1em;
display: inline-block;
margin: 0 0.5rem;flex-shrink: 0;
transform: scale(1.4) translateY(0.1em);
}
#msg .error::before{
content: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" fill="%23e5484d" viewBox="0 0 16 16"><path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM8 4a.905.905 0 0 0-.9.995l.35 3.507a.552.552 0 0 0 1.1 0l.35-3.507A.905.905 0 0 0 8 4zm.002 6a1 1 0 1 0 0 2 1 1 0 0 0 0-2z"/></svg>');
}
#msg .success::before{
content: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" fill="%2330a46c" viewBox="0 0 16 16"><path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zm-3.97-3.03a.75.75 0 0 0-1.08.022L7.477 9.417 5.384 7.323a.75.75 0 0 0-1.06 1.06L6.97 11.03a.75.75 0 0 0 1.079-.02l3.992-4.99a.75.75 0 0 0-.01-1.05z"/></svg>');
}
#msg .info::before{
content: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" fill="%2305a2c2" viewBox="0 0 16 16"><path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16zm.93-9.412-1 4.705c-.07.34.029.533.304.533.194 0 .487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703 0-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381 2.29-.287zM8 5.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2z"/></svg>');
}
#msg .warn::before{
content: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" fill="%23dcdf30" viewBox="0 0 16 16"><path d="M8.982 1.566a1.13 1.13 0 0 0-1.96 0L.165 13.233c-.457.778.091 1.767.98 1.767h13.713c.889 0 1.438-.99.98-1.767L8.982 1.566zM8 5c.535 0 .954.462.9.995l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 5.995A.905.905 0 0 1 8 5zm.002 6a1 1 0 1 1 0 2 1 1 0 0 1 0-2z"/></svg>');
}
.window{
position: fixed;
z-index: 9;
top: 3rem;left: 0;right: 0;
max-width: 30rem;
margin: auto;
border-radius: 0.5rem;
overflow: hidden;
}
.whead{
padding: 0.5rem 1rem;
color: white;
background-color: #51b6f1;
}
.wbody{
padding: 1rem;
background-color: white;
max-height:60vh;
overflow:auto;
}
.wtool{
padding: 0.5rem;
text-align: right;
background-color: white;
}
.wtool > button{
padding: .5rem 1rem;
margin: 0 .25rem;
background-color: #b3b1b3;
color: white;
border-radius: 0.5rem;
outline: solid 0.2em transparent;border: none;
}
.wtool > button:focus{
outline-color: lightgray !important;
}
.wtool > button.success{
background-color: #40df6a;
}
.wtool > button.info{
background-color: #2ca0d5;
}
.wtool > button.warn{
background-color: #f3da2b;
}
.wtool > button.danger{
background-color: #ff6363;
}
.offcv{
display: none;
position:fixed;
display:flex;
flex-direction:column;
z-index: 8;
}
.offcv-right{
right:0;top:0;bottom:0;
max-width: 90vw;width:30rem;
}
.offcv-left{
left:0;top:0;bottom:0;
}
.offcv-title{
padding:1rem 2rem 0 2rem;
font-size:1.1rem;font-weight:bold;
background-color:white;
}
.offcv-ctx{
flex-grow:1;
overflow:auto;
padding:1rem 2rem;
background-color:white;
}
</style>
</head>
<body>
<!-- vList V2~iframe版.有效减小性能损耗 -->
<iframe></iframe>
<div id="view" hidden></div>
<div id="box"></div>
<div id="msg"></div>
<div hidden id="ctx" class="ctx">
<p>没有内容</p>
</div>
<script>
// 初始化
const VIEW = document.getElementsByTagName('iframe')[0];
if(!VIEW instanceof HTMLElement)
throw new Error('Failed to load Document');
if(undefined == localStorage){
console.log('Warn:Your browser do not support cache.');
$.dialog.msg('warn','注意','你的浏览器版本太低');
var CONFIG = {};
}
// 初始化秘制API
String.prototype.splitLast = function(char){
let pos = this.lastIndexOf(char);
if(pos == -1) return [this,this];
return [this.substring(0,pos),this.substring(pos+1)];
}
// 元素单选
class ElementSelector{
element;
currect = 0;
callback;
constructor(element,callback){
if(typeof callback != 'function')
throw new TypeError('Param #2 require a callback function');
this.callback = callback;
this.element = element;
}
set i(num){
if(this.element == null) throw new Error('Please set an Element first.');
let last = this.i;
if(num >= this.element.length) num = 0;
else if(num < 0) num = this.element.length-1;
if(CONFIG.debug) console.log('Select item ID',num);
this.currect = num;
this.callback(this.i,last);
}
get i(){
if(this.element == null) throw new Error('Please set an Element first.');
if(!this.element[this.currect] instanceof HTMLElement)
throw new Error('Nothing was selected(ID:',this.currect,')');
if(!this.element.length) throw new Error('Empty List.');
return this.element[this.currect];
}
set e(e){
if(!e instanceof NodeList && !e instanceof HTMLCollection)
throw new TypeError('element require NodeList or HTMLCollection.');
this.element = e;
}
get e(){
return this.element;
}
add(val){
this.i = this.currect + val;
}
}
// 基类
const $ = {
// 列表
list : {
// 文件(dir) 文件夹(dir) 列表
list : {},
// 根据后缀名获取类型
getype : function(ext){
if(typeof ext != 'string')
// throw new TypeError('argument #0 is '+typeof ext+',not string');
ext = ext.toString();
ext = ext.toLowerCase();
for (const type1 in CONFIG.map)
for (const type2 in CONFIG.map[type1])
if(CONFIG.map[type1][type2].includes(ext)) return [type1,type2];
return ['bin','unsupport'];
},
// 刷新缓存
refresh : function(){
var list = VIEW.contentDocument.querySelectorAll('body>table#list>tbody>tr>td.link'),
start = Date.now();
this.list = {"file":[],"dir":[]};
for (let i = 0; i < list.length; i++){
this.additem(list[i].getElementsByTagName('a')[0],list[i]);
}
this.autohide();
// for(let {element} of this.match())
this.loadevent.forEach(func => func()); // 执行回调
if(CONFIG.debug) console.log('Success rebuilt DB in',Date.now() - start,'ms');
},
// 隐藏部分匹配的文件
autohide : function(){
for(let hides of CONFIG.hide){
try{
var [,type,name,ext] = hides.match(/^(<\w+>)?([\w\W]+)\.(\w+|\*)$/i),after = [];
}catch(e){
throw new Error('CONFIG param "hide" contains some error.');
}
if(!type || type.length <= 2){ // 名称完全匹配
if(name == '*' || ext == '*') after = this.list.file;
if(ext != '*') after = after.filter(item => item.name.splitLast('.')[1] == ext);
if(name != '*') after = after.filter(item => item.name.splitLast('.')[0] == name);
}else{ // 类型+后缀匹配
type = type.substring(1,type.length-1);
if(name == '*' && ext == '*') return console.error('Please use hideType instead of',hides);
after = [...this.match(name,type)];
if(ext != '*') after = after.filter(item => item.name.splitLast('.')[1] == ext);
}
if(CONFIG.debug) console.log('Matched',after.length,'for',{type,name,ext},':',after);
after.forEach(item => item.element.dataset.hidden = item.element.parentElement.hidden = true);
}
},
// 增加文件/文件夹
additem:function(element,box){
if(!element instanceof HTMLAnchorElement)
throw new TypeError('Cannot add an empty element');
var href = element.href, // 链接地址
name = element.getAttribute('title'), // 文件名
rherf = element.getAttribute('href'), // 真实指向
isdir = rherf.substr(-1) == '/'; // 判断是否为文件夹
if(CONFIG.debug) console.log(isdir,name);
element.parentElement.dataset.href = href;
if(isdir){
// 文件夹
this.list.dir.push({
name : name,
path : href
});
box.classList.add('dir');
box.parentElement.classList.add('dir');
}else{
var ext = name.splitLast('.')[1],[type1,type2] = this.getype(ext);
// 1.决定是否隐藏
box.dataset.hidden = box.parentElement.hidden = CONFIG.autohide && CONFIG.hideType.includes(type1+"/"+type2);
// 2.添加图标
box.classList.add(type1,type2);
// 3.去除默认事件
element.onclick = function(){return false;}
// 4.推入数据库
this.list.file.push({
name : name, // 全名
path : href, // URL路径
type : [type1,type2],// 类型
element : box // 元素
});
}
},
// 匹配文件
match : function*(base,type){
if(type == '*' && base == '*') throw new Error('Nothing matched.');
base = decodeURIComponent(base.toLowerCase());
for (var i = 0; i < this.list.file.length; i++) {
let now = this.list.file[i];
if(type == '*' && now.splitLast('.')[0] == base) yield now;
else if(
(
base === '*' ||
$.tool.base(now.name).toLowerCase() == base
)
&& now.type.includes(type)
) yield now;
}
},
// 获取第一个匹配的文件
matchFirst:function(base,type){
return this.match(base,type).next().value;
},
loadevent:[],
// 添加加载事件
onload:function(callback){
if(typeof callback != 'function')
throw new Error('Should be an function');
if(this.list.file || this.list.dir)
callback(); // 已经构建好一次:直接调用
this.loadevent.push(callback);
}
},
tool:{
view : document.getElementById('view'), // 预览盒子
tool : document.getElementById('box'), // 工具盒子
// 增加工具按钮(底部)
add : function(svg,desc,callback){
var box = document.createElement('button');
box.innerHTML = svg;
box.onclick = callback;
box.title = desc;
this.tool.append(box);
return box;
},
// 添加一个预览盒子
box : function(id,tag,icon,callback){
var box = document.createElement(tag||'div');
box.classList.add('view');
box.id = id;
box.hidden = true;
this.view.append(box);
if(icon != undefined){
let tag = this.add(icon,id,function(){
if(CONFIG.debug) console.log(id,' state: ',box.hidden?'hidden':'showing');
if(box.hidden)
$.tool.show(box);
else
$.tool.view.hidden = box.hidden = true;
});
tag.classList.add('view');
if(callback != undefined)
tag.oncontextmenu = tag.ondblclick = function(){
if(CONFIG.debug) console.log(id,' ctxMenu selected.');
callback(this);
return false;
}
}
return box;
},
// 展示预览盒子
show : function(box){
if(!box instanceof HTMLElement)
throw new TypeError('only HTMLElement should be shown');
if(CONFIG.debug) console.log(box.id,' shown and focus.');
box.hidden = this.view.hidden = false;
(box.autoFocus || box).focus();
},
// 工具:返回文件名,不含后缀名
base : function(path){
if(typeof path != 'string')
// throw new TypeError('base(#1) require arg#1:string,'+typeof path+' was given.');
path = path.toString();
return path.splitLast('/')[1].splitLast('.')[0].toString();
},
// 工具:快速获取URL内容
curl : async function(url){
var fe = await fetch(url);
if(!fe.ok) throw new Error(fe.status);
var res = await fe.text();
return res;
},
// 工具:初始化键盘事件、元素单选
selector : function(listened,elements,keys={},addClass,callback){
let addup,
selector = new ElementSelector(elements,(now,last)=>{
if(!now || !last) return;
if(addClass){
if(last) last.classList.remove(addClass);
if(now.hidden) return selector.add(addup);
now.classList.add(addClass);
}else{
if(now.hidden) selector.add(addup);
else now.focus();
}
if(!now.hidden && callback) callback(now,last);
});
for (let i = 0; i < elements.length; i++)
elements[i].addEventListener('click',()=>selector.i = i);
listened.addEventListener('keydown',function(e){
if(CONFIG.debug) console.log('Clicked [',e.key,'] ON Element',listened);
if(e.key in keys) keys[e.key](selector);
else if(e.key == 'ArrowRight' || e.key == 'ArrowDown') selector.add(addup = 1);
else if(e.key == 'ArrowLeft' || e.key == 'ArrowUp') selector.add(addup = -1);
else if(e.key == 'Enter') selector.i.click();
else return true;
return !!e.preventDefault();
});
return selector;
}
},
dialog : {
box : document.getElementById('msg'),
// 重写remove属性
rewrite:function(e,anim='fadeout',callback){
if(CONFIG.animate)
e.remove = function(){
this.classList.add(anim);
setTimeout(()=>this.parentElement.removeChild(this), 500);
if(typeof callback == 'function') callback(this);
}
else e.remove = function(){
callback(this);
this.parentElement.removeChild(this);
}
},
// 创建DIV池
create:function(ii=3){
var pool = [];
for (var i = 0; i < ii; i++)
pool.push(document.createElement('div'));
return pool;
},
// 输出一段信息
msg: function(type, title, content, remove) {
var e = document.createElement('div');
e.classList.add(type);
e.innerHTML = ` <b> ${title} </b><span style="flex-grow:1;"> ${content} </span>
<span class="btn-close" onclick="this.parentElement.remove();"></span>`;
this.box.append(e);
if(typeof remove == 'number')
setTimeout(()=>e.remove(),remove*1000);
if(CONFIG.animate) this.rewrite(e);
return e;
},
// 弹出一个交互框
dialog : function(title,html,btns,noclose=false){
// 创建DIV池
let [main,head,btn,content,end,cover] = this.create(6);
// 标题
head.innerText = title; // 头
head.classList.add('whead');
if(!noclose){
btn.classList.add('btn-close');
btn.onclick = ()=> main.remove();
head.append(btn);
}
// 内容
content.classList.add('wbody');
content.innerHTML = html;
// 尾部
main.classList.add('window');
main.append(head,content);
// 罩子
cover.classList.add('layer');
if(typeof btns == 'object'){
end.classList.add('wtool');
for (let key in btns) {
let prop = key.split(':'),
btn = document.createElement('button');
btn.innerText = prop[0]; // 内容
if(prop[1] != undefined && prop[1] != '')
btn.classList.add(prop[1]); // 按钮类
btn.onclick = () => btns[key](main);
end.append(btn);
}
main.append(end);
}
// 监听
$.tool.selector(end,end.children);
// 结束
document.body.append(main,cover);
if(end.children.length > 0) end.children[0].focus();
this.rewrite(main,'jumpout',function(){
cover.remove();
});
return main;
},
// offcanvas侧边栏
offcv:function(head,body,direction='right'){
let [main,title,content] = this.create(3);
// 主盒子
main.classList.add('offcv','offcv-'+direction);
// 标题
title.classList.add('offcv-title');
title.innerText = head;
title.innerHTML += '<span class="btn-close" onclick="this.parentElement.parentElement.hidden = true;"></span>';
// 内容物
content.classList.add('offcv-ctx');
content.innerHTML = body;
// 完成
main.append(title,content);
document.body.append(main);
return main;
}
},
cache:{
// 刷新/初始化缓存
reload : async function(focus=false,norpt=false){
// 初始化缓存
if(undefined == localStorage.vlist || focus){
try{
let f = await fetch('./config.json');
if(!f.ok) throw 'e';
CONFIG = await f.json();
}catch(e){
$.dialog.msg('error','配置','加载失败(网络错误)!');
}
localStorage.vlist = JSON.stringify(CONFIG);
$.dialog.msg('success','成功','配置更新成功!',5);
}else try{
window.CONFIG = JSON.parse(localStorage.vlist);
}catch(e){
if(!norpt){
console.log('Wrong JSON.Try to fetch again.');
return this.reload(true,true);
}else{
$.dialog.msg('error','配置','加载失败(解析失败)!');
throw new Error('Failed to fetch&parse JSON');
}
}
},
listener:{},
// 动态设置缓存
set:function(item,val){
let items = item.split('.'),
target = items[items.length-1],
json = JSON.parse(localStorage.vlist),
now = json;
delete items[items.length-1];
items.forEach(n=>{
now = now[n];
if(now == undefined) throw new Error('undefined key'+item);
});
now[target]=val;
localStorage.vlist = JSON.stringify(json);
// 复写opener
json.opener = CONFIG.opener;
CONFIG = json;
// 调用监听
if(item in this.listener) this.listener[item](val);
if(CONFIG.debug) console.warn('config changed.it is dangerous!');
},
// 监听某一个配置的变化
onChange:function(target,callback){
if(target in this.listener && CONFIG.debug)
console.warn('Overwrite handle for:',target);
this.listener[target] = callback;
}
},
loaded : 0,
// 寻找<a>元素
_find : function(src){
if(src.tagName == 'TD'){
if(src.classList.contains('link')){
return src.getElementsByTagName('a')[0];
}else if(src.classList.contains('size') || src.classList.contains('date')){
return src.parentElement.getElementsByTagName('a')[0];
}
}else if(src.tagName == 'A'){
return src;
}else if(src.tagName == 'TR'){
return src.getElementsByTagName('a')[0];
}else return null;
},
// 初始化vList
init : function(){
if(CONFIG.debug) console.log('Restart VLIST service.');
// 1.增加事件监听
var tg = VIEW.contentDocument.getElementsByTagName('tbody')[0];
if(tg == null){
$.dialog.msg('error','错误','这不是vList页面',5);
if(this.loaded != 0)
if(CONFIG.autoBack) setTimeout(()=>VIEW.contentWindow.history.back(),1000);
else $.dialog.msg('warn','警告','请检查URL是否有误后刷新页面',10);
else
if(CONFIG.autoBack) setTimeout(()=>VIEW.contentDocument.location.replace(CONFIG.prefix),1000);
else $.dialog.msg('warn','警告','请尝试在下方工具栏中刷新缓存',10);
throw new Error('An NON-VLIST1-PAGE-ERROR trigged.');
}else this.loaded++;
if(CONFIG.debug) console.log('debug:true,loaded:',this.loaded,',version:',CONFIG.version);
tg.addEventListener('click',function(e){
let a = $._find(e.target);
if(!a) return;
let box = a.parentElement;
// 阻止继续按下
box.parentElement.style.pointerEvents = 'none';
// 检查是否为文件夹
if(box.classList.contains('dir'))
VIEW.src = a.href; // 跳转
// 获取类型并打开
else $.open(a,box.classList).then(()=> box.parentElement.style.pointerEvents = 'auto');
return false;
});
// 2.重新构建目录树
$.list.refresh();
},
// 打开一个文件
open:async function(a,type,no_callself=false){
if(!a instanceof HTMLAnchorElement)
throw new TypeError('Not a tag named <a>');
var open;
if(CONFIG.debug) console.log(a.getAttribute('title'),type[1],type[2]);
if(type[2] != undefined && type[2] in CONFIG.opener){
open = CONFIG.opener[type[2]];
}else if(type[1] in CONFIG.opener){
open = CONFIG.opener[type[1]];
}else{
return window.open(a.href);
}
// this.openlock = true;
try{
if(typeof open == 'string'){
if(no_callself) throw new Error('Failed to open:loop(maybe module contains some error?)');
else {
// 打开协议
if(open.substring(0,1) == '<'){
let [,scheme,target] = open.matchAll(/^\<(\w+)\>([\w\W]+)$/g).next().value;
if(!(scheme in this.module.scheme) || typeof this.module.scheme[scheme] != 'function')
return $.dialog.msg('error','打开失败','找不到协议'+scheme+'的打开方式');
try{
return await this.module.scheme[scheme](target.replaceAll('{{link}}',a.href));
}catch(e){
$.dialog.msg('error','打开失败',e.message);
throw e;
}
// 正常打开
}else try{
await this.module.load(open);
}catch(e){
$.dialog.msg('error','打开失败','依赖加载失败');
}
return $.open(a,type,true); //打开文件
}
}else if(typeof open == 'function') return await open(a.href);
else throw new Error('Unknown opener(Tag:"'+type+'"&Type:"'+typeof open+'")');
}catch(e){
$.dialog.msg('error','打开失败',e.message);
throw e;
}
},
module:{
loaded:{},
// 加载一个模块
load:function(js,tagName='script',callback){
return new Promise(function(rs,rj){
var tag;
if(js in $.module.loaded){
if(typeof $.module.loaded[js] == 'boolean'){
return $.module.loaded[js]?rs():rj('Loaded but failed.');
}else{
tag = $.module.loaded[js];
}
}else{
tag = document.createElement(tagName);
tag.src = js;
tag.onload = ()=> $.module.loaded[js] = true;
tag.onerror = ()=> $.module.loaded[js] = false;
$.module.loaded[js] = tag;
if(callback) callback(tag);
document.body.append(tag);
}
tag.addEventListener('load',e=>{
if(CONFIG.debug) console.log('Load['+$.tool.base(js)+']succeed');
rs(e);
});
tag.addEventListener('error', e=>{
console.warn('Load['+$.tool.base(js)+']failed');
rj(e);
});
});
return true;
},
// 加载css
css:function(css){
return this.load(css,'link',function(tag){
tag.rel='stylesheet';
tag.href = tag.src;
});
},
scheme:{},
// 绑定协议或者type
bind:function(name,callback,typename='opener'){
if(typeof callback != 'function')
throw new TypeError('Callback must a function');
if(CONFIG.debug && typeof this[typename][name] == 'function')
console.warn('WARN:[',name,'] overwriten.It may cause some problem!');
let type = this[typename] || {0:console.error('Unknown type:',typename)};
type[name] = callback;
if(CONFIG.debug) console.log('Loaded new',typename,':[',name,']');
}
},
};
// 启动监听URL
// (new MutationObserver(function(){
// var src = VIEW.src; // 新地址
// $.init();
// console.log('New location:'+src);
// })).observe(VIEW,{
// attributeFilter:["src"]
// });
VIEW.onerror = ()=>VIEW.srcdoc = `
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>错误页</title>
<style>
body{
display: flex;
align-items: center;
justify-content: center;
height: 100vh;overflow: hidden;
margin: 0;padding: 0;
}
div{
text-align: center;
}
svg{
display: inline-block;
width: 5rem;
}
</style>
</head>
<body>
<div onclick="document.location.href="${VIEW.src}";">
<svg fill="gray" viewBox="0 0 16 16">
<path d="M7.938 2.016A.13.13 0 0 1 8.002 2a.13.13 0 0 1 .063.016.146.146 0 0 1 .054.057l6.857 11.667c.036.06.035.124.002.183a.163.163 0 0 1-.054.06.116.116 0 0 1-.066.017H1.146a.115.115 0 0 1-.066-.017.163.163 0 0 1-.054-.06.176.176 0 0 1 .002-.183L7.884 2.073a.147.147 0 0 1 .054-.057zm1.044-.45a1.13 1.13 0 0 0-1.96 0L.165 13.233c-.457.778.091 1.767.98 1.767h13.713c.889 0 1.438-.99.98-1.767L8.982 1.566z"/>
<path d="M7.002 12a1 1 0 1 1 2 0 1 1 0 0 1-2 0zM7.1 5.995a.905.905 0 1 1 1.8 0l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 5.995z"/>
</svg>
<p>网络错误,点击重试</p>
</div>
</body>
</html>`,VIEW.src = '';
// 监听事件
$.tool.view.onclick = e =>{
if(!e || e.target.id == 'view'){
$.tool.view.hidden = true;
for (var i = 0; i < $.tool.view.children.length; i++)
$.tool.view.children[i].hidden = true;
VIEW.contentWindow.focus();
}
};
$.tool.view.addEventListener('keydown',function(e){
if($.tool.view.hidden) return true;
e.stopPropagation();//e.preventDefault();
if(e.key == 'Escape' || e.key == 'Home') $.tool.view.onclick();
});
$.tool.tools = $.tool.selector($.tool.tool,$.tool.tool.children,{
'ArrowUp':()=>VIEW.contentWindow.focus()
})
// 初始化view
$.cache.reload().then(function(){
// 字体大小重载
let changeFontSize = ()=>
document.documentElement.style.fontSize
= VIEW.contentDocument.documentElement.style.fontSize
= CONFIG.globalFont + 'px';
$.cache.onChange('globalFont',changeFontSize);
changeFontSize();
// 页面标题
document.title = CONFIG.title;
// opener
$.module.opener = CONFIG.opener;
// 初始化主题
let theme = ['dark','white'];
if(!theme.includes(sessionStorage.mode) && CONFIG.autoTheme){
let hour = (new Date()).getHours();
sessionStorage.mode = (hour >= 20 || hour <=6)?'dark':'white';
}
// 初始化iframe
var hash = document.location.hash.substring(1).split('?');
VIEW.src = hash[0] == '' ? CONFIG.prefix : hash[0];
let param = {};
if(typeof hash[1] == 'string'){
for(let param of hash[1].split('&')){
let nv = decodeURIComponent(param).split('=');
param[nv[0]] = nv[1];
if(CONFIG.debug) console.log(nv[0],":",nv[1]);
}
if(CONFIG.debug) console.log('param found(in uri:"',hash[1],'")');
if(typeof param.open == 'string')
$.open(param.open);
}
window.location.param = param;
// 初始化dialog
if(CONFIG.animate){
let style = document.createElement('style');
style.innerHTML = '@keyframes fadein{from{opacity: 0;transform: translateX(-2rem);}to{opacity: 1;transform: translateX(0);}}@keyframes fadeout{from{opacity: 1;transform: translateX(0);}to{opacity: 0;transform: translateX(2rem);}}#msg > *{animation: fadein 0.3s forwards;}.fadeout{animation: fadeout 0.3s forwards !important;}@keyframes jumpin{from{opacity: 0;transform: scale(0);}to{opacity: 1;transform: scale(1);}}@keyframes jumpout{from{opacity: 1;transform: scale(1);}to{opacity: 0;transform: scale(0);}}.window{animation: jumpin 0.3s forwards;}.jumpout{animation: jumpout 0.3s forwards;}';
document.body.append(style);
}
// 加载扩展
for (var i = 0; i < CONFIG.extension.length; i++)
$.module.load(CONFIG.extension[i]);
});
VIEW.onload = function(){
// ctx.hidden = true;
if(VIEW.srcdoc != '' || VIEW.srcdoc == null) return;
try{
VIEW.contentDocument.location;
if(VIEW.srcdoc != '') VIEW.srcdoc = '';
}catch(e){
VIEW.onerror();
throw new Error('Network Error!Fatal Error trigged.');
}
$.init();
document.location.hash = '#'+VIEW.contentDocument.location.pathname;
VIEW.contentDocument.addEventListener('contextmenu', e => {
let x = e.clientX,y = e.clientY,
ctx = document.getElementById('ctx'),
pageY = document.documentElement.clientHeight,pageX = document.documentElement.clientWidth;
if(typeof ctx.ondisplay == 'function')
ctx.ondisplay($._find(e.target) || e.target);
if(ctx.clientWidth + x > pageX)
ctx.style.left = (x - ctx.clientWidth) + 'px';
else ctx.style.left = x + 'px';
if(ctx.clientHeight + y > pageY)
ctx.style.top = (y - ctx.clientHeight) + 'px';
else ctx.style.top = y + 'px';
ctx.hidden = false;
VIEW.contentDocument.onclick = document.onclick = function(e){
ctx.hidden = true, document.onclick = VIEW.contentDocument.onclick = null;
return false;
}
e.preventDefault();
});
let so = VIEW.contentDocument.getElementById('search');
so.onfocus = function(){
if(!CONFIG.search) return $.dialog.msg('warn','搜索','功能被管理员禁用!',5);
}
if(typeof CONFIG.search == 'string')
so.onchange = function(){
//...
}
$.tool.selector(VIEW.contentDocument,VIEW.contentDocument.querySelectorAll('table#list>tbody>tr'),{
'ArrowRight' : ()=>$.tool.tools.i = 0,
'AllowLeft' : ()=>void(0),
'Escape' : ()=>VIEW.contentWindow.history.back(),
'Enter' : ctrl=>{
ctrl.i.getElementsByTagName('a')[0].click();
$.tool.view.focus();
}
},'focus',element=>
element.scrollIntoView({
behavior: "smooth",
block: "center"
})
);
VIEW.focus();
};
</script>
</body>
</html>