Skip to content

Commit e2aebe7

Browse files
authored
v0.4.1 更新
1 parent dac4451 commit e2aebe7

2 files changed

Lines changed: 52 additions & 45 deletions

File tree

main.js

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*This plugin is licensed under the GNU/GPL-3.0*/
22
let rvN, v, c, cover, cvUrlCache, songIdCache, songDataCache, tMsT, lrcCache, pLrc, pLrcKeys, showRefrshing, thePiPWindow
3-
, isVLsnAdded=false, DontPlay=false, DontPause=false, autoRatio, autoRatioValue=480, lastReRatio=0, playProgress=0, nrLrc=false, lrcNowLoading=false, reRatioPending=false, isDynamicLyrics=false, debugMode=false
3+
, isVLsnAdded=false, DontPlay=false, DontPause=false, autoRatio, autoRatioValue=480, lastReRatio=0, playProgress=0, nrLrc=false, lrcNowLoading=false, reRatioPending=false, isDynamicLyrics=false, debugMode=false, lrcRnpLsnAdded=false
44
, t = "0:00/0:00", tC = 0, tT = 0, tP = 0, tR = 0 //显示用,Current,Total,PassedRate,Remaining
55
, 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"
66
, 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>`
@@ -14,6 +14,7 @@ let rvN, v, c, cover, cvUrlCache, songIdCache, songDataCache, tMsT, lrcCache, pL
1414
, smoothProgessBar: true, resolutionRatio: "auto", aspectRatio: "2:1", customLoadingTxt: "正在载入猫猫…"})
1515
, color = ({accent: "", text: "", textT13: "", textT31: "", textT42: "", textT56: "", bg: "", bgT00: "", bgTSetting: ""}), colorCache = ({text: "", bg: ""})
1616
readCfg = {...cfgDefault, ...readCfg} //缺失配置啥的处理一下
17+
let oldCfg = {...readCfg}
1718
window.PiPWShowRefrshing = (x=true)=>{if(x==true){showRefrshing=true;return true}else if(x==false){showRefrshing=false;return false}}
1819
function cE(n, d=document) {return d.createElement(n)}
1920
function q(n, d=document) {return d.querySelector(n)}
@@ -223,6 +224,7 @@ async function loadPiP(isToPiP=true, from="unknow") {
223224
}
224225

225226
if (data.id != songIdCache) {reloadHead(); songIdCache = data.id; nrLrc=true}
227+
if (from=="Settings" && oldCfg.lyricsFrom!=readCfg.lyricsFrom) {nrLrc=true}
226228

227229
/*时间*/
228230
try {
@@ -251,10 +253,16 @@ async function loadPiP(isToPiP=true, from="unknow") {
251253
currentT: 0,
252254
currentD: 0,
253255
};
256+
if (readCfg.lyricsFrom != "RNP") {
257+
document.removeEventListener("lyrics-updated", rnpLrcUpdate);
258+
lrcRnpLsnAdded = false
259+
}
254260
switch (readCfg.lyricsFrom) {
255261
case "RNP":getLrcRnp();break
256262
case "OriginalLyricBar":getLrcOrg();break
257263
case "LibLyric":getLrcLibLyric();break
264+
case "Native":getLrcNative();//break
265+
case "Custom":getLrcCustom();//break
258266
default: getLrcErr()
259267
}
260268
function getLrcErr() {
@@ -268,34 +276,26 @@ async function loadPiP(isToPiP=true, from="unknow") {
268276
};
269277
pLrcKeys = Object.keys(pLrc)
270278
}
279+
function rnpLrcUpdate(e) {
280+
pLrc = JSON.parse(JSON.stringify(e.detail.lyrics))
281+
pLrcKeys = Object.keys(pLrc)
282+
for (let i = 0; i < pLrcKeys.length; i++) {
283+
let o = pLrc[i].originalLyric
284+
pLrc[i].originalLyric = o.replace(/\s+/g, " ").trim();
285+
if (o == "") {pLrc[i].originalLyric = "· · ·", pLrc[i].translatedLyric = ""}
286+
}
287+
console.log("PiPW Log: GotLyrics", pLrc)
288+
};
271289
function getLrcRnp() {
272-
if (q(".m-pinfo [data-log-type='dj']")) {return}
273-
let l = ".rnp-lyrics-line", o = `${l}-original`, k = `${l}-karaoke`, t = `${l}-translated`
274-
if (q(".rnp-lyrics-line")) {
275-
for (i = 0; i <= 4; i++) {
276-
try {
277-
try {
278-
lyrics[`M${i}`] = q(`${l}[offset='${i}'] ${o}`).textContent;
279-
} catch {
280-
lyrics[`M${i}`] = q(`${l}[offset='${i}'] ${k}`).textContent;
281-
}
282-
try {lyrics[`T${i}`] = q(`${l}[offset='${i}'] ${t}`).textContent}catch{}
283-
} catch {
284-
if (q(`${l}.rnp-interlude[offset='${i}']`)) {
285-
lyrics[`M${i}`] = "· · ·";
286-
lyrics[`T${i}`] = "";
287-
}
288-
if (i == 0 && !q(`${l}[offset='0']`) && q(`${l}[offset='-1']`)) {
289-
try {
290-
lyrics[`M${i}`] = q(`${l}[offset='-1'] ${o}`).textContent;
291-
} catch {
292-
lyrics[`M${i}`] = q(`${l}[offset='-1'] ${k}`).textContent;
293-
}
294-
try {lyrics[`T${i}`] = q(`${l}[offset='-1'] ${t}`).textContent}catch{}
295-
}
296-
}
290+
if (!loadedPlugins.RefinedNowPlaying) {getLrcErr()}
291+
try {
292+
if (!lrcRnpLsnAdded) {
293+
document.addEventListener("lyrics-updated", rnpLrcUpdate);
294+
lrcRnpLsnAdded = true
295+
rnpLrcUpdate({detail:window.currentLyrics})
297296
}
298-
} else {getLrcErr()}
297+
lrcUpdate();
298+
} catch(e) {console.error(`PiPW Error: 获取歌词时出错,详情:\n${e}`);getLrcErr()}
299299
}
300300
function getLrcOrg() {
301301
try {
@@ -308,6 +308,7 @@ async function loadPiP(isToPiP=true, from="unknow") {
308308
}
309309
async function getLrcLibLyric() {
310310
if (lrcNowLoading) {lyrics["M0"] = ldTxt;return}
311+
if (!loadedPlugins.liblyric) {getLrcErr()}
311312
try {
312313
let ll = loadedPlugins.liblyric
313314
if (nrLrc) {
@@ -332,6 +333,12 @@ async function loadPiP(isToPiP=true, from="unknow") {
332333
lrcUpdate();
333334
} catch(e) {lrcNowLoading = false;console.error(`PiPW Error: 获取歌词时出错,详情:\n${e}`);getLrcErr()}
334335
}
336+
function getLrcNative() {
337+
//nocode
338+
}
339+
function getLrcCustom() {
340+
//nocode
341+
}
335342
function lrcUpdate() {
336343
let l = pLrcKeys.length
337344
for (let i = 0; i < l; i++) {
@@ -348,13 +355,11 @@ async function loadPiP(isToPiP=true, from="unknow") {
348355
lyrics["M2"] = i+2<l?pLrc[i+2].originalLyric:""
349356
lyrics["M3"] = i+3<l?pLrc[i+3].originalLyric:""
350357
lyrics["M4"] = i+4<l?pLrc[i+4].originalLyric:""
351-
if (lrcCache.tlyric&&lrcCache.tlyric.lyric!="") {
352-
lyrics["T0"] = pLrc[i].translatedLyric
353-
lyrics["T1"] = i+1<l?pLrc[i+1].translatedLyric:""
354-
lyrics["T2"] = i+2<l?pLrc[i+2].translatedLyric:""
355-
lyrics["T3"] = i+3<l?pLrc[i+3].translatedLyric:""
356-
lyrics["T4"] = i+4<l?pLrc[i+4].translatedLyric:""
357-
}
358+
lyrics["T0"] = pLrc[i].translatedLyric?pLrc[i].translatedLyric:""
359+
lyrics["T1"] = i+1<l?pLrc[i+1].translatedLyric?pLrc[i+1].translatedLyric:"":""
360+
lyrics["T2"] = i+2<l?pLrc[i+2].translatedLyric?pLrc[i+2].translatedLyric:"":""
361+
lyrics["T3"] = i+3<l?pLrc[i+3].translatedLyric?pLrc[i+3].translatedLyric:"":""
362+
lyrics["T4"] = i+4<l?pLrc[i+4].translatedLyric?pLrc[i+4].translatedLyric:"":""
358363
}
359364
}
360365
}
@@ -446,23 +451,24 @@ async function loadPiP(isToPiP=true, from="unknow") {
446451
isDynamicLyrics = false
447452
if (Array.isArray(lyrics["M0"])) {
448453
isDynamicLyrics = true
449-
let lyricDO = ""/*lyricDynamicOrigin*/, l = lyrics["M0"].length, now = 0
454+
let lyricDO = ""/*lyricDynamicOrigin*/, l = lyrics["M0"].length, now = 0, nowWidth = 0
450455
lyricDO = lyrics["M0"].map(item => item.word).join("");
456+
lrcMNowUnplayed();
451457
for (let i=0; i<l; i++) {
452458
let lrc = lyrics["M0"][i], Cnow = (playProgress-lrc.time)/lrc.duration
453459
Cnow = lrc.time>playProgress ? 0 : Cnow>1?1:Cnow
454-
now += Cnow
460+
nowWidth += cC.measureText(lrc.word).width*Cnow
455461
}
456-
now = now/l
457-
lrcMNowUnplayed(); let w = cC.measureText(lyricDO).width
462+
let w = cC.measureText(lyricDO).width
463+
now = nowWidth/w
458464
updateMLrcMgL(w, now);
459465
cC.fillText(lyricDO, mLrcMgL, lrc0); /*主歌词(未播放)*/
460466
lrcMNow(); cC.save();
461467
cC.beginPath();
462468
cC.rect(0, 0, w*now+mLrcMgL, c.height); cC.clip();
463469
cC.fillText(lyricDO, mLrcMgL, lrc0); /*主歌词(已播放)*/
464470
cC.restore();
465-
} else if (readCfg.lyricsFrom=="LibLyric") {
471+
} else if (readCfg.lyricsFrom!="OriginalLyricBar") {
466472
lrcMNow();
467473
updateMLrcMgL(cC.measureText(lyrics["M0"]).width);
468474
cC.fillText(lyrics["M0"], mLrcMgL, lrc0); /*主歌词*/
@@ -472,7 +478,7 @@ async function loadPiP(isToPiP=true, from="unknow") {
472478

473479
if (lyrics["T0"]!="") {
474480
lrcTNow();
475-
if (readCfg.lyricsFrom=="LibLyric") {
481+
if (readCfg.lyricsFrom!="OriginalLyricBar") {
476482
mLrcMgL = lrcMgL
477483
updateMLrcMgL(cC.measureText(lyrics["T0"]).width);
478484
}
@@ -636,6 +642,7 @@ async function writeCfg(Cfg) { //写配置
636642
};
637643

638644
async function saveCfg(all="all") { //保存设置
645+
oldCfg = {...readCfg}
639646
let a = Array.from(arguments);
640647
if (a[0] == "all") {a = Object.keys(cfgDefault)}
641648
for (let i = 0; i < a.length; i++) {
@@ -668,7 +675,7 @@ async function saveCfg(all="all") { //保存设置
668675
console.error(`PiPW Error: 无效的设置项: ${a[i]}`)
669676
}
670677
}
671-
writeCfg(readCfg); loadPiP(false, "Settings"); tipMsg("设置已更新");console.log("PiPW Log: 设置已保存")
678+
writeCfg(readCfg); loadPiP(false, "Settings"); tipMsg("设置已更新");console.log("PiPW Log: 设置已保存", oldCfg, readCfg)
672679
};
673680

674681
plugin.onConfig(()=>{
@@ -947,7 +954,7 @@ plugin.onConfig(()=>{
947954
}
948955
</style>
949956
<div class="part noAutoBr" style="margin-top: 0;">
950-
<p class="partTitle">PiPWindow </p><p> 0.4.0</p>
957+
<p class="partTitle">PiPWindow </p><p> 0.4.1</p>
951958
<br />
952959
<p>by </p>
953960
<input class="link" type="button" onclick="betterncm.ncm.openUrl('https://github.com/Lukoning')" value=" Lukoning " />
@@ -1071,7 +1078,7 @@ plugin.onConfig(()=>{
10711078
<input id="dynamicLyricsSwitch" type="checkbox" />
10721079
<span class="slider button"></span>
10731080
</label>
1074-
<p>逐字歌词* (需要词源LibLyric)</p>
1081+
<p>逐字歌词*</p>
10751082
<br />
10761083
<label class="switch">
10771084
<input id="autoScrollSwitch" type="checkbox" />
@@ -1125,7 +1132,7 @@ plugin.onConfig(()=>{
11251132
<input type="radio" name="lyricsFrom" value="RNP" ${loadedPlugins.RefinedNowPlaying?"":"disabled"}/>
11261133
<span class="slider button"></span>
11271134
</label>
1128-
<p>LyricBar插件/RefinedNowPlaying插件</p>
1135+
<p>RefinedNowPlaying插件</p>
11291136
</div>
11301137
<br />
11311138
<label class="switch">

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "PiPWindow",
44
"slug": "PiPWindow",
55
"type": "extension",
6-
"version": "0.4.0",
6+
"version": "0.4.1",
77
"author": "Lukoning",
88
"author_links": ["https://github.com/Lukoning"],
99
"description": "一个纯JavaScript实现的歌曲信息小窗",

0 commit comments

Comments
 (0)