Skip to content

Commit 5c45b03

Browse files
committed
Add offscreen for audio sounds
1 parent 18f2a1f commit 5c45b03

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

src/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"idle",
2020
"unlimitedStorage",
2121
"alarms",
22-
"notifications"
22+
"notifications",
23+
"offscreen"
2324
],
2425
"offline_enabled": true,
2526
"background": {

src/offscreen.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Offscreen</title>
8+
</head>
9+
10+
<body>
11+
<audio></audio>
12+
<script type="module" src="./offscreen.ts"></script>
13+
</body>
14+
</html>

src/offscreen.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Browser from 'webextension-polyfill';
2+
import { Messages } from './utils/messages';
3+
4+
console.log('ofscreen');
5+
6+
Browser.runtime.onMessage.addListener(msg => {
7+
console.log('ofscreen message');
8+
if (msg.message == Messages.PlayAudio) {
9+
if (msg.offscreen == undefined) return;
10+
11+
playAudio(msg.sound);
12+
}
13+
});
14+
15+
function playAudio(sound: string) {
16+
const audio = document.querySelector('audio');
17+
if (!audio) return;
18+
19+
const path = Browser.runtime.getURL(`../assets/pomodoro-sounds/${sound}`);
20+
audio.src = path;
21+
audio.volume = 1;
22+
audio.play();
23+
}

src/offscreen/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Browser from 'webextension-polyfill';
2+
3+
export async function createOffscreen() {
4+
const path = 'src/offscreen.html';
5+
const offscreenUrl = Browser.runtime.getURL(path);
6+
if (await chrome.offscreen.hasDocument()) return;
7+
await chrome.offscreen.createDocument({
8+
url: offscreenUrl,
9+
reasons: ['AUDIO_PLAYBACK'],
10+
justification: 'Play audio sounds',
11+
});
12+
}

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default defineConfig(({ mode }) => ({
5555
webExtension({
5656
manifest: generateManifest,
5757
watchFilePaths: ['package.json', 'manifest.json'],
58-
additionalInputs: ['src/block.html', 'src/welcome.html'],
58+
additionalInputs: ['src/block.html', 'src/welcome.html', 'src/offscreen.html'],
5959
}),
6060
copy({
6161
targets: [

0 commit comments

Comments
 (0)