-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
70 lines (66 loc) · 2.26 KB
/
background.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
let audio;
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.set({ playing: false, volume: 0.32, songTitle: '' });
}); // Default = 0.32
chrome.runtime.onStartup.addListener(() => {
chrome.storage.local.set({ playing: false });
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'play') {
chrome.storage.local.get('volume', (result) => {
if (!audio) {
audio = new Audio('https://radio.queercraft.net:443/stream/;'); // Station URL
audio.volume = result.volume; // Set initial volume from storage
}
audio.play().then(() => {
chrome.storage.local.set({ playing: true });
chrome.browserAction.setIcon({
path: {
"16": "icons/icon16_playing.png",
"32": "icons/icon32_playing.png",
"48": "icons/icon48_playing.png",
"64": "icons/icon64_playing.png",
"128": "icons/icon128_playing.png"
}
});
sendResponse({ success: true });
}).catch(error => {
console.error("Error playing audio:", error);
sendResponse({ success: false, error: error.message });
});
});
} else if (message.action === 'stop') {
if (audio) {
audio.pause();
audio.src = ''; // Reset the audio source to stop the stream
audio = null; // Clear the audio object
chrome.storage.local.set({ playing: false });
chrome.browserAction.setIcon({
path: {
"16": "icons/icon16.png",
"32": "icons/icon32.png",
"48": "icons/icon48.png",
"64": "icons/icon64.png",
"128": "icons/icon128.png"
}
});
sendResponse({ success: true });
} else {
sendResponse({ success: false, error: "No audio to stop" });
}
} else if (message.action === 'setVolume') {
if (audio) {
audio.volume = message.volume;
}
chrome.storage.local.set({ volume: message.volume }); // Store volume in storage
sendResponse({ success: true });
}
return true; // Ensure sendResponse is valid asynchronously
});
chrome.storage.onChanged.addListener((changes, area) => {
if (area === 'local' && changes.playing) {
if (!changes.playing.newValue && audio) {
audio.pause();
}
}
});