-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopout-script.js
More file actions
186 lines (151 loc) · 5.91 KB
/
popout-script.js
File metadata and controls
186 lines (151 loc) · 5.91 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
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
177
178
179
180
181
182
183
184
185
186
import { stations } from './stations.js';
const stationSelect = document.getElementById('station-select');
const playPauseBtn = document.getElementById('play-pause-btn');
const volumeSlider = document.getElementById('volume-slider');
const nowPlayingStation = document.getElementById('now-playing-station');
const nowPlayingTrack = document.getElementById('now-playing-track');
const PROXY_URL = 'https://api.djay.ca/';
let metadataInterval = null;
const audio = new Audio();
audio.crossOrigin = 'anonymous';
let isPlaying = false;
// Helper: Determine the actual URL to feed the audio element
function getProxiedAudioUrl(url) {
if (!url) return '';
// Route ALL streams through our secure proxy to inject CORS headers.
return `${PROXY_URL}?url=${encodeURIComponent(url)}`;
}
// --- UI Update Functions ---
function updatePlayPauseIcon(playing) {
const playIcon = playPauseBtn.querySelector('.icon-play');
const pauseIcon = playPauseBtn.querySelector('.icon-pause');
if (playIcon && pauseIcon) {
playIcon.style.display = playing ? 'none' : 'block';
pauseIcon.style.display = playing ? 'block' : 'none';
}
}
function updateVolumeSliderTrack(value) {
const progress = value * 100;
// Note: We need to get the CSS variables from the document to use them here.
const primaryColor = getComputedStyle(document.documentElement).getPropertyValue('--primary-color').trim();
const borderColor = getComputedStyle(document.documentElement).getPropertyValue('--border-color').trim();
volumeSlider.style.background = `linear-gradient(to right, ${primaryColor} ${progress}%, ${borderColor} ${progress}%)`;
}
async function fetchMetadata(streamUrl) {
if (!nowPlayingTrack) return;
try {
const response = await fetch(`${PROXY_URL}metadata?url=${encodeURIComponent(streamUrl)}`);
const data = await response.json();
if (data.title) {
nowPlayingTrack.textContent = data.title;
if (nowPlayingTrack.scrollWidth > nowPlayingTrack.parentElement.clientWidth) {
nowPlayingTrack.classList.add('marquee-active');
} else {
nowPlayingTrack.classList.remove('marquee-active');
}
} else {
const stationName = stationSelect.options[stationSelect.selectedIndex]?.text || '';
nowPlayingTrack.textContent = stationName;
nowPlayingTrack.classList.remove('marquee-active');
}
} catch (error) {
nowPlayingTrack.textContent = stationSelect.options[stationSelect.selectedIndex]?.text || '';
nowPlayingTrack.classList.remove('marquee-active');
}
}
function updateNowPlaying() {
if (!nowPlayingStation || !nowPlayingTrack) return;
const selectedOption = stationSelect.options[stationSelect.selectedIndex];
if (!selectedOption) return;
nowPlayingStation.textContent = `Now Playing: ${selectedOption.text}`;
nowPlayingTrack.textContent = "Loading string info...";
nowPlayingTrack.classList.remove('marquee-active');
if (metadataInterval) {
clearInterval(metadataInterval);
metadataInterval = null;
}
const streamUrl = stationSelect.value;
if (isPlaying) {
nowPlayingTrack.textContent = "Loading track info...";
nowPlayingTrack.classList.remove('marquee-active');
fetchMetadata(streamUrl);
} else {
nowPlayingTrack.textContent = "Ready to play...";
nowPlayingTrack.classList.remove('marquee-active');
}
metadataInterval = setInterval(() => {
if (isPlaying) fetchMetadata(streamUrl);
}, 12000);
}
// --- Player Logic ---
function togglePlay() {
isPlaying = !isPlaying;
if (isPlaying) {
audio.play().catch(err => {
console.error('Playback failed:', err);
if (nowPlayingTrack) nowPlayingTrack.textContent = 'Error: Unable to play stream';
isPlaying = false;
});
} else {
audio.pause();
}
updatePlayPauseIcon(isPlaying);
updateNowPlaying();
}
// --- Event Listeners ---
playPauseBtn.addEventListener('click', togglePlay);
stationSelect.addEventListener('change', () => {
audio.src = getProxiedAudioUrl(stationSelect.value);
updateNowPlaying();
if (isPlaying) {
audio.play();
}
});
volumeSlider.addEventListener('input', () => {
audio.volume = volumeSlider.value;
updateVolumeSliderTrack(audio.volume);
});
// Notify main window when pop-out is closed
window.addEventListener('beforeunload', () => {
if (window.opener && !window.opener.closed) {
window.opener.postMessage({ type: 'popoutClosed' }, '*');
}
});
// --- Initialization ---
function init() {
// Merge default and custom stations
const customStations = JSON.parse(localStorage.getItem('customStations')) || [];
const allStations = [...stations, ...customStations];
// Populate stations
allStations.forEach(station => {
const option = document.createElement('option');
option.value = station.url;
option.textContent = station.name;
stationSelect.appendChild(option);
});
// Get initial state from URL
const params = new URLSearchParams(window.location.search);
const initialStation = params.get('station');
const theme = params.get('theme');
// Apply theme
if (theme === 'dark') {
document.documentElement.classList.add('dark-theme');
}
// Set initial station and volume
if (initialStation) {
stationSelect.value = initialStation;
audio.src = getProxiedAudioUrl(initialStation);
} else {
if (stationSelect.options.length > 0) {
audio.src = getProxiedAudioUrl(stationSelect.value);
}
}
// For simplicity, start with a default volume
const initialVolume = 0.5;
audio.volume = initialVolume;
volumeSlider.value = initialVolume;
updateVolumeSliderTrack(initialVolume);
updateNowPlaying();
updatePlayPauseIcon(false);
}
init();