-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathlyrics.js
More file actions
113 lines (100 loc) · 2.83 KB
/
lyrics.js
File metadata and controls
113 lines (100 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
export function lyricParser(lrc) {
return {
lyric: parseLyric(lrc?.lrc?.lyric || ''),
tlyric: parseLyric(lrc?.tlyric?.lyric || ''),
romalyric: parseLyric(lrc?.romalrc?.lyric || ''),
lyricuser: lrc.lyricUser,
transuser: lrc.transUser,
};
}
// regexr.com/6e52n
const extractLrcRegex =
/^(?<lyricTimestamps>(?:\[.+?\])+)(?!\[)(?<content>.+)$/gm;
const extractTimestampRegex =
/\[(?<min>\d+):(?<sec>\d+)(?:\.|:)*(?<ms>\d+)*\]/g;
/**
* @typedef {{time: number, rawTime: string, content: string}} ParsedLyric
*/
/**
* Parse the lyric string.
*
* @param {string} lrc The `lrc` input.
* @returns {ParsedLyric[]} The parsed lyric.
* @example parseLyric("[00:00.00] Hello, World!\n[00:00.10] Test\n");
*/
export function parseLyric(lrc) {
/**
* A sorted list of parsed lyric and its timestamp.
*
* @type {ParsedLyric[]}
* @see binarySearch
*/
const parsedLyrics = [];
/**
* Find the appropriate index to push our parsed lyric.
* @param {ParsedLyric} lyric
*/
const binarySearch = lyric => {
let time = lyric.time;
let low = 0;
let high = parsedLyrics.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
const midTime = parsedLyrics[mid].time;
if (midTime === time) {
return mid;
} else if (midTime < time) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return low;
};
for (const line of lrc.trim().matchAll(extractLrcRegex)) {
const { lyricTimestamps, content } = line.groups;
for (const timestamp of lyricTimestamps.matchAll(extractTimestampRegex)) {
const { min, sec, ms } = timestamp.groups;
const rawTime = timestamp[0];
const time = Number(min) * 60 + Number(sec) + Number(ms ?? 0) * 0.001;
/** @type {ParsedLyric} */
const parsedLyric = { rawTime, time, content: trimContent(content) };
parsedLyrics.splice(binarySearch(parsedLyric), 0, parsedLyric);
}
}
return parsedLyrics;
}
/**
* @param {string} content
* @returns {string}
*/
function trimContent(content) {
let t = content.trim();
return t.length < 1 ? content : t;
}
/**
* @param {string} lyric
*/
export async function copyLyric(lyric) {
const textToCopy = lyric;
if (navigator.clipboard && navigator.clipboard.writeText) {
try {
await navigator.clipboard.writeText(textToCopy);
} catch (err) {
alert('复制失败,请手动复制!');
}
} else {
const tempInput = document.createElement('textarea');
tempInput.value = textToCopy;
tempInput.style.position = 'absolute';
tempInput.style.left = '-9999px';
document.body.appendChild(tempInput);
tempInput.select();
try {
document.execCommand('copy');
} catch (err) {
alert('复制失败,请手动复制!');
}
document.body.removeChild(tempInput);
}
}