-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1720 lines (1465 loc) · 66.4 KB
/
Copy pathindex.html
File metadata and controls
1720 lines (1465 loc) · 66.4 KB
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>视频文件 MD5 修改器</title>
<style>
:root {
/* 主题色 */
--primary-50: #E3F2FD;
--primary-100: #BBDEFB;
--primary-200: #90CAF9;
--primary-300: #64B5F6;
--primary-400: #42A5F5;
--primary-500: #2196F3;
--primary-600: #1E88E5;
--primary-700: #1976D2;
--primary-800: #1565C0;
--primary-900: #0D47A1;
/* 成功状态色 */
--success-50: #E8F5E9;
--success-500: #4CAF50;
--success-700: #388E3C;
/* 错误状态色 */
--error-50: #FFEBEE;
--error-500: #F44336;
--error-700: #D32F2F;
/* 警告状态色 */
--warning-50: #FFF3E0;
--warning-500: #FF9800;
--warning-700: #F57C00;
/* 中性色 */
--neutral-50: #FAFAFA;
--neutral-100: #F5F5F5;
--neutral-200: #EEEEEE;
--neutral-300: #E0E0E0;
--neutral-400: #BDBDBD;
--neutral-500: #9E9E9E;
--neutral-600: #757575;
--neutral-700: #616161;
--neutral-800: #424242;
--neutral-900: #212121;
/* 间距 */
--spacing-xs: 4px;
--spacing-sm: 8px;
--spacing-md: 16px;
--spacing-lg: 24px;
--spacing-xl: 32px;
/* 圆角 */
--radius-sm: 4px;
--radius-md: 8px;
--radius-lg: 16px;
/* 阴影 */
--shadow-sm: 0 2px 4px rgba(0,0,0,0.1);
--shadow-md: 0 4px 6px rgba(0,0,0,0.1);
--shadow-lg: 0 10px 15px rgba(0,0,0,0.1);
/* 动画 */
--transition-fast: 0.2s ease;
--transition-normal: 0.3s ease;
--transition-slow: 0.5s ease;
/* 字体 */
--font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
--font-size-xs: 12px;
--font-size-sm: 14px;
--font-size-md: 16px;
--font-size-lg: 18px;
--font-size-xl: 24px;
/* 新增状态色变量 */
--success-100: #C8E6C9;
--error-100: #FFCDD2;
--warning-100: #FFE0B2;
}
/* 重置样式 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: var(--font-family);
font-size: var(--font-size-md);
line-height: 1.6;
color: var(--neutral-900);
background: var(--neutral-100);
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.container {
max-width: 1000px;
margin: 0 auto;
padding: var(--spacing-xl);
}
.card {
background: white;
border-radius: var(--radius-md);
box-shadow: var(--shadow-md);
padding: var(--spacing-xl);
margin-bottom: var(--spacing-xl);
}
/* 标题样式 */
h1 {
font-size: var(--font-size-xl);
font-weight: 600;
color: var(--neutral-900);
text-align: center;
margin-bottom: var(--spacing-xl);
}
h2 {
font-size: var(--font-size-lg);
font-weight: 500;
color: var(--neutral-800);
margin-bottom: var(--spacing-md);
}
/* 按钮基础样式 */
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
padding: var(--spacing-sm) var(--spacing-lg);
border-radius: var(--radius-sm);
font-size: var(--font-size-md);
font-weight: 500;
border: none;
cursor: pointer;
transition: all var(--transition-fast);
gap: var(--spacing-sm);
color: white;
}
.btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
/* 按钮变体 */
.btn-primary {
background: var(--primary-500);
}
.btn-primary:hover:not(:disabled) {
background: var(--primary-600);
}
.btn-success {
background: var(--success-500);
}
.btn-success:hover:not(:disabled) {
background: var(--success-700);
}
.btn-warning {
background: var(--warning-500);
}
.btn-warning:hover:not(:disabled) {
background: var(--warning-700);
}
.btn-error {
background: var(--error-500);
}
.btn-error:hover:not(:disabled) {
background: var(--error-700);
}
.settings-panel {
background: white;
padding: var(--spacing-lg);
border-radius: var(--radius-md);
margin-bottom: var(--spacing-xl);
box-shadow: var(--shadow-sm);
}
.settings-panel h2 {
font-size: var(--font-size-lg);
font-weight: 500;
color: var(--neutral-800);
margin-bottom: var(--spacing-md);
display: flex;
align-items: center;
gap: var(--spacing-xs);
padding-left: var(--spacing-sm);
border-left: 4px solid var(--primary-500);
}
.settings-panel h2::before {
content: none;
}
.settings-content {
display: flex;
flex-wrap: wrap;
gap: var(--spacing-xl);
align-items: stretch;
}
.settings-item {
flex: 1;
min-width: 250px;
border: 1px solid var(--neutral-200);
border-radius: var(--radius-md);
padding: var(--spacing-lg);
background-color: white;
transition: all var(--transition-normal);
display: flex;
flex-direction: column;
}
.settings-item:hover {
border-color: var(--primary-200);
box-shadow: var(--shadow-sm);
}
.settings-item-title {
font-weight: 500;
margin-bottom: var(--spacing-lg);
display: flex;
align-items: center;
gap: var(--spacing-sm);
color: var(--neutral-800);
}
.settings-item-icon {
display: inline-flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
background-color: var(--primary-50);
border-radius: 50%;
color: var(--primary-500);
}
.settings-item-content {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
}
.suffix-input {
margin-bottom: 0;
}
.suffix-input label {
display: block;
margin-bottom: var(--spacing-sm);
color: var(--neutral-700);
font-size: var(--font-size-sm);
}
.suffix-input input {
width: 100%;
padding: var(--spacing-md);
border: 1px solid var(--neutral-300);
border-radius: var(--radius-sm);
font-size: var(--font-size-md);
transition: all var(--transition-fast);
}
.suffix-input input:focus {
outline: none;
border-color: var(--primary-500);
box-shadow: 0 0 0 3px var(--primary-100);
}
.directory-input {
display: flex;
flex-direction: column;
gap: var(--spacing-md);
}
.directory-btn-group {
display: flex;
align-items: center;
gap: var(--spacing-sm);
}
.directory-btn-group .btn {
padding: var(--spacing-md) var(--spacing-xl);
}
.selected-dir {
color: var(--neutral-700);
font-size: var(--font-size-sm);
padding: var(--spacing-md);
background: var(--neutral-50);
border: 1px solid var(--neutral-200);
border-radius: var(--radius-sm);
width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
position: relative;
}
.selected-dir::before {
content: '当前目录: ';
color: var(--neutral-500);
font-weight: normal;
}
/* 拖放区域 */
.drop-zone {
border: 2px dashed var(--neutral-300);
border-radius: var(--radius-lg);
padding: var(--spacing-xl) var(--spacing-lg);
text-align: center;
cursor: pointer;
transition: all var(--transition-normal);
background: white;
margin-bottom: var(--spacing-xl);
}
.drop-zone:hover {
border-color: var(--primary-500);
background: var(--primary-50);
}
.drop-zone.drag-over {
border-color: var(--primary-500);
background: var(--primary-50);
transform: scale(1.02);
}
/* 文件列表 */
.file-list {
display: flex;
flex-direction: column;
gap: var(--spacing-md);
}
.file-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: var(--spacing-lg);
border: 1px solid var(--neutral-200);
border-radius: var(--radius-md);
background: white;
transition: all var(--transition-normal);
}
.file-item:hover {
border-color: var(--primary-200);
box-shadow: var(--shadow-sm);
}
.file-info {
flex: 1;
min-width: 0;
}
.file-name {
font-weight: 500;
color: var(--neutral-900);
margin-bottom: var(--spacing-xs);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.file-size {
font-size: var(--font-size-sm);
color: var(--neutral-600);
}
.md5-info {
font-family: monospace;
font-size: var(--font-size-sm);
color: var(--neutral-700);
margin-top: var(--spacing-sm);
background: var(--neutral-50);
padding: var(--spacing-sm);
border-radius: var(--radius-sm);
word-break: break-all;
}
.file-actions {
display: flex;
gap: var(--spacing-sm);
margin-left: var(--spacing-md);
}
/* 进度条 */
.progress {
height: 6px;
background: var(--neutral-100);
border-radius: var(--radius-sm);
overflow: hidden;
margin-top: var(--spacing-sm);
}
.progress-bar {
height: 100%;
background: var(--primary-500);
width: 0;
transition: width var(--transition-normal);
}
.status {
font-size: var(--font-size-sm);
color: var(--neutral-600);
margin-top: var(--spacing-sm);
}
/* Toast 通知 */
.toast-container {
display: none; /* 不再使用通知系统 */
}
.toast {
display: none; /* 不再使用通知系统 */
}
@keyframes toastIn {
from {
opacity: 0;
transform: translateY(-20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes toastOut {
to {
opacity: 0;
transform: translateY(-20px);
}
}
/* 全局状态面板 */
.global-status-panel {
background: white;
border: 1px solid var(--neutral-200);
border-radius: var(--radius-md);
padding: var(--spacing-lg);
margin: var(--spacing-xl) 0;
display: flex;
align-items: center;
justify-content: space-between;
position: relative;
overflow: hidden;
box-shadow: var(--shadow-sm);
}
.global-status-panel::before {
content: '';
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 4px;
}
.global-status-panel.processing {
background: var(--primary-50);
border-color: var(--primary-100);
}
.global-status-panel.success {
background: var(--success-50);
border-color: var(--success-100);
}
.global-status-panel.error {
background: var(--error-50);
border-color: var(--error-100);
}
.global-status-panel.processing::before {
background: var(--primary-500);
}
.global-status-panel.success::before {
background: var(--success-500);
}
.global-status-panel.error::before {
background: var(--error-500);
}
.global-status-panel .status-text {
flex: 1;
padding-left: var(--spacing-md);
}
.global-status-panel .status-message {
font-weight: 500;
color: var(--neutral-900);
font-size: var(--font-size-md);
margin-bottom: var(--spacing-xs);
}
.global-status-panel .status-info {
font-size: var(--font-size-sm);
color: var(--neutral-600);
}
.global-status-panel .status-counter {
font-size: var(--font-size-sm);
padding: var(--spacing-xs) var(--spacing-sm);
border-radius: var(--radius-sm);
min-width: 80px;
text-align: center;
background: var(--neutral-100);
color: var(--neutral-700);
}
.global-status-panel.processing .status-counter {
background: var(--primary-100);
color: var(--primary-700);
}
.global-status-panel.success .status-counter {
background: var(--success-100);
color: var(--success-700);
}
.global-status-panel.error .status-counter {
background: var(--error-100);
color: var(--error-700);
}
/* 下载进度覆盖层 */
.download-overlay {
display: none; /* 隐藏弹窗,不再使用 */
}
/* 响应式设计 */
@media (max-width: 768px) {
.container {
padding: var(--spacing-md);
}
.file-item {
flex-direction: column;
gap: var(--spacing-md);
}
.file-actions {
width: 100%;
justify-content: flex-end;
margin-left: 0;
}
.directory-input {
flex-direction: column;
align-items: flex-start;
}
.selected-dir {
max-width: 100%;
}
.download-overlay-content {
padding: var(--spacing-lg);
}
}
.button-group {
display: flex;
gap: var(--spacing-md);
margin: var(--spacing-lg) 0;
justify-content: flex-end;
}
.drop-zone p {
margin: 0;
font-size: var(--font-size-md);
color: var(--neutral-600);
}
@keyframes pulse {
0% { box-shadow: 0 0 0 0 rgba(33, 150, 243, 0.4); }
70% { box-shadow: 0 0 0 10px rgba(33, 150, 243, 0); }
100% { box-shadow: 0 0 0 0 rgba(33, 150, 243, 0); }
}
.global-status-panel.processing {
animation: pulse 2s infinite;
}
/* 页脚样式 */
.footer {
text-align: center;
padding: var(--spacing-md) 0;
margin-top: var(--spacing-xl);
color: var(--neutral-600);
font-size: var(--font-size-sm);
}
.footer a {
color: var(--primary-500);
text-decoration: none;
transition: color var(--transition-fast);
display: inline-flex;
align-items: center;
gap: var(--spacing-xs);
}
.footer a:hover {
color: var(--primary-700);
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<div class="card">
<h1>视频文件 MD5 修改器</h1>
<div class="settings-panel">
<h2>设置</h2>
<div class="settings-content">
<div class="settings-item">
<div class="settings-item-title">
<div class="settings-item-icon">
<svg viewBox="0 0 24 24" width="18" height="18" stroke="currentColor" stroke-width="2" fill="none">
<path d="M12 6v12M6 12h12" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
</div>
文件后缀
</div>
<div class="settings-item-content">
<div class="suffix-input">
<label for="customSuffix">设置修改后文件的自定义后缀:</label>
<input type="text" id="customSuffix" placeholder="例如: -new" value="-new">
</div>
</div>
</div>
<div class="settings-item">
<div class="settings-item-title">
<div class="settings-item-icon">
<svg viewBox="0 0 24 24" width="18" height="18" stroke="currentColor" stroke-width="2" fill="none">
<path d="M4 20h16a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.93a2 2 0 0 1-1.66-.9l-.82-1.2A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13c0 1.1.9 2 2 2Z" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
</div>
保存目录
</div>
<div class="settings-item-content">
<div class="directory-input">
<div class="directory-btn-group">
<button class="btn btn-primary" onclick="selectSaveDirectory()">选择目录</button>
</div>
<div class="selected-dir" id="selectedDirPath">未选择目录</div>
</div>
</div>
</div>
</div>
</div>
<div id="globalStatusPanel" class="global-status-panel" style="display: none;">
<div class="status-text">
<div id="globalStatusMessage" class="status-message">准备就绪</div>
<div id="globalStatusInfo" class="status-info"></div>
</div>
<div id="globalStatusCounter" class="status-counter" style="display: none;">
0/0 完成
</div>
</div>
<div class="drop-zone" id="dropZone">
<p>拖放文件到此处或点击选择文件</p>
<input type="file" id="fileInput" multiple accept=".mp4,.avi,.mkv,.mov,.rmvb" style="display: none">
</div>
<div class="button-group">
<button id="downloadAllBtn" class="btn btn-success" disabled onclick="downloadAllFiles()">
<span>下载全部</span>
</button>
<button id="reprocessAllBtn" class="btn btn-warning" onclick="reprocessAllFiles()">
<span>重新处理全部</span>
</button>
<button id="clearAllBtn" class="btn btn-error" onclick="clearAllFiles()">
<span>删除所有</span>
</button>
</div>
<div class="file-list" id="fileList"></div>
</div>
<footer class="footer">
<a href="https://github.com/kokojacket/md5-modifier" target="_blank" rel="noopener noreferrer">
<svg height="20" width="20" viewBox="0 0 16 16" fill="currentColor" style="vertical-align: -0.125em;">
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"></path>
</svg>
<span>GitHub 项目地址</span>
</a>
</footer>
</div>
<script>
const ALLOWED_EXTENSIONS = ['.mp4', '.avi', '.mkv', '.mov', '.rmvb'];
const files = new Map();
let selectedDirHandle = null;
// 尝试恢复之前的目录句柄
async function tryRestoreDirectoryHandle() {
try {
// 检查是否支持File System Access API
if (!('showDirectoryPicker' in window) || !('indexedDB' in window)) {
return false;
}
// 尝试从IndexedDB获取保存的目录句柄
const dirHandle = await getSavedDirectoryHandle();
if (!dirHandle) {
return false;
}
try {
// 请求权限
const permission = await dirHandle.requestPermission({ mode: 'readwrite' });
if (permission === 'granted') {
selectedDirHandle = dirHandle;
// 更新UI显示选中的目录
const pathSpan = document.getElementById('selectedDirPath');
pathSpan.textContent = dirHandle.name;
pathSpan.title = `已选择目录: ${dirHandle.name}`;
updateGlobalStatus(`已恢复保存目录: ${dirHandle.name}`, 'success', false, '您现在可以从该目录选择文件或拖放文件到此处');
return true;
}
} catch (permErr) {
console.error('请求目录权限失败:', permErr);
}
// 如果恢复失败,清除保存的句柄
clearSavedDirectoryHandle();
return false;
} catch (err) {
console.error('恢复目录句柄时出错:', err);
return false;
}
}
// 使用IndexedDB存储目录句柄
async function saveDirectoryHandle(dirHandle) {
return new Promise((resolve, reject) => {
try {
if (!dirHandle) {
resolve(false);
return;
}
const request = indexedDB.open('MD5ModifierDB', 1);
request.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains('handles')) {
db.createObjectStore('handles', { keyPath: 'id' });
}
};
request.onsuccess = (event) => {
const db = event.target.result;
const transaction = db.transaction(['handles'], 'readwrite');
const store = transaction.objectStore('handles');
const storeRequest = store.put({
id: 'saveDir',
handle: dirHandle
});
storeRequest.onsuccess = () => resolve(true);
storeRequest.onerror = () => {
console.error("存储目录句柄失败");
resolve(false);
};
};
request.onerror = (event) => {
console.error("打开数据库失败:", event.target.error);
resolve(false);
};
} catch (err) {
console.error('保存目录句柄失败:', err);
resolve(false);
}
});
}
// 从IndexedDB获取保存的目录句柄
async function getSavedDirectoryHandle() {
return new Promise((resolve, reject) => {
try {
const request = indexedDB.open('MD5ModifierDB', 1);
request.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains('handles')) {
db.createObjectStore('handles', { keyPath: 'id' });
}
};
request.onsuccess = (event) => {
const db = event.target.result;
try {
const transaction = db.transaction(['handles'], 'readonly');
const store = transaction.objectStore('handles');
const getRequest = store.get('saveDir');
getRequest.onsuccess = () => {
if (getRequest.result) {
resolve(getRequest.result.handle);
} else {
resolve(null);
}
};
getRequest.onerror = () => {
console.error("获取目录句柄失败");
resolve(null);
};
} catch (txError) {
console.error("事务错误:", txError);
resolve(null);
}
};
request.onerror = (event) => {
console.error("打开数据库失败:", event.target.error);
resolve(null);
};
} catch (err) {
console.error('获取目录句柄失败:', err);
resolve(null);
}
});
}
// 清除保存的目录句柄
async function clearSavedDirectoryHandle() {
return new Promise((resolve) => {
try {
const request = indexedDB.open('MD5ModifierDB', 1);
request.onsuccess = (event) => {
const db = event.target.result;
const transaction = db.transaction(['handles'], 'readwrite');
const store = transaction.objectStore('handles');
const deleteRequest = store.delete('saveDir');
deleteRequest.onsuccess = () => resolve(true);
deleteRequest.onerror = () => {
console.error("删除目录句柄失败");
resolve(false);
};
};
request.onerror = () => {
console.error("打开数据库失败");
resolve(false);
};
} catch (err) {
console.error('清除目录句柄失败:', err);
resolve(false);
}
});
}
// 选择保存目录
async function selectSaveDirectory() {
try {
if (!('showDirectoryPicker' in window)) {
updateGlobalStatus('您的浏览器不支持选择目录功能', 'error', false, '文件将保存到默认下载位置');
return;
}
const dirHandle = await window.showDirectoryPicker({
mode: 'readwrite',
// 添加可选的提示信息
id: 'md5ModifierSaveDir',
startIn: 'downloads'
});
selectedDirHandle = dirHandle;
// 更新UI显示选中的目录
const pathSpan = document.getElementById('selectedDirPath');
pathSpan.textContent = dirHandle.name;
pathSpan.title = `已选择目录: ${dirHandle.name}`;
updateGlobalStatus(`已成功选择保存目录: ${dirHandle.name}`, 'success', false, '您现在可以从该目录选择文件或拖放文件到此处');
// 保存目录句柄
await saveDirectoryHandle(dirHandle);
} catch (err) {
if (err.name === 'AbortError') {
updateGlobalStatus('用户取消了目录选择', 'error', false);
} else {
console.error('选择目录失败:', err);
updateGlobalStatus('选择目录失败: ' + err.message, 'error', false);
}
}
}
// 将showToast函数改为updateGlobalStatus函数的简单封装
function showToast(message, type = 'success') {
// 直接使用updateGlobalStatus,不再创建Toast元素
updateGlobalStatus(message, type, false);
}
// 更新全局状态面板
function updateGlobalStatus(message, type = 'info', processing = false, details = '', completed = null, total = null) {
const panel = document.getElementById('globalStatusPanel');
const statusMessage = document.getElementById('globalStatusMessage');
const statusInfo = document.getElementById('globalStatusInfo');
const statusCounter = document.getElementById('globalStatusCounter');
// 显示面板
panel.style.display = 'flex';
// 更新样式
panel.className = 'global-status-panel';
panel.classList.add(type);
if (processing) {
panel.classList.add('processing');
}
// 更新消息
statusMessage.textContent = message;
statusInfo.textContent = details || '';
// 更新计数器
if (completed !== null && total !== null) {
statusCounter.style.display = 'block';
statusCounter.textContent = `${completed}/${total} 完成`;
} else {
statusCounter.style.display = 'none';
}
}
// 清除全局状态
function clearGlobalStatus() {
const panel = document.getElementById('globalStatusPanel');
panel.style.display = 'none';
}
// 修改保存文件到指定目录的函数
async function saveFileToDirectory(fileData, fileName) {
if (!selectedDirHandle) {
return false;
}
try {
updateGlobalStatus(`正在保存到目录: ${fileName}`, 'info', true, `目标目录: ${selectedDirHandle.name}`);
// 检查文件是否已存在并处理重名情况
let finalFileName = fileName;
let fileExists = true;
let counter = 1;
// 文件存在性检查函数
const checkFileExists = async (name) => {
try {
await selectedDirHandle.getFileHandle(name);
return true; // 文件存在
} catch (err) {
if (err.name === 'NotFoundError') {
return false; // 文件不存在
}
throw err; // 其他错误
}
};
// 生成新文件名的函数
const generateNewFileName = (origName, num) => {
const dotIndex = origName.lastIndexOf('.');
if (dotIndex === -1) {
// 没有扩展名
return `${origName}-${num}`;
} else {
// 有扩展名,在扩展名前添加计数
const name = origName.substring(0, dotIndex);
const ext = origName.substring(dotIndex);
return `${name}-${num}${ext}`;