-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoffscreen.js
More file actions
40 lines (32 loc) · 1.18 KB
/
offscreen.js
File metadata and controls
40 lines (32 loc) · 1.18 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
// Offscreen document for playing audio in the background
// This runs in a DOM context where Audio is available
// Uses browser API with chrome fallback for cross-browser compatibility
const browserAPI = typeof browser !== 'undefined' ? browser : chrome
const SOUND_FILES = {
notification: 'notification.mp3',
cash: 'cash.mp3',
}
browserAPI.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'play-sound') {
const soundFile = SOUND_FILES[message.soundType]
if (!soundFile) {
console.log('[Offscreen] Unknown sound type:', message.soundType)
sendResponse({ success: false, error: 'Unknown sound type' })
return
}
const audio = new Audio(soundFile)
audio.volume = 0.5
audio.play()
.then(() => {
console.log('[Offscreen] ✅ Sound played:', message.soundType)
sendResponse({ success: true })
})
.catch((error) => {
console.error('[Offscreen] ❌ Sound failed:', error)
sendResponse({ success: false, error: error.message })
})
// Return true to indicate async response
return true
}
})
console.log('[Offscreen] Audio player ready')