-
Notifications
You must be signed in to change notification settings - Fork 0
/
offscreen.js
34 lines (26 loc) · 727 Bytes
/
offscreen.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
'use strict';
import Sound from './common/sound';
const sounds = {};
chrome.runtime.onMessage.addListener(handleMessages);
async function handleMessages(message) {
if (message.target !== 'noise-offscreen') {
return;
}
switch (message.type) {
case 'noise-play':
handlePlay(message.data);
break;
default:
console.warn(`Unexpected message type: '${message.type}'.`);
}
}
async function handlePlay(data) {
const { id, src } = data;
let sound = sounds[id];
if (!sound) {
sound = new Sound({id: id, src: src});
sound.src = src; // must provide src beforehand to avoid loadSrc from browser.storage (offscreen doc can't access it)
}
sounds[id] = sound;
sound.play();
}