File size: 44,256 Bytes
0a0eb07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b08e6e
 
0a0eb07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b08e6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a0eb07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b08e6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a0eb07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b08e6e
 
 
 
 
 
 
0a0eb07
3b08e6e
0a0eb07
3b08e6e
0a0eb07
3b08e6e
0a0eb07
3b08e6e
0a0eb07
3b08e6e
 
 
 
0a0eb07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b08e6e
0a0eb07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b08e6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a0eb07
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网易云音乐工具箱</title>
    <link href="https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/twitter-bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/aplayer/1.10.1/APlayer.min.css">
    <style>
        body {
            background-color: #f8f9fa;
            color: #333;
        }
        .container {
            max-width: 800px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .btn-primary {
            margin-top: 20px;
            background-color: #007bff;
            border-color: #007bff;
        }
        .btn-primary:hover {
            background-color: #0056b3;
            border-color: #004085;
        }
        .btn-success {
            margin-top: 20px;
            background-color: #28a745;
            border-color: #28a745;
        }
        .btn-success:hover {
            background-color: #218838;
            border-color: #1e7e34;
        }
        .btn-warning {
            margin-top: 20px;
            background-color: #ffc107;
            border-color: #ffc107;
        }
        .btn-warning:hover {
            background-color: #e0a800;
            border-color: #d39e00;
        }
        #song-info {
            margin-top: 20px;
        }
        #song-info img {
            max-width: 100%;
            border-radius: 8px;
        }
        .alert-info {
            background-color: #d1ecf1;
            color: #0c5460;
            border-color: #bee5eb;
        }
        /* 歌曲/歌单标题过长自动省略号 */
        .song-title, .playlist-title {
            display: inline-block;
            max-width: 180px;
            vertical-align: middle;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        /* 歌单列表按钮不换行 */
        .list-group-item .select-song {
            flex-shrink: 0;
            margin-left: 10px;
        }
        /* 歌词区域美化 */
        .lyric-box {
            max-height: 180px;
            overflow-y: auto;
            background: linear-gradient(90deg,#f7f7fa 60%,#f0f4fa 100%);
            border-radius: 8px;
            padding: 12px 16px;
            font-size: 15px;
            color: #222;
            box-shadow: 0 1px 4px rgba(0,0,0,0.04);
            line-height: 1.7;
            margin-bottom: 0;
        }
    </style>
</head>
<body>
    <div class="container mt-5">
        <h1 class="text-center mb-4">网易云音乐工具箱</h1>
        <div class="card shadow-sm">
            <div class="card-body">
                <form id="main-form">
                    <div class="mb-3">
                        <label class="form-label">功能选择</label>
                        <select id="mode-select" class="form-select">
                            <option value="search">歌曲搜索</option>
                            <option value="parse">单曲解析</option>
                            <option value="playlist">歌单解析</option>
                            <option value="album">专辑解析</option>
                            <option value="download">音乐下载</option>
                            <option value="mv-search">MV搜索</option>
                            <option value="mv-parse">MV解析</option>
                        </select>
                    </div>
                    <div id="search-area">
                        <div class="mb-3">
                            <label for="search_keywords" class="form-label">搜索关键词</label>
                            <input type="text" id="search_keywords" class="form-control" placeholder="输入关键词进行搜索">
                        </div>
                        <div class="mb-3">
                            <label for="search_limit" class="form-label">返回数量</label>
                            <input type="number" id="search_limit" class="form-control" value="10" min="1" max="50">
                        </div>
                        <div class="text-center">
                            <button type="button" id="search-btn" class="btn btn-success w-50">搜索</button>
                        </div>
                    </div>
                    <div id="parse-area" style="display:none;">
                        <div class="mb-3">
                            <label for="song_ids" class="form-label">歌曲ID或URL</label>
                            <input type="text" id="song_ids" class="form-control" placeholder="输入歌曲ID或URL">
                        </div>
                        <div class="mb-3">
                            <label for="level" class="form-label">音质选择</label>
                            <select id="level" class="form-select">
                                <option value="standard">标准音质</option>
                                <option value="exhigh">极高音质</option>
                                <option value="lossless">无损音质</option>
                                <option value="hires">Hires音质</option>
                                <option value="sky">沉浸环绕声</option>
                                <option value="jyeffect">高清环绕声</option>
                                <option value="jymaster">超清母带</option>
                            </select>
                        </div>
                        <div class="text-center">
                            <button type="button" id="parse-btn" class="btn btn-primary w-50">解析</button>
                        </div>
                    </div>
                    <div id="playlist-area" style="display:none;">
                        <div class="mb-3">
                            <label for="playlist_id" class="form-label">歌单ID或链接</label>
                            <input type="text" id="playlist_id" class="form-control" placeholder="输入歌单ID或网易云歌单链接">
                        </div>
                        <div class="text-center">
                            <button type="button" id="playlist-btn" class="btn btn-warning w-50">解析歌单</button>
                        </div>
                    </div>
                    <div id="album-area" style="display:none;">
                        <div class="mb-3">
                            <label for="album_id" class="form-label">专辑ID或链接</label>
                            <input type="text" id="album_id" class="form-control" placeholder="输入专辑ID或网易云专辑链接">
                        </div>
                        <div class="text-center">
                            <button type="button" id="album-btn" class="btn btn-info w-50">解析专辑</button>
                        </div>
                    </div>
                    <div id="download-area" style="display:none;">
                        <div class="mb-3">
                            <label for="download_id" class="form-label">音乐ID或URL</label>
                            <input type="text" id="download_id" class="form-control" placeholder="输入音乐ID或网易云音乐链接">
                        </div>
                        <div class="mb-3">
                            <label for="download_quality" class="form-label">下载音质</label>
                            <select id="download_quality" class="form-select">
                                <option value="standard">标准音质</option>
                                <option value="exhigh">极高音质</option>
                                <option value="lossless" selected>无损音质</option>
                                <option value="hires">Hires音质</option>
                                <option value="sky">沉浸环绕声</option>
                                <option value="jyeffect">高清环绕声</option>
                                <option value="jymaster">超清母带</option>
                            </select>
                        </div>
                        <div class="text-center">
                            <button type="button" id="download-btn" class="btn btn-success w-50">下载音乐</button>
                        </div>
                        <div id="download-progress" class="mt-3 d-none">
                            <div class="progress">
                                <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" style="width: 100%"></div>
                            </div>
                            <div class="text-center mt-2">
                                <small class="text-muted">正在下载并写入元信息...</small>
                            </div>
                        </div>
                    </div>
                    <!-- MV搜索区域 -->
                    <div id="mv-search-area" style="display:none;">
                        <div class="mb-3">
                            <label for="mv_search_keywords" class="form-label">MV搜索关键词</label>
                            <input type="text" id="mv_search_keywords" class="form-control" placeholder="输入关键词搜索MV">
                        </div>
                        <div class="mb-3">
                            <label for="mv_search_limit" class="form-label">返回数量</label>
                            <input type="number" id="mv_search_limit" class="form-control" value="10" min="1" max="50">
                        </div>
                        <div class="text-center">
                            <button type="button" id="mv-search-btn" class="btn btn-success w-50">搜索MV</button>
                        </div>
                    </div>
                    <!-- MV解析区域 -->
                    <div id="mv-parse-area" style="display:none;">
                        <div class="mb-3">
                            <label for="mv_id" class="form-label">MV ID或URL</label>
                            <input type="text" id="mv_id" class="form-control" placeholder="输入MV ID或网易云MV链接">
                        </div>
                        <div class="mb-3">
                            <label for="mv_quality" class="form-label">画质选择</label>
                            <select id="mv_quality" class="form-select">
                                <option value="240">240P (标清)</option>
                                <option value="480">480P (标清)</option>
                                <option value="720" selected>720P (高清)</option>
                                <option value="1080">1080P (全高清)</option>
                            </select>
                        </div>
                        <div class="text-center">
                            <button type="button" id="mv-parse-btn" class="btn btn-primary w-50">解析MV</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <!-- 搜索结果列表 -->
        <div id="search-result" class="mt-4 d-none">
            <h5>搜索结果:</h5>
            <ul class="list-group" id="search-list"></ul>
        </div>
        <!-- 结果展示区域 -->
        <div id="song-info" class="alert alert-info d-none mt-4 p-0 border-0" style="box-shadow:0 2px 8px rgba(0,0,0,0.07);">
            <div class="row g-0 align-items-stretch">
                <div class="col-md-8 p-3 d-flex flex-column justify-content-between">
                    <h4 class="mb-2" id="song_name" style="font-weight:700;"></h4>
                    <div class="mb-2"><span class="badge bg-primary me-2">歌手</span><span id="artist_names"></span></div>
                    <div class="mb-2"><span class="badge bg-secondary me-2">专辑</span><span id="song_alname"></span></div>
                    <div class="mb-2"><span class="badge bg-success me-2">音质</span><span id="song_level"></span></div>
                    <div class="mb-2"><span class="badge bg-warning text-dark me-2">大小</span><span id="song_size"></span></div>
                    <div class="mb-2">
                        <button id="show-big-pic" type="button" class="btn btn-outline-info btn-sm me-2" style="vertical-align:middle;">显示大图</button>
                    </div>
                    <div class="mb-2"><span class="badge bg-info text-dark me-2">链接</span><a id="song_url" href="" target="_blank">点击下载</a></div>
                    <div class="mb-2"><span class="badge bg-dark me-2">歌词</span></div>
                    <div class="lyric-box" id="lyric"></div>
                </div>
            </div>
            <div class="row g-0">
                <div class="col-12 p-3 pt-0">
                    <div id="aplayer"></div>
                </div>
            </div>
        </div>
        <!-- 歌单解析结果 -->
        <div id="playlist-result" class="mt-4 d-none">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex align-items-center mb-3">
                        <img id="playlist-cover" src="" alt="cover" style="width:60px;height:60px;object-fit:cover;border-radius:8px;margin-right:15px;">
                        <div>
                            <h5 id="playlist-name" class="mb-1"></h5>
                            <div class="text-muted" id="playlist-creator"></div>
                        </div>
                    </div>
                    <div id="playlist-desc" class="mb-2 text-secondary small"></div>
                    <div>共 <span id="playlist-count"></span> 首歌</div>
                </div>
            </div>
            <ul class="list-group mt-3" id="playlist-tracks"></ul>
        </div>
        <!-- 专辑解析结果 -->
        <div id="album-result" class="mt-4 d-none">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex align-items-center mb-3">
                        <img id="album-cover" src="" alt="cover" style="width:60px;height:60px;object-fit:cover;border-radius:8px;margin-right:15px;">
                        <div>
                            <h5 id="album-name" class="mb-1"></h5>
                            <div class="text-muted" id="album-artist"></div>
                        </div>
                    </div>
                    <div id="album-desc" class="mb-2 text-secondary small"></div>
                    <div>共 <span id="album-count"></span> 首歌</div>
                </div>
            </div>
            <ul class="list-group mt-3" id="album-tracks"></ul>
        </div>
        <!-- MV搜索结果 -->
        <div id="mv-search-result" class="mt-4 d-none">
            <h5>MV搜索结果:</h5>
            <ul class="list-group" id="mv-search-list"></ul>
        </div>
        <!-- MV解析结果 -->
        <div id="mv-info" class="alert alert-info d-none mt-4 p-0 border-0" style="box-shadow:0 2px 8px rgba(0,0,0,0.07);">
            <div class="row g-0">
                <div class="col-md-4">
                    <img id="mv-cover" src="" alt="MV封面" style="width:100%;height:100%;object-fit:cover;border-radius:8px 0 0 8px;">
                </div>
                <div class="col-md-8 p-3">
                    <h4 class="mb-2" id="mv_name" style="font-weight:700;"></h4>
                    <div class="mb-2"><span class="badge bg-primary me-2">艺术家</span><span id="mv_artist"></span></div>
                    <div class="mb-2"><span class="badge bg-secondary me-2">时长</span><span id="mv_duration"></span></div>
                    <div class="mb-2"><span class="badge bg-success me-2">画质</span><span id="mv_quality_display"></span></div>
                    <div class="mb-2"><span class="badge bg-warning text-dark me-2">大小</span><span id="mv_size"></span></div>
                    <div class="mb-2"><span class="badge bg-info text-dark me-2">播放量</span><span id="mv_play_count"></span></div>
                    <div class="mb-3"><span class="badge bg-danger me-2">发布时间</span><span id="mv_publish_time"></span></div>
                    <div class="mb-2">
                        <a id="mv_url" href="" target="_blank" class="btn btn-primary btn-sm me-2">在线播放</a>
                        <button id="mv-download-link" class="btn btn-success btn-sm">下载链接</button>
                    </div>
                    <div class="mt-3">
                        <video id="mv-player" controls style="width:100%;max-height:400px;border-radius:8px;" class="d-none"></video>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Modal for big picture -->
    <div class="modal fade" id="bigPicModal" tabindex="-1" aria-labelledby="bigPicModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="bigPicModalLabel">大图预览</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body text-center">
                    <img id="big-pic-img" src="" alt="大图" style="max-width:100%;max-height:60vh;border-radius:10px;box-shadow:0 2px 12px rgba(0,0,0,0.12);">
                </div>
            </div>
        </div>
    </div>
    <footer class="footer mt-5 py-3 bg-light border-top">
        <div class="container text-center text-muted small">
            <span>网易云音乐工具箱 &copy; 2025 | Protect What You Love</span>
        </div>
    </footer>
    <script src="https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <script src="https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/aplayer/1.10.1/APlayer.min.js"></script>
    <script src="https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/twitter-bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
    <script>
        $(document).ready(function() {
            function lrctrim(lyrics) {
                const lines = lyrics.split('\n');
                const data = [];

                lines.forEach((line, index) => {
                    const matches = line.match(/\[(\d{2}):(\d{2}[\.:]?\d*)]/);
                    if (matches) {
                        const minutes = parseInt(matches[1], 10);
                        const seconds = parseFloat(matches[2].replace('.', ':')) || 0;
                        const timestamp = minutes * 60000 + seconds * 1000;

                        let text = line.replace(/\[\d{2}:\d{2}[\.:]?\d*\]/g, '').trim();
                        text = text.replace(/\s\s+/g, ' '); // Replace multiple spaces with a single space

                        data.push([timestamp, index, text]);
                    }
                });

                data.sort((a, b) => a[0] - b[0]);

                return data;
            }

            function lrctran(lyric, tlyric) {
                lyric = lrctrim(lyric);
                tlyric = lrctrim(tlyric);

                let len1 = lyric.length;
                let len2 = tlyric.length;
                let result = "";

                for (let i = 0, j = 0; i < len1 && j < len2; i++) {
                    while (lyric[i][0] > tlyric[j][0] && j + 1 < len2) {
                        j++;
                    }

                    if (lyric[i][0] === tlyric[j][0]) {
                        tlyric[j][2] = tlyric[j][2].replace('/', '');
                        if (tlyric[j][2]) {
                            lyric[i][2] += ` (翻译:${tlyric[j][2]})`;
                        }
                        j++;
                    }
                }

                for (let i = 0; i < len1; i++) {
                    let t = lyric[i][0];
                    result += `[${String(Math.floor(t / 60000)).padStart(2, '0')}:${String(Math.floor((t % 60000) / 1000)).padStart(2, '0')}.${String(t % 1000).padStart(3, '0')}]${lyric[i][2]}\n`;
                }

                return result;
            }

            function extractLinks(text) {
                var regex = /https?:\/\/\S+/g;
                var matches = text.match(regex);
                if (matches) {
                    return matches[0];
                } else {
                    return '';
                }
            }
    
            function checkValidLink(link) {
                if (link.indexOf("http") === -1 || 
                    (link.indexOf("music.163.com") === -1 && link.indexOf("163cn.tv") === -1)) {
                    return false;
                }
                return true;
            }
    
            function extractAndCheckId(text) {
                var link = extractLinks(text);
                if (checkValidLink(link)) {
                    return link;
                } else {
                    var idRegex = /\b\d+\b/g;
                    var ids = text.match(idRegex);
                    if (ids && ids.length > 0) {
                        return ids[0];
                    }
                    return '';
                }
            }

            // 切换功能区
            $('#mode-select').on('change', function() {
                // 隐藏所有区域
                $('#search-area, #parse-area, #playlist-area, #album-area, #download-area, #mv-search-area, #mv-parse-area').hide();
                $('#search-result, #song-info, #playlist-result, #album-result, #mv-search-result, #mv-info').addClass('d-none');
                
                // 根据选择显示对应区域
                const mode = $(this).val();
                if (mode === 'search') {
                    $('#search-area').show();
                } else if (mode === 'parse') {
                    $('#parse-area').show();
                } else if (mode === 'playlist') {
                    $('#playlist-area').show();
                } else if (mode === 'album') {
                    $('#album-area').show();
                } else if (mode === 'download') {
                    $('#download-area').show();
                } else if (mode === 'mv-search') {
                    $('#mv-search-area').show();
                } else if (mode === 'mv-parse') {
                    $('#mv-parse-area').show();
                }
            });

            // 搜索功能
            $('#search-btn').on('click', function() {
                const keywords = $('#search_keywords').val();
                const limit = $('#search_limit').val();
                if (!keywords) {
                    alert('请输入搜索关键词');
                    return;
                }
                $.ajax({
                    url: '/Search',
                    method: 'POST',
                    data: { keyword: keywords, limit: limit },
                    dataType: 'json',
                    success: function(data) {
                        if (data.status === 200) {
                            $('#search-list').empty();
                            data.data.forEach(function(song) {
                                const item = `<li class="list-group-item d-flex justify-content-between align-items-center">
                                    <div>
                                        <img src="${song.picUrl}" alt="cover" style="width:40px;height:40px;object-fit:cover;border-radius:4px;margin-right:10px;">
                                        <strong class='song-title'>${song.name}</strong> - <span>${song.artists}</span> <span class="text-muted">[${song.album}]</span>
                                    </div>
                                    <div>
                                        <button class="btn btn-sm btn-outline-primary select-song me-2" data-id="${song.id}" data-name="${song.name}">解析</button>
                                        <button class="btn btn-sm btn-success download-song" data-id="${song.id}" data-name="${song.name}">下载</button>
                                    </div>
                                </li>`;
                                $('#search-list').append(item);
                            });
                            $('#search-result').removeClass('d-none');
                        } else {
                            $('#search-list').html('<li class="list-group-item">未找到相关歌曲</li>');
                            $('#search-result').removeClass('d-none');
                        }
                    },
                    error: function() {
                        $('#search-list').html('<li class="list-group-item">搜索失败,请重试</li>');
                        $('#search-result').removeClass('d-none');
                    }
                });
            });

            // 搜索结果点击解析
            $(document).on('click', '.select-song', function() {
                const songId = $(this).data('id');
                $('#song_ids').val(songId);
                $('#mode-select').val('parse').trigger('change');
                $('html,body').animate({scrollTop: $('#main-form').offset().top}, 300);
            });

            // 单曲解析
            $('#parse-btn').on('click', function() {
                const songIds = $('#song_ids').val();
                const level = $('#level').val();
                if (!songIds) {
                    alert('请输入歌曲ID或URL');
                    return;
                }
                $.post('/Song_V1', { url: songIds, level: level, type:'json' }, function(data) {
                    if (data.status === 200) {
                        $('#song_name').text(data.data.name);
                        $('#artist_names').text(data.data.ar_name);
                        $('#song_alname').text(data.data.al_name);
                        $('#song_level').text(data.data.level);
                        $('#song_size').text(data.data.size);
                        let processedLyrics = data.data.lyric;
                        if (data.data.tlyric) {
                            processedLyrics = lrctran(data.data.lyric, data.data.tlyric);
                        }
                        $('#lyric').html(processedLyrics.replace(/\n/g, '<br>'));
                        $('#song_url').attr('href', data.data.url).text('点击下载');
                        $('#song-info').removeClass('d-none');
                        $('#show-big-pic').data('pic', data.data.pic);
                        new APlayer({
                            container: document.getElementById('aplayer'),
                            lrcType: 1,
                            audio: [{
                                name: data.data.name,
                                artist: data.data.ar_name,
                                url: data.data.url,
                                cover: data.data.pic,
                                lrc: processedLyrics
                            }]
                        });
                    } else {
                        alert(data.data.msg);
                    }
                }, 'json');
            });

            // 显示大图按钮事件
            $(document).on('click', '#show-big-pic', function() {
                var picUrl = $(this).data('pic');
                $('#big-pic-img').attr('src', picUrl);
                var modal = new bootstrap.Modal(document.getElementById('bigPicModal'));
                modal.show();
            });

            // 歌单解析
            $('#playlist-btn').on('click', function() {
                let pid = $('#playlist_id').val().trim();
                if (!pid) {
                    alert('请输入歌单ID或链接');
                    return;
                }
                // 支持直接粘贴歌单链接
                const idMatch = pid.match(/playlist\?id=(\d+)/);
                if (idMatch) pid = idMatch[1];
                $.get('/Playlist', { id: pid }, function(data) {
                    if (data.status === 200) {
                        const pl = data.data.playlist;
                        $('#playlist-cover').attr('src', pl.coverImgUrl);
                        $('#playlist-name').text(pl.name);
                        $('#playlist-creator').text('by ' + pl.creator);
                        $('#playlist-desc').text(pl.description || '');
                        $('#playlist-count').text(pl.trackCount);
                        $('#playlist-tracks').empty();
                        pl.tracks.forEach(function(song, idx) {
                            const item = `<li class="list-group-item d-flex justify-content-between align-items-center">
                                <div>
                                    <img src="${song.picUrl}" alt="cover" style="width:32px;height:32px;object-fit:cover;border-radius:4px;margin-right:8px;">
                                    <strong class="playlist-title">${idx+1}. ${song.name}</strong> - <span>${song.artists}</span> <span class="text-muted">[${song.album}]</span>
                                </div>
                                <div>
                                    <button class="btn btn-sm btn-outline-primary select-song me-2" data-id="${song.id}" data-name="${song.name}">解析</button>
                                    <button class="btn btn-sm btn-success download-song" data-id="${song.id}" data-name="${song.name}">下载</button>
                                </div>
                            </li>`;
                            $('#playlist-tracks').append(item);
                        });
                        $('#playlist-result').removeClass('d-none');
                    } else {
                        $('#playlist-result').removeClass('d-none');
                        $('#playlist-tracks').html('<li class="list-group-item">歌单解析失败:'+data.data.msg+'</li>');
                    }
                }, 'json');
            });

            // 专辑解析
            $(document).on('click', '#album-btn', function() {
                let aid = $('#album_id').val().trim();
                if (!aid) {
                    alert('请输入专辑ID或链接');
                    return;
                }
                // 支持直接粘贴专辑链接
                const idMatch = aid.match(/album\?id=(\d+)/);
                if (idMatch) aid = idMatch[1];
                $.get('/Album', { id: aid }, function(data) {
                    if (data.status === 200) {
                        const al = data.data.album;
                        $('#album-cover').attr('src', al.coverImgUrl);
                        $('#album-name').text(al.name);
                        $('#album-artist').text(al.artist);
                        $('#album-desc').text(al.description || '');
                        $('#album-count').text(al.songs.length);
                        $('#album-tracks').empty();
                        al.songs.forEach(function(song, idx) {
                            const item = `<li class="list-group-item d-flex justify-content-between align-items-center">
                                <div>
                                    <img src="${song.picUrl}" alt="cover" style="width:32px;height:32px;object-fit:cover;border-radius:4px;margin-right:8px;">
                                    <strong class="playlist-title">${idx+1}. ${song.name}</strong> - <span>${song.artists}</span> <span class="text-muted">[${song.album}]</span>
                                </div>
                                <div>
                                    <button class="btn btn-sm btn-outline-primary select-song me-2" data-id="${song.id}" data-name="${song.name}">解析</button>
                                    <button class="btn btn-sm btn-success download-song" data-id="${song.id}" data-name="${song.name}">下载</button>
                                </div>
                            </li>`;
                            $('#album-tracks').append(item);
                        });
                        $('#album-result').removeClass('d-none');
                    } else {
                        $('#album-result').removeClass('d-none');
                        $('#album-tracks').html('<li class="list-group-item">专辑解析失败:'+data.data.msg+'</li>');
                    }
                }, 'json');
            });

            // 音乐下载功能
            $('#download-btn').on('click', function() {
                const musicId = $('#download_id').val().trim();
                const quality = $('#download_quality').val();
                
                if (!musicId) {
                    alert('请输入音乐ID或URL');
                    return;
                }
                
                // 提取ID(支持URL格式)
                let processedId = musicId;
                const idMatch = musicId.match(/song\?id=(\d+)/);
                if (idMatch) {
                    processedId = idMatch[1];
                }
                
                // 显示进度条
                $('#download-progress').removeClass('d-none');
                $('#download-btn').prop('disabled', true).text('下载中...');
                
                // 使用XMLHttpRequest来处理下载并获取响应头
                const xhr = new XMLHttpRequest();
                xhr.open('POST', '/Download', true);
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xhr.responseType = 'blob';
                
                xhr.onload = function() {
                    if (xhr.status === 200) {
                        // 获取自定义响应头
                        const downloadMessage = xhr.getResponseHeader('X-Download-Message');
                        const encodedFilename = xhr.getResponseHeader('X-Download-Filename');
                        const filename = encodedFilename ? decodeURIComponent(encodedFilename) : 'music.flac';
                        
                        // 创建下载链接
                        const blob = xhr.response;
                        const url = window.URL.createObjectURL(blob);
                        const a = document.createElement('a');
                        a.href = url;
                        a.download = filename || 'music.flac';
                        document.body.appendChild(a);
                        a.click();
                        document.body.removeChild(a);
                        window.URL.revokeObjectURL(url);
                        
                        // 显示完成提示
                        alert('✅ 音乐文件下载完成!');
                        console.log(`下载完成: ${filename}`);
                        if (downloadMessage) {
                            console.log(`服务器消息: ${downloadMessage}`);
                        }
                    } else {
                        alert('下载失败,请重试');
                    }
                    
                    // 重置按钮状态
                    $('#download-progress').addClass('d-none');
                    $('#download-btn').prop('disabled', false).text('下载音乐');
                };
                
                xhr.onerror = function() {
                    alert('下载出错,请重试');
                    $('#download-progress').addClass('d-none');
                    $('#download-btn').prop('disabled', false).text('下载音乐');
                };
                
                // 发送请求
                const formData = `id=${encodeURIComponent(processedId)}&quality=${encodeURIComponent(quality)}`;
                xhr.send(formData);
            });

            // 列表中的下载按钮
            // 下载按钮点击跳转到下载功能
            $(document).on('click', '.download-song', function() {
                const musicId = $(this).data('id');
                const songName = $(this).data('name');
                
                if (!musicId) {
                    alert('无法获取音乐ID');
                    return;
                }
                
                // 填充下载ID并跳转到下载功能区域
                $('#download_id').val(musicId);
                $('#mode-select').val('download').trigger('change');
                $('html,body').animate({scrollTop: $('#main-form').offset().top}, 300);
            });

            // ========== MV功能 ==========
            
            // MV搜索
            $('#mv-search-btn').on('click', function() {
                const keywords = $('#mv_search_keywords').val();
                const limit = $('#mv_search_limit').val();
                
                if (!keywords) {
                    alert('请输入搜索关键词');
                    return;
                }
                
                $.ajax({
                    url: '/mv/search',
                    method: 'POST',
                    data: { keywords: keywords, limit: limit },
                    dataType: 'json',
                    success: function(data) {
                        if (data.status === 200 && data.data && data.data.length > 0) {
                            $('#mv-search-list').empty();
                            data.data.forEach(function(mv) {
                                const playCount = (mv.playCount / 10000).toFixed(1) + '万';
                                const duration = Math.floor(mv.duration / 1000 / 60) + ':' + (Math.floor(mv.duration / 1000) % 60).toString().padStart(2, '0');
                                const item = `<li class="list-group-item d-flex justify-content-between align-items-center">
                                    <div class="d-flex align-items-center">
                                        <img src="${mv.cover}" alt="MV封面" style="width:80px;height:45px;object-fit:cover;border-radius:4px;margin-right:10px;">
                                        <div>
                                            <strong class="d-block">${mv.name}</strong>
                                            <small class="text-muted">${mv.artistName} | 播放: ${playCount} | 时长: ${duration}</small>
                                        </div>
                                    </div>
                                    <button class="btn btn-sm btn-primary parse-mv" data-id="${mv.id}">解析</button>
                                </li>`;
                                $('#mv-search-list').append(item);
                            });
                            $('#mv-search-result').removeClass('d-none');
                        } else {
                            $('#mv-search-list').html('<li class="list-group-item">未找到相关MV</li>');
                            $('#mv-search-result').removeClass('d-none');
                        }
                    },
                    error: function(xhr) {
                        const error = xhr.responseJSON || {};
                        alert('MV搜索失败: ' + (error.message || '请重试'));
                    }
                });
            });
            
            // MV搜索结果点击解析
            $(document).on('click', '.parse-mv', function() {
                const mvId = $(this).data('id');
                $('#mv_id').val(mvId);
                $('#mode-select').val('mv-parse').trigger('change');
                $('html,body').animate({scrollTop: $('#main-form').offset().top}, 300);
            });
            
            // MV解析
            $('#mv-parse-btn').on('click', function() {
                let mvId = $('#mv_id').val().trim();
                const quality = $('#mv_quality').val();
                
                if (!mvId) {
                    alert('请输入MV ID或URL');
                    return;
                }
                
                // 提取MV ID
                const idMatch = mvId.match(/mv\?id=(\d+)/);
                if (idMatch) {
                    mvId = idMatch[1];
                }
                
                // 获取MV详情
                $.ajax({
                    url: '/mv/detail',
                    method: 'POST',
                    data: { id: mvId },
                    dataType: 'json',
                    success: function(detailData) {
                        if (detailData.status === 200) {
                            const mv = detailData.data;
                            
                            // 获取MV播放链接
                            $.ajax({
                                url: '/mv/url',
                                method: 'POST',
                                data: { id: mvId, quality: quality },
                                dataType: 'json',
                                success: function(urlData) {
                                    if (urlData.status === 200 && urlData.data && urlData.data.length > 0) {
                                        const mvUrl = urlData.data[0];
                                        
                                        // 显示MV信息
                                        $('#mv-cover').attr('src', mv.cover);
                                        $('#mv_name').text(mv.name);
                                        $('#mv_artist').text(mv.artistName);
                                        $('#mv_duration').text(Math.floor(mv.duration / 1000 / 60) + ':' + (Math.floor(mv.duration / 1000) % 60).toString().padStart(2, '0'));
                                        $('#mv_quality_display').text(quality + 'P');
                                        $('#mv_size').text((mvUrl.size / 1024 / 1024).toFixed(2) + ' MB');
                                        $('#mv_play_count').text((mv.playCount / 10000).toFixed(1) + '万');
                                        $('#mv_publish_time').text(mv.publishTime);
                                        $('#mv_url').attr('href', mvUrl.url);
                                        
                                        // 下载按钮
                                        $('#mv-download-link').off('click').on('click', function() {
                                            const link = document.createElement('a');
                                            link.href = mvUrl.url;
                                            link.download = `${mv.artistName} - ${mv.name} [${quality}P].mp4`;
                                            link.target = '_blank';
                                            document.body.appendChild(link);
                                            link.click();
                                            document.body.removeChild(link);
                                        });
                                        
                                        // 播放器
                                        const player = $('#mv-player');
                                        player.attr('src', mvUrl.url);
                                        player.removeClass('d-none');
                                        
                                        $('#mv-info').removeClass('d-none');
                                    } else {
                                        alert('无法获取MV播放链接');
                                    }
                                },
                                error: function() {
                                    alert('获取MV播放链接失败');
                                }
                            });
                        } else {
                            alert('获取MV详情失败: ' + (detailData.message || ''));
                        }
                    },
                    error: function() {
                        alert('获取MV详情失败,请重试');
                    }
                });
            });
        });
    </script>
</body>
</html>