forked from macmorning/pomodoro-webext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffscreen.js
More file actions
48 lines (40 loc) · 1.55 KB
/
offscreen.js
File metadata and controls
48 lines (40 loc) · 1.55 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
// Offscreen document for audio playback in Manifest V3
const audio = document.getElementById('notificationSound');
let currentSrc = '';
// Preload default sound
audio.preload = 'auto';
audio.src = 'sound/bell-ringing-02.mp3';
currentSrc = audio.src;
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'PLAY_SOUND') {
try {
const soundSrc = message.soundData || 'sound/bell-ringing-02.mp3';
// Only change src if different to avoid reloading
if (currentSrc !== soundSrc) {
audio.src = soundSrc;
currentSrc = soundSrc;
}
audio.volume = message.volume || 1.0;
// Reset to beginning if already playing
audio.currentTime = 0;
audio.play().then(() => {
console.log('Audio started playing');
sendResponse({ success: true });
}).catch((e) => {
console.error('Error playing sound:', e);
sendResponse({ success: false, error: e.message });
});
// Stop after 5 seconds
setTimeout(() => {
if (!audio.paused) {
audio.pause();
audio.currentTime = 0;
}
}, 5000);
} catch (e) {
console.error('Error in sound handler:', e);
sendResponse({ success: false, error: e.message });
}
}
return true;
});