-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
176 lines (152 loc) · 5.9 KB
/
script.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
const tsvscode = acquireVsCodeApi();
let artworkElement = document.querySelector(".album-artwork");
let nameElement = document.querySelector(".name");
let artistElement = document.querySelector(".artist");
let albumElement = document.querySelector(".album");
let playButton = document.querySelector(".play");
let pauseButton = document.querySelector(".pause");
let nextButton = document.querySelector(".next");
let previousButton = document.querySelector(".previous");
let debugElements = document.querySelector(".debug");
let radioNoticeElement = document.querySelector(".radio-notice");
let heartBeat = 0;
let currentMediaItem = {};
let audioKind = "song";
function postMessageToExtension(type, value = '') {
tsvscode.postMessage({
type: type,
value: value
});
}
function play() {
playButton.style.display = "none";
pauseButton.style.display = "inline-block";
postMessageToExtension('controlPlayback', 'play')
setTimeout(fetchPlaybackInfo, 500)
}
function pause() {
pauseButton.style.display = "none";
playButton.style.display = "inline-block";
postMessageToExtension('controlPlayback', 'pause')
setTimeout(fetchPlaybackInfo, 500)
}
function next() {
postMessageToExtension('controlPlayback', 'next')
setTimeout(fetchPlaybackInfo, 500)
}
function previous() {
postMessageToExtension('controlPlayback', 'previous')
setTimeout(fetchPlaybackInfo, 500)
}
function seekTo(time, adjust = true) {
// TODO: Implement seeking
// if (adjust) { time = parseInt(time / 1000) }
// dataSocket.send(JSON.stringify({ action: "seek", time: time }));
}
setInterval(fetchPlaybackInfo, 2000)
async function heartTap() {
heartBeat += 1;
if (heartBeat == 5) {
debugElements.style.display = "block";
postMessageToExtension('debugMenuOpened');
}
await new Promise((resolve) => setTimeout(resolve, 1000));
heartBeat -= 1;
}
function heartHide() {
heartBeat = 0;
debugElements.style.display = "none";
postMessageToExtension('debugMenuClosed');
}
async function fetchPlaybackInfo() {
postMessageToExtension('fetchPlaybackInfo');
}
window.addEventListener('message', (event) => {
const message = event.data;
switch (message.type) {
case "playbackInfo":
setData(message.value);
break;
}
});
async function setData(data) {
currentMediaItem = data;
if (!currentMediaItem) return;
if (currentMediaItem.playParams.kind) {
audioKind = currentMediaItem.playParams.kind
}
if (audioKind == "song" || audioKind == "musicVideo" || audioKind == "uploadedVideo" || audioKind == "music-movie") {
// Playback Info
if (currentMediaItem.artistName) {
if (artistElement.style.display == "none") {
artistElement.style.display = "block";
}
if (artistElement.innerText !== currentMediaItem.artistName) {
artistElement.innerText = currentMediaItem.artistName;
}
} else if (!currentMediaItem.artistName) {
artistElement.style.display = "none";
}
if (currentMediaItem.albumName && albumElement.innerText !== currentMediaItem.albumName) {
if (albumElement.style.display == "none") {
albumElement.style.display = "block";
}
albumElement.innerText = currentMediaItem.albumName;
} else if (!currentMediaItem.albumName) {
albumElement.style.display = "none";
}
// Radio Notice
radioNoticeElement.style.display = "none";
// Play/Pause Logic
if (currentMediaItem.isPlaying !== undefined) {
if (currentMediaItem.isPlaying == true) {
playButton.style.display = "none";
pauseButton.style.display = "inline-block";
} else {
pauseButton.style.display = "none";
playButton.style.display = "inline-block";
}
}
// Next/Previous Logic
if (currentMediaItem.state !== undefined || currentMediaItem.state !== undefined) {
nextButton.style.display = "inline-block";
previousButton.style.display = "inline-block";
}
if (currentMediaItem.name && nameElement.innerText !== currentMediaItem.name) {
nameElement.innerText = currentMediaItem.name;
}
} else if (audioKind == "radioStation") {
radioNoticeElement.style.display = "block";
if (currentMediaItem.editorialNotes.name && nameElement.innerText !== currentMediaItem.editorialNotes.name) {
nameElement.innerText = currentMediaItem.editorialNotes.name;
}
if (currentMediaItem.editorialNotes.short) {
if (artistElement.innerText !== currentMediaItem.editorialNotes.short) {
if (artistElement.style.display == "none") {
artistElement.style.display = "block";
}
artistElement.innerText = currentMediaItem.editorialNotes.short;
}
} else {
artistElement.style.display = "none";
}
albumElement.innerText = "LIVE - Radio Station";
// Hide Not Working Elements
playButton.style.display = "none";
pauseButton.style.display = "none";
nextButton.style.display = "none";
previousButton.style.display = "none";
}
// Song Name & No Title Check
if (currentMediaItem.name && nameElement.innerText !== currentMediaItem.name && nameElement.innerText == "No Title Found") {
artistElement.innerText = "";
albumElement.innerText = "";
artworkElement.style.display = "none";
artworkElement.src = "";
}
// Album Artwork
if (currentMediaItem.artwork && currentMediaItem.artwork.url.length > 0) {
artworkElement.src = currentMediaItem.artwork.url.replace('{w}', 600).replace('{h}', 600);
artworkElement.style.display = "block";
}
}