forked from nondanee/vsc-netease-music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
108 lines (97 loc) · 3.07 KB
/
index.html
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
<!DOCTYPE html>
<html>
<script>
const audio = new Audio()
const Lyric = () => {
const lyric = {}
const parse = lrc => lrc.trim().split('\n').filter(item => item).forEach(line => {
let point = line.slice(1, line.indexOf(']')).split(':')
let text = line.slice(line.indexOf(']') + 1).trim()
if (isNaN(point[0])) return
point = (parseInt(point[0]) * 60 + parseFloat(point[1])).toFixed(2)
if (!(point in lyric)) lyric[point] = []
lyric[point].push(text)
})
song.lyric.filter(lrc => lrc).forEach(parse)
const timeline = Object.keys(lyric).sort((x, y) => parseFloat(x) - parseFloat(y))
let cursor = 0
return second => {
if (second > timeline[cursor]) {
cursor += 1
return lyric[timeline[cursor - 1]].join(' ')
}
}
}
let song = null
let lyric = null
audio.addEventListener('play', () => {
postMessage('event', {name: 'play'})
}, false)
audio.addEventListener('pause', () => {
postMessage('event', {name: 'pause'})
}, false)
audio.addEventListener('ended', () => {
postMessage('event', {name: 'end'})
}, false)
audio.addEventListener('timeupdate', () => {
let update = lyric(audio.currentTime)
if (update) postMessage('event', {name: 'lyric', data: update})
}, false)
audio.addEventListener('volumechange', () => {
postMessage('event', {name: audio.muted ? 'mute' : 'unmute'})
})
audio.addEventListener('loadstart', () => {
postMessage('event', {name: 'load', data: song})
}, false)
const webSocket = new WebSocket('ws://localhost:16363')
webSocket.addEventListener('message', event => {
let data = JSON.parse(event.data)
receiveMessage(data.command, data.data)
})
const postMessage = (type, body) => webSocket.send(JSON.stringify({type, body}))
const receiveMessage = (command, data) => {
if (command === 'load') {
let play = (!audio.src || !audio.paused || audio.ended)
audio.src = data.url
if (play) audio.play()
song = data
lyric = Lyric()
}
else if (command === 'play') {
if (audio.paused) audio.play()
}
else if (command === 'pause') {
if (!audio.paused) audio.pause()
}
else if (command === 'mute') {
if (!audio.muted) audio.muted = true
}
else if (command === 'unmute') {
if (audio.muted) audio.muted = false
}
else if (command === 'volume_up') {
audio.volume += 0.2
audio.volume = Math.min(1, audio.volume)
}
else if (command === 'volume_down') {
audio.volume -= 0.2
audio.volume = Math.max(0, audio.volume)
}
else if (command === 'volume_change_repeat') {
if(audio.volume > 0.8) audio.volume = 0
else audio.volume += 0.2
}
}
const interacted = () => {
postMessage('event', {name: 'ready'})
document.removeEventListener('click', interacted)
document.removeEventListener('touchend', interacted)
document.body.innerHTML = 'Please preserve this webview tab'
}
document.addEventListener('click', interacted)
document.addEventListener('touchend', interacted)
</script>
<body>
Please interact with the document first otherwise play() will failed
</body>
</html>