-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
1705 lines (1651 loc) · 75 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*This plugin is licensed under the GNU/GPL-3.0
**Copyright (C) 2024-2025 Lukoning
*/
let rvN, v, amllbgv/*Apple Music-like Lyrics Background Video*/, b/*(toggle)button*/, cP/*configPage*/, c/*canvas*/, bgc/*Background Canvas*/, cpc/*ColorPick() Canvas*/, cover, OcvUrl, cvUrl, cvUrlCache, songDataCache, tMsT, lrcCache, pLrc, pLrcKeys, showRefreshing, thePiPWindow
, DontPlay=false, DontPause=false, autoRatio, autoRatioValue=480, lastReRatio=0, songIdCache=0, playProgress=0, nrLrc=false, lrcNowLoading=false, reRatioPending=false, isDynamicLyrics=false, isJp=false,debugMode=false
, isVLsnAdded=false, isLrcRnpLsnAdded=false
, t = "0:00 / 0:00", tC = 0, tT = 0, tP = 0, tR = 0 //显示用,Current,Total,PassedRate,Remaining
, readCfg = JSON.parse(localStorage.getItem("PiPWindowSettings"))
, color = ({accent: "", text: "", textT13: "", textT31: "", textT42: "", textT56: "", bg: "", bgT00: "", bgT50: ""}), colorS = ({accent: "", text: "", bg: "", bgT: ""}), colorCache = ({text: "", bg: ""})
, song = ({name:"", nameAnother:"", artist:""})
const pdd = "M21 3C21.5523 3 22 3.44772 22 4V11H20V5H4V19H10V21H3C2.44772 21 2 20.5523 2 20V4C2 3.44772 2.44772 3 3 3H21ZM21 13C21.5523 13 22 13.4477 22 14V20C22 20.5523 21.5523 21 21 21H13C12.4477 21 12 20.5523 12 20V14C12 13.4477 12.4477 13 13 13H21Z"
, pO = `<path d="${pdd}M20 15H14V19H20V15ZM6.70711 6.29289L8.95689 8.54289L11 6.5V12H5.5L7.54289 9.95689L5.29289 7.70711L6.70711 6.29289Z"></path>`
, pC = `<path d="${pdd}"></path>`
, cfgDefault = ({
whenClose: "none", whenBack: "back", whenCloseOrBack_paused: "close", autoHideMainWindow: false, showTaskbarButton: false
, showDiscWhenNoCover: false, allowNonsquareCover: false, showAlbum: true, timeInfo: "CurrentTotal"
, dynamicLyrics: true, autoScroll: true, originalLyricsBold: false, showTranslation: true, /*showLatinization: false,*/ lyricsTaperOff: true, lyricsMask: false, lyricsHanzi2Kanji: true, lyricsOffset: 0, lyricsFrom: "LibLyric", lyricsCustomSources: "https://example.com/lyric?track=${track}&id=${trackId}&art=${artist}&arts=${artists}&album=${album}&albumId=${albumId}", showLyricsErrorTip: true
, colorFrom: "albumCover", backgroundFrom: "albumCoverBlur", customFonts: "\"Segoe UI\", \"Microsoft Yahei UI\", system-ui", useJapaneseFonts: true, customJapaneseFonts: "\"Yu Gothic UI\", \"Meiryo UI\", \"Microsoft Yahei UI\", system-ui"
, smoothProgessBar: true, resolutionRatio: "auto", aspectRatio: "2:1", albumCoverSize: 160, useFullCover: false
, customLoadingTxt: "正在载入猫猫…"})
, DcvUrl/*Default*/ = "orpheus://orpheus/style/res/common/discovery/calendar_bg.png"
, discUrl = "orpheus://orpheus/style/res/default/default_play_disc.png"
readCfg = {...cfgDefault, ...readCfg} //缺失配置啥的处理一下
let oldCfg = {...readCfg}
window.PiPWShowRefreshing = (x=true)=>{if(x==true){showRefreshing=true;return true}else if(x==false){showRefreshing=false;return false}}
function cE(n, d=document) {return d.createElement(n)}
function q(n, d=document) {return d.querySelector(n)}
function qAll(n, d=document) {return d.querySelectorAll(n)}
const cn2jp = loadedPlugins.LibOpenCC?OpenCC.Converter({from: "cn", to: "jp"}):waht;
function waht() {return arguments[0]}
function DEBUG() {try{
q("#PiPWSettings").appendChild(bgc);
q("#PiPWSettings").appendChild(c);
q("#PiPWSettings").appendChild(v);
amllbgv?q("#PiPWSettings").appendChild(amllbgv):"";
}catch{}}
function taskbarButton(isShow=true) {betterncm.app.exec(`PowerShell "${loadedPlugins.PiPWindow.pluginPath}/taskbarButton.ps1" -Action ${isShow?"Show":"Hide"}`)}
async function tipMsg(m, t) {
let c1="u-result", c2="j-tips", c=`.${c1}.${c2}`, iiH=`<span class="u-tit f-ff2">${m}</span>`
if (t=="err") {
iiH=`<i class="icon u-icn u-icn-operatefail"></i><span class="u-tit f-ff2 errTxt">${m}</span>`
}
let iH=`<div class="wrap"><div class="inner j-flag">${iiH}</div></div>`
if (q(c+":not(.z-hide)")) {
if (q(c+" .inner")) {q(c+" .inner").innerHTML=iiH} else {q(c).innerHTML=iH}
try{clearTimeout(tMsT)}catch{}
} else {
let d = cE("div");
d.classList.add(c1, c2);
d.innerHTML = iH;
q("body").appendChild(d);
}
tMsT = setTimeout(()=>{
q(c).classList.add("z-hide");
q(c+".z-hide").addEventListener("animationend", ()=>{q(c).remove()})
}, 1200)
}
function reRatio(rv) {
function r() {autoRatioValue=rvN; lastReRatio=Date.now()}
if (autoRatio) { let l = lastReRatio+1000, n = Date.now(); rvN=rv
if (l<=n) {lastReRatio=Date.now();reRatio(rv)}
else if (!reRatioPending){reRatioPending=true;setTimeout(()=>{r();reRatioPending=false}, l-n)}
}
}
HTMLCanvasElement.prototype.toPiP = function(){
if (!(v instanceof HTMLVideoElement)) {
v = cE("video");
v.addEventListener("loadedmetadata", async()=>{
try {
//请求小窗
v.requestPictureInPicture().then(p=>{
thePiPWindow=p;reRatio(p.height);p.addEventListener("resize", e=>{reRatio(e.target.height)});//自适应分辨率
taskbarButton(readCfg.showTaskbarButton);//任务栏按钮
})
let pS = ".m-player:not(.f-dn)"
//控制按钮设置
navigator.mediaSession.setActionHandler("play", ()=>{v.play();navigator.mediaSession.playbackState = "playing";});
navigator.mediaSession.setActionHandler("pause", ()=>{v.pause();navigator.mediaSession.playbackState = "paused";});
navigator.mediaSession.setActionHandler("previoustrack", ()=>{q(`${pS} .btnc-prv`).click()});
navigator.mediaSession.setActionHandler("nexttrack", ()=>{q(`${pS} .btnc-nxt`).click()});
if (isVLsnAdded) {return} //防止重复加监听器
//小窗暂停/播放同步到主窗口
function ncmPlay() {if (!DontPlay) {try{ q(`${pS} .btnp-play`).click() }catch{}}DontPause=false}
function ncmPause() {if (!DontPause) {try{ q(`${pS} .btnp-pause`).click() }catch{}}DontPlay=false}
v.addEventListener("play", ()=>{ncmPlay()})
v.addEventListener("pause", ()=>{ncmPause()})
//小窗打开/关闭逻辑
v.addEventListener("enterpictureinpicture", e=>{ console.log("PiPW Log: PiP窗口已创建", v)
let s = betterncm.ncm.getPlayingSong()
if (!s) {s=({state:1})}
if (readCfg.autoHideMainWindow) {mwf.cef.R$exec("winhelper.showWindow", "minimize");mwf.cef.R$call("winhelper.showWindow", "hide")}
tipMsg("已打开小窗"); q("svg", b).innerHTML=pC; b.setAttribute("style", "fill: currentColor; opacity: 1"); b.title = "关闭小窗";
if (s.state==1) {v.pause()} DontPlay=false
if(debugMode){console.log(e)}
});
v.addEventListener("leavepictureinpicture", e=>{ DontPause=true; let p = v.paused
tipMsg("已关闭小窗"); q("svg", b).innerHTML=pO; b.setAttribute("style", ""); b.title = "打开小窗";
setTimeout(()=>{
if (v.paused!=p) { //状态不一致,判定为按下关闭按钮
let c = readCfg.whenClose;
if (c == "pause") {DontPause=false;ncmPause()}
else if (c == "shutdown") {
DontPause=false; ncmPause(); if(debugMode){tipMsg("调试模式:触发退出云音乐");return}
mwf.cef.R$call("winhelper.showWindow", "hide"); mwf.cef.R$exit()
}
} else if (!p&&readCfg.whenBack == "back") {mwf.cef.R$call("winhelper.showWindow", "show")}
else if (readCfg.whenCloseOrBack_paused == "back") {mwf.cef.R$call("winhelper.showWindow", "show")}
}, 10)
if(debugMode){console.log(e)}
});
//主窗口暂停/播放同步到小窗
legacyNativeCmder.appendRegisterCall("PlayState", "audioplayer", (_, __, state) => { //监听播放状态变动
if (state === 2) {
DontPause=true; v.pause(); try{amllbgv.pause()}catch{}
} else if (state === 1) {
DontPlay=true; v.play(); try{amllbgv.play()}catch{}
}
});
isVLsnAdded = true
} catch (e) {
console.error("PiPW Error: PiP窗口创建出错,详情:\n", e);tipMsg("PiP窗口创建出错,详见JavaScript控制台", "err")
}
})
}
v.id = "PiPW-VideoE";
let cs = this.captureStream();
v.srcObject = cs; //刷新源
v.controls = true; //调试用
if(debugMode){DEBUG()}
v.play() //否则黑窗
}
function pipToggle() {
let pE = document.pictureInPictureElement
if (pE && pE.id=="PiPW-VideoE") {
document.exitPictureInPicture();
} else {
loadPiP(true, "PiPW-Toggle");
}
}
function colorPick(from=null) { //取色
let textTO, bgTO;
function s(e, p) {
return getComputedStyle(q(e)).getPropertyValue(p);
}
colorS.accent = s("body", "--themeC1"), colorS.text = s("body", "color");
if (q("body.material-you-theme")) {colorS.text = s("body", "--md-accent-color-secondary")}
if (/rgba/.test(colorS.text)) {textTO = colorS.text.replace(/,([^,)]*)\)/, "")}
else {textTO = colorS.text.replace(/rgb\(/, "rgba(").replace(/\)/, "")}
colorS.text = `${textTO})`
colorS.bg = s("body", "background-color");
if (/rgba/.test(colorS.bg)) {bgTO = colorS.bg.replace(/,([^,)]*)\)/, "")}
else {bgTO = colorS.bg.replace(/rgb\(/, "rgba(").replace(/\)/, "")}
colorS.bg = `${bgTO})`, colorS.bgT = `${bgTO}, .3)`;
if (q("body.material-you-theme:not(.ncm-light-theme)")) {colorS.bgT = `${colorS.accent.replace(/\)/, "")}, .1)`;}
if (from==null) {
color.accent = colorS.accent;
color.text = `${textTO})`, color.textT56 = `${textTO}, .56)`, color.textT42 = `${textTO}, .42)`, color.textT31 = `${textTO}, .31)`, color.textT13 = `${textTO}, .13)`;
color.bg = `${bgTO})`, color.bgT00 = `${bgTO}, 0)`, color.bgT50 = `${bgTO}, .5)`;
} else {
function brightness(rgb, factor) {
let rN = Math.min(255, Math.max(0, rgb[0] * factor))
, gN = Math.min(255, Math.max(0, rgb[1] * factor))
, bN = Math.min(255, Math.max(0, rgb[2] * factor));
return [Math.round(rN), Math.round(gN), Math.round(bN), 255];
}
function saturation(rgb, factor) {
let l = (.299*rgb[0]+.587*rgb[1]+.114*rgb[2])
, rN = Math.min(255, Math.max(0, l + (rgb[0] - l) * factor))
, gN = Math.min(255, Math.max(0, l + (rgb[1] - l) * factor))
, bN = Math.min(255, Math.max(0, l + (rgb[2] - l) * factor));
return [Math.round(rN), Math.round(gN), Math.round(bN), 255];
}
if (!cpc) {cpc = cE("canvas"); cpc.width = 3; cpc.height = 3;}
let cpcC = cpc.getContext("2d",{alpha:false})
from.height?cpcC.drawImage(from, 0, 0, cpc.width, cpc.height):""
let rgb = cpcC.getImageData(1, 1, 2, 2).data
, l = (.299*rgb[0]+.587*rgb[1]+.114*rgb[2])
, bf = 1, sf = 1.2
l<8?bf=50 : l<16?bf=16 : l<32?bf=12 : l<64?bf=8 : l<128?bf=2 : l>144?bf=(-.4) : ""
l>144?sf=6 : ""
let rgbN = brightness(rgb, l>160?1.1:l>144?1.4:.5)
bgTO = `rgba(${rgbN[0]}, ${rgbN[1]}, ${rgbN[2]}`
color.bg = `${bgTO})`, color.bgT00 = `${bgTO}, 0)`, color.bgT50 = `${bgTO}, .5)`;
rgbN = brightness(saturation(rgb, sf), .7+bf)
if ((.299*rgbN[0]+.587*rgbN[1]+.114*rgbN[2])>245) {rgbN = brightness(saturation(rgb, 4), .7+bf)}
color.accent = `rgb(${rgbN[0]}, ${rgbN[1]}, ${rgbN[2]})`
textTO = `rgba(${rgbN[0]}, ${rgbN[1]}, ${rgbN[2]}`
color.text = `${textTO})`, color.textT56 = `${textTO}, .56)`, color.textT42 = `${textTO}, .42)`, color.textT31 = `${textTO}, .31)`, color.textT13 = `${textTO}, .13)`;
}
try {let s0 = q("#PiPWSettingsStyle0", cP), s = `
#PiPWSettings {
--pipws-fg: ${colorS.accent};
--pipws-bg: ${colorS.bgT};
--pipws-bg-wot: ${colorS.bg};
color: ${colorS.text};
}`
if (s0.innerHTML!=s) {s0.innerHTML=s}
} catch {}
}
async function loadPiP(isToPiP=true, from="unknow") {
let PiPE = document.pictureInPictureElement;
if (!isToPiP && !PiPE && !debugMode) {return}
if (PiPE && PiPE.id != "PiPW-VideoE") {tipMsg("PiP窗口被占用", "err"); if(!debugMode){return}}
let startTime = Date.now();
try {
let nrInfo=false/*need re-*/, nrHead=false, chigai=false /*曲目不同以往(?)*/
, ldTxt = readCfg.customLoadingTxt;
/*分辨率*/
let r = readCfg.resolutionRatio
if (r=="auto") {autoRatio=true; r=autoRatioValue} else {autoRatio=false; r=r*1}
let pS = betterncm.ncm.getPlayingSong(), data;
if (pS) {data = pS.data}
let cvSizeX = r/3, cvSizeY = r/3
/*封面*/
if (!cover) {
cover = new Image();
}
let s = readCfg.albumCoverSize
, thbn = `thumbnail=${s}y${s}`;
readCfg.useFullCover?thbn="":""
if (readCfg.allowNonsquareCover) {cvSizeX = cover.width*(cvSizeY/cover.height);thbn=""}
try {
let u = data.album.picUrl
if (!u) {ya.ma.no.su.su.me;throw new Error()}
else {cvUrl = `orpheus://cache/?${u}?imageView&enlarge=1&type=webp${thbn==""?"":`&${thbn}`}`}
} catch {
try {
let c = q("img.j-cover")
OcvUrl = c.src; cvUrl = OcvUrl.replace(/thumbnail=([^&]+)/, `type=webp${thbn==""?"":`&${thbn}`}`)
if (!cvUrl) {cvUrl = null}
} catch {cvUrl = null}
}
if (cvUrl != cvUrlCache||data.id != songIdCache) {nrHead=true}
if (data.id != songIdCache) {getInfo(); chigai=true; songIdCache = data.id; nrLrc=true}
if (from=="Settings" && (oldCfg.lyricsFrom!=readCfg.lyricsFrom||oldCfg.lyricsCustomSources!=readCfg.lyricsCustomSources)) {nrLrc=true}
function getInfo() {
/*歌名*/
try {
song.name = data.name;
if (readCfg.showAlbum) {
let n = data.album.name, t = data.album.transNames;
n = n?n:null, t = t?t[0]:null;
if (n||t) {song.nameAnother = `${n||""}${t?" ("+t+")":""}`}
else {song.nameAnother = "未知专辑"}
} else {
let t = data.transNames, a = data.alias;
t = t?t[0]:null, a = a?a[0]:null;
if (t||a) {song.nameAnother = `${t||""}${t&&a?" ":""}${a||""}`}
else {song.nameAnother = ""}
}
} catch {
let t = q(".m-pinfo .j-title")
song.name = t?t.title:"没有曲目";
try {
song.nameAnother = q(".m-pinfo .j-title .s-fc4").textContent.slice(1).slice(0, -1);
}catch{}
}
/*歌手*/
let sa="",saE
try {
saE = data.artists;
for (i=0; i<saE.length; i++, sa=sa+" / ") {
sa = sa + saE[i].name;
}
sa = sa.slice(0, -3); /*处理多余斜杠*/
song.artist = sa==""?"未知艺术家":sa
} catch {
saE = qAll(".m-pinfo .bar > .j-title span:first-child *")
if (saE&&saE.length!=0) {
for (i=0; i<saE.length; i++, sa=sa+" / ") {
sa = sa + saE[i].textContent;
}
sa = sa.slice(0, -3); /*处理多余斜杠*/
song.artist = sa=="未知"?"未知艺术家":sa
}
}
}
/*时间*/
try {
tT = data.duration/1000, tP = tC/tT, tR = tT-tC
let tCM = Math.floor(tC/60), tCS = Math.floor(tC%60), tCD = `${tCM}:${tCS<10?"0":""}${tCS}`
, tTM = Math.floor(tT/60), tTS = Math.floor(tT%60), tTD = `${tTM}:${tTS<10?"0":""}${tTS}`
, tRM = tTM-tCM, tRS = tTS-tCS
, tI = readCfg.timeInfo
if (tRS<0) {tRM = tRM-1, tRS = tRS+60} tRD = `-${tRM}:${tRS<10?"0":""}${tRS}`
if (tI == "CurrentTotal") {t = `${tCD} / ${tTD}`}
else if (tI == "CurrentRemaining") {t = `${tCD} / ${tRD}`}
}catch{}
/*歌词*/
let lyrics = ({
M: {
0: "暂无歌词",
1: "",
2: "",
3: "",
4: "",
},
T: {
0: "",
1: "",
2: "",
3: "",
4: "",
},
currentT: 0,
currentD: 0,
}), offset = readCfg.lyricsOffset*1000;
if (readCfg.lyricsFrom != "RNP") {
document.removeEventListener("lyrics-updated", rnpLrcUpdate);
isLrcRnpLsnAdded = false
}
switch (readCfg.lyricsFrom) {
case "RNP":getLrcRnp();break
case "OriginalLyricBar":getLrcOrg();break
case "LibLyric":getLrcLibLyric();break
case "Custom":getLrcCustom();break
default: showLrcErr("该词源设置项不存在")
}
function showLrcErr(e="没有详细错误信息") {
isJp = false
if (readCfg.showLyricsErrorTip) {
lyrics.M[0] = "暂无歌词", lyrics.T[0] = ""
lyrics.M[1] = `当前歌词源错误: ${readCfg.lyricsFrom}`, lyrics.T[1] = e
}
pLrc = {
0: {time:0, duration:Infinity, originalLyric: lyrics.M[0], translatedLyric: lyrics.T[0]},
1: {time:Infinity, duration:0, originalLyric: lyrics.M[1], translatedLyric: lyrics.T[1]}
};
pLrcKeys = Object.keys(pLrc)
}
function getLrcErr(e) {
lrcNowLoading = false;
console.error("PiPW Error: 获取歌词时出错,详情:", e);
Object.prototype.toString.call(e)==="[object Object]"?e.message===void 0?e=JSON.stringify(e):e=e.message:""
showLrcErr(e);
}
function rnpLrcUpdate(e) {
pLrc = JSON.parse(JSON.stringify(e.detail.lyrics))
handleLyrics()
console.log("PiPW Log: GotLyrics", pLrc)
};
function getLrcRnp() {
if (!loadedPlugins.RefinedNowPlaying) {showLrcErr("依赖的插件未安装")}
try {
if (!isLrcRnpLsnAdded) {
document.addEventListener("lyrics-updated", rnpLrcUpdate);
isLrcRnpLsnAdded = true
rnpLrcUpdate({detail:window.currentLyrics})
}
lrcUpdate();
} catch(e) {console.error(`PiPW Error: 获取歌词时出错,详情:\n${e}`);showLrcErr(e)}
}
function getLrcOrg() {
try {
lyrics.M[0] = q(".m-lyric .s-fc0").textContent, lyrics.T[0] = q(".m-lyric .s-fc3").textContent
}catch{
try {
lyrics.M[0] = q(".m-lyric p").textContent
}catch{}
}
}
async function getLrcLibLyric() {
if (lrcNowLoading) {lyrics.M[0] = ldTxt;return}
if (!loadedPlugins.liblyric) {showLrcErr("依赖的插件未安装")}
try {
let ll = loadedPlugins.liblyric
if (nrLrc) {
lrcNowLoading = true
nrLrc = false
lrcCache = await ll.getLyricData(data.id)
console.log("PiPW Log: Lyrics", lrcCache);
pLrc = ll.parseLyric(
lrcCache.lrc.lyric
, lrcCache.tlyric? lrcCache.ytlrc ? lrcCache.ytlrc.lyric : lrcCache.tlyric.lyric :""
, /*lrcCache.romalrc? lrcCache.yromalrc ? lrcCache.yromalrc.lyric : lrcCache.romalrc.lyric :*/""
, lrcCache.yrc? lrcCache.yrc.lyric? lrcCache.yrc.lyric :"":"" //为什么lrcCache.yrc.lyric可以是null...
)
handleLyrics()
console.log("PiPW Log: ParsedLyrics", pLrc);
lrcNowLoading = false
}
lrcUpdate();
} catch(e) {getLrcErr(e)}
}
async function getLrcCustom() {
if (lrcNowLoading) {lyrics.M[0] = ldTxt;return}
if (!loadedPlugins.liblyric) {showLrcErr("依赖的插件未安装")}
try {
let ll = loadedPlugins.liblyric
if (nrLrc) {
lrcNowLoading = true
nrLrc = false
let songDetails = {
track: data.name,
trackId: data.id,
artist: data.artists[0].name,
artists: song.artist,
album: data.album.name,
albumId: data.album.id,
};
let url = readCfg.lyricsCustomSources.replace(/\$\{(\w+)\}/g, (_, p1) => {
return songDetails[p1];
});
fetch(url).then(response => {
if (!response.ok) {
throw new Error(`${response.status} ${response.statusText}`);
}
return response.text()
})
.then(lrc=>{
pLrc = ll.parseLyric("", "", "", lrc)
handleLyrics()
console.log("PiPW Log: ParsedLyrics", pLrc);
lrcNowLoading = false
})
.catch(e=>{getLrcErr(e)});
}
lrcUpdate();
} catch(e) {getLrcErr(e)}
}
function handleLyrics() {
pLrcKeys = Object.keys(pLrc)
for (let i = 0; i < pLrcKeys.length; i++) {
isJp = /[ぁ-ヿ]/g.test(pLrc[i].originalLyric)
if (isJp==true) {break}
}
for (let i = 0; i < pLrcKeys.length; i++) {
let o = pLrc[i].originalLyric, t = pLrc[i].translatedLyric, d = JSON.stringify(pLrc[i].dynamicLyric)
if (o==t) {pLrc[i].translatedLyric = ""} //优化歌词查看体验
o = pLrc[i].originalLyric.replace(/\s+/g, " ").trim()
if (o=="") {if (i+1==pLrcKeys.length) {delete pLrc[i];pLrcKeys = Object.keys(pLrc);continue} else {pLrc[i].originalLyric = "· · ·", pLrc[i].translatedLyric = ""}}
else if (isJp && readCfg.lyricsHanzi2Kanji) {
pLrc[i].originalLyric = cn2jp(o)
try{d = cn2jp(d)}catch{}
} else {pLrc[i].originalLyric = o}
try{
d = d.replace(/\s+/g, " ").trim()
pLrc[i].dynamicLyric = JSON.parse(d)
}catch{}
}
}
function lrcUpdate() {
let l = pLrcKeys.length, p = playProgress+offset
for (let i = 0; i < l; i++) {
let d = pLrc[i].duration
if (p < pLrc[i].time+d || i==l-1) {
if (pLrc[i].dynamicLyric&&readCfg.dynamicLyrics) {
lyrics.M[0] = pLrc[i].dynamicLyric
lyrics.currentT = pLrc[i].dynamicLyricTime
} else {
lyrics.M[0] = pLrc[i].originalLyric
lyrics.currentT = pLrc[i].time
}
lyrics.currentD = d==0?data.duration-pLrc[i].time:d
for (let j = 1; j < 5; j++) {lyrics.M[j] = i+j<l?pLrc[i+j].originalLyric:""}
if (!readCfg.showTranslation) {for (let i=0; i<5; i++) {lyrics[`T${i}`] = ""} break}
for (let j = 0; j < 5; j++) {lyrics.T[j] = i+j<l?pLrc[i+j].translatedLyric?pLrc[i+j].translatedLyric:"":""}
break
}
}
}
/*取色环节*/
chigai? readCfg.colorFrom=="albumCover"? colorPick(cover?cover:null) : colorPick() :""
if (color.text!=colorCache.text||color.bg!=colorCache.bg) {nrHead=true; colorCache.text=color.text, colorCache.bg=color.bg}
/*创建canvas*/
loadC()
function loadC() {
let [w, h] = readCfg.aspectRatio.split(":").map(Number), rw = Math.round(r*(w/h)) //因为width不能设为小数
if (!c) {
c = cE("canvas");
console.log("PiPW Log: canvas元素已创建", c);
}
if (!bgc) {
bgc = cE("canvas");
console.log("PiPW Log: 背景canvas元素已创建", bgc);
}
if (c.width != rw || c.height != r) {
c.width = rw; c.height = r; nrHead=true;
bgc.width = rw; bgc.height = r;
}
if (isToPiP && !PiPE) {
DontPlay=true; //解决打开小窗时自动播放的问题
c.toPiP();
nrHead=true
}
}
/*字体*/
let bold="", f = readCfg.customFonts, fM = f, fT = f; //这里f后期也许单独做一个界面字体
readCfg.originalLyricsBold?bold="bold":""
/*日文字体更换*/
readCfg.useJapaneseFonts&&isJp ? fM=readCfg.customJapaneseFonts :""
if (from=="Settings") {getInfo();nrHead=true} //如果是改字体/显示的信息或者颜色有变……
nrHead?reloadHead():""
function reloadHead() {
if (!cvUrl) {
cover.src = DcvUrl;
readCfg.showDiscWhenNoCover?"":cvSizeX = r/96
}
else {cover.src = cvUrl}
cvUrlCache = cvUrl;
nrInfo = true;
}
let cC = c.getContext("2d",{alpha:false}), bgcC = bgc.getContext("2d",{alpha:false}) //alpha:false可有效解决内存溢出问题
, o1 = r/480, o2 = r/240, o3 = r/160, o5 = r/96, o6 = r/80, o9 = r/53.3333, o10 = r/48, o12 = r/40, o15 = r/32, o20 = r/24, o21p5 = r/22.3256, o25 = r/19.2, o30 = r/16, o30p5 = r/15.7377, o35 = r/13.7143, o40 = r/12, o45 = r/10.6667, o55 = r/8.7272, o60 = r/8, o105 = r/4.57143, o150 = r/3.2, o480 = r
, txtMgL = cvSizeX + o10
, x = 0, y = 0;
cC.textAlign = "left";
y = cvSizeY+o3;
cC.clearRect(0, y, c.width, c.height);
let lrcFS = o55, lrcMgT = o45, lrcMgL = o15, mLrcMgL = lrcMgL
, lrcTop = cvSizeY+lrcMgT
, lrcSSS = readCfg.lyricsTaperOff;
let lrcLine = {0: lrcTop+lrcFS, 1: lrcTop+lrcFS*2+o10, 2: lrcTop+lrcFS*3+o12, 3: lrcTop+lrcFS*4+o10, 4: lrcTop+lrcFS*5+o2}
function lrcMNow() {cC.fillStyle = color.text, cC.font = `${bold} ${lrcFS}px ${fM}`, lrcMgL = o15}
function lrcMNowUnplayed() {cC.fillStyle = color.textT42, cC.font = `${bold} ${lrcFS}px ${fM}`, lrcMgL = o15}
function lrcMNext1() {cC.fillStyle = color.textT56, cC.font = `${bold} ${lrcFS-o10}px ${fM}`, lrcSSS?lrcMgL = o12:""}
function lrcMNext2() {cC.fillStyle = color.textT56, cC.font = `${bold} ${lrcSSS?lrcFS-o15:lrcFS-o10}px ${fM}`, lrcSSS?lrcMgL = o9:""}
function lrcMNext3() {cC.fillStyle = color.textT56, cC.font = `${bold} ${lrcSSS?lrcFS-o20:lrcFS-o10}px ${fM}`, lrcSSS?lrcMgL = o6:""}
function lrcMNext4() {cC.fillStyle = color.textT56, cC.font = `${bold} ${lrcSSS?lrcFS-o25:lrcFS-o10}px ${fM}`, lrcSSS?lrcMgL = o3:""}
function lrcTNow() {cC.fillStyle = color.textT56, cC.font = `${lrcFS-o5}px ${fT}`, lrcMgL = o15}
function lrcTNext1() {cC.fillStyle = color.textT31, cC.font = `${lrcFS-o15}px ${fT}`, lrcSSS?lrcMgL = o12:""}
function lrcTNext2() {cC.fillStyle = color.textT31, cC.font = `${lrcSSS?lrcFS-o20:lrcFS-o15}px ${fT}`, lrcSSS?lrcMgL = o9:""}
function lrcTNext3() {cC.fillStyle = color.textT31, cC.font = `${lrcSSS?lrcFS-o25:lrcFS-o15}px ${fT}`, lrcSSS?lrcMgL = o6:""}
function updateMLrcMgL(w, now) {
if (!readCfg.autoScroll) {return}
if (!now) {
now = (playProgress+offset-lyrics.currentT)/lyrics.currentD
now = lyrics.currentT>playProgress+offset ? 0 : now>1?1:now
}
let l = w*now
if (w>c.width-lrcMgL&&l+o105>c.width-lrcMgL) {
mLrcMgL = 0-(l+lrcMgL-c.width)+lrcMgL-o105
}
}
isDynamicLyrics = false
if (Array.isArray(lyrics.M[0])) {
isDynamicLyrics = true
let lyricDO = ""/*lyricDynamicOrigin*/, l = lyrics.M[0].length, now = 0, nowWidth = 0
lyricDO = lyrics.M[0].map(item => item.word).join("");
lrcMNowUnplayed();
for (let i=0; i<l; i++) {
let lrc = lyrics.M[0][i], Cnow = (playProgress+offset-lrc.time)/lrc.duration
Cnow = lrc.time>playProgress+offset ? 0 : Cnow>1?1:Cnow
nowWidth += cC.measureText(lrc.word).width*Cnow
}
let w = cC.measureText(lyricDO).width
now = nowWidth/w
updateMLrcMgL(w, now);
cC.fillText(lyricDO, mLrcMgL, lrcLine[0]); /*主歌词(未播放)*/
lrcMNow(); cC.save();
cC.beginPath();
cC.rect(0, 0, w*now+mLrcMgL, c.height); cC.clip();
cC.fillText(lyricDO, mLrcMgL, lrcLine[0]); /*主歌词(已播放)*/
cC.restore();
} else if (readCfg.lyricsFrom!="OriginalLyricBar") {
lrcMNow();
updateMLrcMgL(cC.measureText(lyrics.M[0]).width);
cC.fillText(lyrics.M[0], mLrcMgL, lrcLine[0]); /*主歌词*/
} else {
lrcMNow(); cC.fillText(lyrics.M[0], lrcMgL, lrcLine[0]); /*主歌词*/
}
if (lyrics.T[0]!="") {
lrcTNow();
if (readCfg.lyricsFrom!="OriginalLyricBar") {
mLrcMgL = lrcMgL
updateMLrcMgL(cC.measureText(lyrics.T[0]).width);
}
cC.fillText(lyrics.T[0], mLrcMgL, lrcLine[1]-o10); /*翻译歌词*/
lrcMNext1(); cC.fillText(lyrics.M[1], lrcMgL, lrcLine[2]); /*下1句主歌词*/
if (lyrics.T[1]!="") {
lrcTNext1(); cC.fillText(lyrics.T[1], lrcMgL, lrcLine[3]-o10); /*下1句翻译歌词*/
lrcMNext2(); cC.fillText(lyrics.M[2], lrcMgL, lrcLine[4]); /*下2句主歌词*/
} else {
lrcMNext2(); cC.fillText(lyrics.M[2], lrcMgL, lrcLine[3]); /*下2句主歌词*/
if (lyrics.T[2]!="") {
lrcTNext2(); cC.fillText(lyrics.T[2], lrcMgL, lrcLine[4]-o10); /*下2句翻译歌词*/
} else {
lrcMNext3(); cC.fillText(lyrics.M[3], lrcMgL, lrcLine[4]); /*下3句主歌词*/
}
}
} else {
lrcMNext1(); cC.fillText(lyrics.M[1], lrcMgL, lrcLine[1]); /*下1句主歌词*/
if (lyrics.T[1]!="") {
lrcTNext1(); cC.fillText(lyrics.T[1], lrcMgL, lrcLine[2]-o10); /*下1句翻译歌词*/
lrcMNext2(); cC.fillText(lyrics.M[2], lrcMgL, lrcLine[3]); /*下2句主歌词*/
if (lyrics.T[2]!="") {
lrcTNext2(); cC.fillText(lyrics.T[2], lrcMgL, lrcLine[4]-o10); /*下2句翻译歌词*/
} else {
lrcMNext3(); cC.fillText(lyrics.M[3], lrcMgL, lrcLine[4]); /*下3句主歌词*/
}
} else {
lrcMNext2(); cC.fillText(lyrics.M[2], lrcMgL, lrcLine[2]); /*下2句主歌词*/
if (lyrics.T[2]!="") {
lrcTNext2(); cC.fillText(lyrics.T[2], lrcMgL, lrcLine[3]-o10); /*下2句翻译歌词*/
lrcMNext3(); cC.fillText(lyrics.M[3], lrcMgL, lrcLine[4]); /*下3句主歌词*/
} else {
lrcMNext3(); cC.fillText(lyrics.M[3], lrcMgL, lrcLine[3]); /*下3句主歌词*/
if (lyrics.T[3]!="") {
lrcTNext3(); cC.fillText(lyrics.T[3], lrcMgL, lrcLine[4]-o10); /*下3句翻译歌词*/
} else {
lrcMNext4(); cC.fillText(lyrics.M[4], lrcMgL, lrcLine[4]); /*下4句主歌词*/
}
}
}
}
if (readCfg.lyricsMask) {
let lrcMask = cC.createLinearGradient(0, lrcTop, 0, c.height*1.3);
lrcMask.addColorStop(.1, color.bgT00); //不能用#0000或者#FFF0等,会影响渐变的渲染效果
lrcMask.addColorStop(1, color.bg);
cC.fillStyle = lrcMask;
cC.fillRect(0, lrcTop, c.width, c.height); /*歌词阴影遮罩*/
}
function drawRC() {
cC.globalCompositeOperation = "destination-out";
cC.beginPath(); cC.strokeStyle = "#000"; cC.lineWidth = o5; x = cvSizeX+o2; y = cvSizeY+o2;
/*封面圆角*/
cC.moveTo(x, 0); cC.arcTo(x, y, 0, y, o12); cC.lineTo(0, y); cC.lineTo(x, y); cC.lineTo(x, 0);
cC.stroke();
cC.globalCompositeOperation = "source-over";
}
function drawInfo() {
cC.clearRect(cvSizeX, 0, c.width, cvSizeY+o5); /*清除*/
cC.fillStyle = color.text; cC.font = `${o55}px ${f}`;
cC.fillText(song.name, txtMgL, o60); /*主名*/
cC.fillStyle = color.textT31; cC.font = `${o35}px ${f}`;
cC.fillText(song.nameAnother, txtMgL, o105); /*副名*/
cC.fillStyle = color.textT56;
cC.fillText(song.artist, txtMgL, song.nameAnother==""?o105:o150); /*歌手*/
}
if (nrInfo) {
cC.fillStyle = color.text; cC.font = `${o25}px ${f}`; cC.fillText(ldTxt, o5, o30); /*封面(加载)*/
cover.onload = ()=>{/*封面(完毕)*/
if (readCfg.allowNonsquareCover) {cvSizeX = cover.width*(cvSizeY/cover.height); txtMgL=cvSizeX+o10}
cC.clearRect(0, 0, cvSizeX, cvSizeY+o5);drawInfo();
if (!cvUrl&&readCfg.showDiscWhenNoCover) {
let disc = new Image();
disc.src = discUrl;
disc.onload = () => {
cC.drawImage(disc, 0, 0, cvSizeX, cvSizeY);drawRC();
if(showRefreshing){console.log(`PiPW Log: 唱片绘制完成`)}
disc = null //处理
};
} else if (cvUrl) {
cC.drawImage(cover, 0, 0, cvSizeX, cvSizeY);drawRC();
if(showRefreshing){console.log(`PiPW Log: 歌曲封面绘制完成`)}
}
readCfg.colorFrom=="albumCover"? colorPick(cover?cover:null) : colorPick()
/*背景图*/
if (readCfg.backgroundFrom == "albumCoverBlur") {
bgcC.fillStyle = color.bg;
bgcC.fillRect(0, 0, bgc.width, bgc.height);
bgcC.filter = `blur(${o60}px)`;
bgcC.drawImage(cover, 0, 0, bgc.width, bgc.height);
bgcC.filter = "none";
bgcC.fillStyle = color.bgT50;
bgcC.fillRect(0, 0, bgc.width, bgc.height);
}
}
cover.onerror = ()=>{/*封面(失败)*/
cover.src = OcvUrl
}
}
cC.font = `${o30}px ${f}`;
cC.fillStyle = color.textT56;
let tW = cC.measureText(t).width
cC.fillText(t, o15, cvSizeY+o35); /*时间*/
let pbMgT = cvSizeY+o21p5, pbMgL = tW+o30p5
cC.fillStyle = color.textT13;
cC.fillRect(pbMgL, pbMgT, c.width-pbMgL, o5); /*进度条背景*/
cC.fillStyle = color.accent;
cC.fillRect(pbMgL, pbMgT, (c.width-pbMgL)*tP, o5); /*进度条*/
/*背景*/
if (readCfg.backgroundFrom == "AMLL"&&loadedPlugins["Apple-Musiclike-lyrics"]) {
let amllbgc = q(".amll-background-render-wrapper canvas")
if (amllbgc) {
if (!amllbgv) {
amllbgv = cE("video")
amllbgv.srcObject = amllbgc.captureStream()
amllbgv.controls = true; //调试用
amllbgv.play()
}
bgcC.drawImage(amllbgv, 0, 0, bgc.width, bgc.height);
} else {
let amllbgE = q("#amll-view > :first-child:not(.lyric-player-horizonal)"), amllbg
amllbgE?"":amllbgE=q("#amll-view")
amllbgE?
amllbg = getComputedStyle(amllbgE).getPropertyValue("background-color")
:amllbg = color.bg;
bgcC.fillStyle = amllbg;
bgcC.fillRect(0, 0, bgc.width, bgc.height);
}
drawInfo();drawRC();
} else {try{amllbgv.pause()}catch{}}
if (readCfg.backgroundFrom == "themeBackgroundColor") {
bgcC.filter = "none";
bgcC.fillStyle = color.bg;
bgcC.fillRect(0, 0, bgc.width, bgc.height);
}
cC.globalCompositeOperation = "destination-over";
y = cvSizeY+o3;
cC.drawImage(bgc, 0, 0, c.width, c.height);
cC.globalCompositeOperation = "source-over";
let timeUsing = Date.now() - startTime;
if(showRefreshing){console.log(`PiPW Log: <canvas>重绘完成,重绘用时${timeUsing==0?"<1":timeUsing}ms,当前分辨率${c.width}x${c.height}, 请求来自${from}`)}
} catch (e) {
console.error("PiPW Error: <canvas>绘制出错,详情:\n", e);tipMsg("<canvas>绘制出错,详见JavaScript控制台", "err")
}
}
plugin.onAllPluginsLoaded(()=>{load()});
function load() {B();C();D();E();F()
legacyNativeCmder.appendRegisterCall("PlayProgress", "audioplayer", (_, p) => {
playProgress = p*1000; let pZ = Math.floor(p), needLoadPiP = false;
if (pZ>tC||p<tC||readCfg.smoothProgessBar) {tC = p; needLoadPiP = true}
if (isDynamicLyrics || readCfg.autoScroll || (readCfg.backgroundFrom=="AMLL" && loadedPlugins["Apple-Musiclike-lyrics"])) {needLoadPiP = true}
if (needLoadPiP) {loadPiP(false, "PlayProgress")}
}); //requestAnimationFrame或setInterval会在网易云最小化后被优化,导致1FPS的感人帧率
async function B() { //监听自带词栏变动
await betterncm.utils.waitForElement(".m-lyric");
new MutationObserver(() => {
loadPiP(false, "NCM-LyricBar");
}).observe(q(".m-lyric"), {
characterData: true,
childList: true,
subtree: true,
});
}
async function C() { //监听LB插件词栏变动
await betterncm.utils.waitForElement(".lyric-bar");
new MutationObserver(() => {
loadPiP(false, "Plugin-LyricBar");
}).observe(q(".lyric-bar"), {
attributeFilter: ["offset"],
characterData: true,
childList: true,
subtree: true,
});
}
async function D() { //监听设置按钮点击事件
await betterncm.utils.waitForElement(`[data-plugin-slug="PiPWindow"]`);
q(`[data-plugin-slug="PiPWindow"]`).addEventListener("click", ()=>{
setTimeout(()=>{
readCfg.colorFrom=="albumCover"? colorPick(cover?cover:null) : colorPick()
}, 200)
})
}
async function E() { //监听颜色变动
let A = qAll("html, body");
for(i = 0; i < A.length; i++){
new MutationObserver(() => {
setTimeout(()=>{readCfg.colorFrom=="albumCover"? colorPick(cover?cover:null) : colorPick()}, 100)
}).observe(A[i], {
attributeFilter: ["style", "class"],
characterData: false,
});
};
}
async function F() { //向歌曲信息旁添加PiP开关
await betterncm.utils.waitForElement(".m-pinfo h3");
if (!b) {b = cE("div")}
b.id = "PiPW-Toggle"; b.title = "打开小窗"; b.classList.add("icn", "f-cp");
b.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width: 24px; height: 24px; transform: scale(.75);">${pO}</svg>`;
b.addEventListener("click", ()=>{pipToggle()})
q(".m-pinfo h3").appendChild(b);
new MutationObserver(() => { //歌曲信息变动会清掉开关,加回去
if (q(".m-pinfo h3") && !q(".m-pinfo h3 [id*=PiP]")) {
q(".m-pinfo h3").appendChild(b);
}
}).observe(q(".m-pinfo"), {
characterData: true,
childList: true,
subtree: true,
});
}
}
async function writeCfg(Cfg) { //写配置
localStorage.setItem("PiPWindowSettings", JSON.stringify(Cfg));
readCfg = JSON.parse(localStorage.getItem("PiPWindowSettings")); //刷新变量内存储的设置
};
async function saveCfg(all="all") { //保存设置
oldCfg = {...readCfg}
let a = Array.from(arguments);
if (a[0] == "all") {a = Object.keys(cfgDefault)}
for (let i = 0; i < a.length; i++) {
if (a[i] in cfgDefault) {
let key
switch (typeof cfgDefault[`${a[i]}`]) {
case "number":
let n = q(`#${a[i]}SetBox`, cP)
if (n) {
let set = n.value*1;
if (set == "undefined" || set == "null" || set == "") {set = n.placeholder*1; n.value = set}
key = set;
}
break;
case "string":
let str = q(`#${a[i]}SetBox`, cP), radios = qAll(`[name=${a[i]}]`, cP)
if (str) { //这里if...else if...两边不!能!调换位置,下同
let set = str.value;
if (set == "undefined" || set == "null" || set == "") {set = str.placeholder; str.value = set}
key = set;
} else if (radios) {
for (let i = 0; i < radios.length; i++) {
if (radios[i].checked) {
key = radios[i].value;
}
}
}
break;
case "boolean":
let swc = q(`#${a[i]}Switch`, cP), ckBox = q(`#${a[i]}CheckBox`, cP)
if (swc) {key = swc.checked} else if (ckBox) {key = ckBox.checked}
break;
default:
console.error(`PiPW Error: !! 不支持此设置项的类型: ${a[i]}`)
}
readCfg[`${a[i]}`] = key
} else {
console.error(`PiPW Error: 无效的设置项: ${a[i]}`)
}
}
writeCfg(readCfg); loadPiP(false, "Settings"); tipMsg("设置已更新");console.log("PiPW Log: 设置已保存", oldCfg, readCfg)
oldCfg.showTaskbarButton==readCfg.showTaskbarButton?"":taskbarButton(readCfg.showTaskbarButton) //特殊处理
};
async function resetCfg() { //重置设置
oldCfg = {...readCfg}
readCfg = {...cfgDefault}
a = Object.keys(cfgDefault)
for (let i = 0; i < a.length; i++) {
if (a[i] in cfgDefault) {
let key = cfgDefault[`${a[i]}`]
switch (typeof key) {
case "string":
let str = q(`#${a[i]}SetBox`, cP), radios = qAll(`[name=${a[i]}]`, cP)
if (str) {
str.value = key;
} else if (radios) {
for (let i = 0; i < radios.length; i++) {
if (radios[i].value == key) {
radios[i].checked = true
}
}
}
break;
case "number":
q(`#${a[i]}SetBox`, cP).value = key
break;
case "boolean":
let swc = q(`#${a[i]}Switch`, cP), ckBox = q(`#${a[i]}CheckBox`, cP)
if (swc) {swc.checked = key} else if (ckBox) {ckBox.checked = key}
break;
default:
console.error(`PiPW Error: !! 不支持此设置项的类型: ${a[i]}`)
}
} else {
console.error(`PiPW Error: 无效的设置项: ${a[i]}`)
}
}
writeCfg(readCfg); tipMsg("设置已重置");console.log("PiPW Log: 设置已重置并保存", oldCfg, readCfg)
setTimeout(()=>{loadPiP(false, "Settings");}, 100)
}
plugin.onConfig(()=>{
return getSettingsPage();
})
function getSettingsPage() {
colorPick()
if (!cP) {cP = cE("div")}
cP.setAttribute("id", "PiPWSettings");
cP.innerHTML = `
<style id="PiPWSettingsStyle0">
#PiPWSettings {
--pipws-fg: ${colorS.accent};
--pipws-bg: ${colorS.bgT};
--pipws-bg-wot: ${colorS.bg};
color: ${colorS.text};
}
</style>
<style>
#PiPWSettings {
padding-top: 10px;
line-height: 24px;
font-size: 16px;
}
#PiPWSettings .noAutoBr p {
display: inline;
}
#PiPWSettings .switch, #PiPWSettings .switch + p, #PiPWSettings .radio, #PiPWSettings .radio + p {
line-height: 34px;
}
#PiPWSettings ::selection {
color: var(--pipws-bg-wot);
background: var(--pipws-fg);
}
#PiPWSettings :disabled, #PiPWSettings :disabled + .slider {
cursor: not-allowed;
}
#PiPWSettings :disabled::selection {
color: #000000;
background: #888;
}
#PiPWSettings .part {
width: 550px;
margin: 20px 0 0;
padding: 10px 20px;
border: 1px solid #0000;
border-radius: 12px;
box-shadow: 0 0 25px -5px #0003;
background: var(--pipws-bg) padding-box;
backdrop-filter: blur(24px);
transition: .5s;
}
#PiPWSettings .parting {
height: 1px;
margin: 10px 0;
border: solid var(--pipws-fg);
border-width: 1px 0 0 0;
box-shadow: 0 0 3px var(--pipws-fg);
}
#PiPWSettings .partTitle {
font-size: 23px;
font-weight: bold;
line-height: 30px;
}
#PiPWSettings .subPart {
margin: 5px;
}
#PiPWSettings .subTitle {
width: fit-content;
padding: 2px;
margin: 6px 0 4px;
font-size: 20px;
font-weight: bold;
box-shadow: inset 0 -9px 3px -6px var(--pipws-fg);
}
#PiPWSettings .item {
display: inline-block;
margin-right: 5px;
}
#PiPWSettings .tipText {
line-height: 12px;
font-size: 12px;
}
#PiPWSettings .button {
color: var(--md-accent-color-secondary, var(--ncm-text)) !important;
font-size: 16px;
width: 90px;
height: 40px;
line-height: 0;
outline: 0;
box-shadow: 0 0 3px var(--pipws-fg);
border: 1px solid var(--pipws-fg);
border-radius: 10px;
background: var(--pipws-bg);
backdrop-filter: blur(12px);
transition: .1s;
}
#PiPWSettings .button:hover {
box-shadow: 0 0 6px var(--pipws-fg);
}
#PiPWSettings .button:active {
font-size: 14px;
border-width: 4px;
box-shadow: 0 0 8px var(--pipws-fg);
}