-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapplication.js
More file actions
97 lines (74 loc) · 2.64 KB
/
application.js
File metadata and controls
97 lines (74 loc) · 2.64 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
"use strict";
const getStationId = (group, station) => `${group}.${station}`;
const controlPlay = document.getElementById("cnt_play");
const controlVolume = document.getElementById("cnt_volume");
const playList = document.getElementById("play_list");
let currentState = { state: "paused", volume: 30, station: "TVR.KissFM" };
// Initialize popup with current state
async function initializePopup() {
currentState = await chrome.runtime.sendMessage({ type: 'GET_STATE' });
controlPlay.setAttribute("class", currentState.state);
controlVolume.value = currentState.volume;
playList.innerHTML = window.stationList
.map(({ name, group, station }) => {
const stationId = getStationId(group, station);
return `<li class="${currentState.station === stationId ? "selected" : ""}" data-id="${stationId}">
<span class="group">${group}</span>
<span class="name">${name}</span>
</li>`;
})
.join("");
if (document.querySelector(".selected")) {
document.querySelector(".selected").scrollIntoView();
}
}
// Play/Pause control
controlPlay.addEventListener("click", async () => {
if (currentState.state === "paused") {
await chrome.runtime.sendMessage({ type: 'PLAY' });
currentState.state = "played";
} else {
await chrome.runtime.sendMessage({ type: 'STOP' });
currentState.state = "paused";
}
controlPlay.setAttribute("class", currentState.state);
});
// Volume control
controlVolume.addEventListener("input", async (event) => {
const volume = event.target.value;
currentState.volume = volume;
await chrome.runtime.sendMessage({
type: 'SET_VOLUME',
volume: parseInt(volume)
});
});
controlVolume.addEventListener("mousewheel", async (e) => {
const value = +currentState.volume + e.wheelDelta / 24;
const volume = value < 0 ? 0 : value > 100 ? 100 : value;
controlVolume.value = volume;
currentState.volume = volume;
await chrome.runtime.sendMessage({
type: 'SET_VOLUME',
volume: parseInt(volume)
});
});
// List control
playList.addEventListener("click", async (event) => {
const element = event.target.closest("li");
if (!element) return;
if (document.querySelector(".selected")) {
document.querySelector(".selected").setAttribute("class", "");
}
element.setAttribute("class", "selected");
controlPlay.setAttribute("class", "played");
const stationId = element.getAttribute("data-id");
currentState.station = stationId;
currentState.state = "played";
await chrome.runtime.sendMessage({
type: 'SET_STATION',
station: stationId
});
await chrome.runtime.sendMessage({ type: 'PLAY' });
});
// Initialize on load
initializePopup();