-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
85 lines (74 loc) · 1.92 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"use strict";
let audioClip = null;
let isSoundPlaying = false;
function openTab(name) {
chrome.tabs.create({
url: chrome.extension.getURL(`particles/html/${name}.html`),
active: true
});
}
function playBackgroundSound(file) {
if (audioClip) {
audioClip.pause();
// stop the same audio playing
if (audioClip.src === file && isSoundPlaying == true) {
isSoundPlaying = false;
chrome.storage.sync.set({ isSoundPlaying: false });
return;
}
}
audioClip = new Audio(file);
audioClip.loop = true;
audioClip.play();
isSoundPlaying = true;
chrome.storage.sync.set({ isSoundPlaying: true });
// gapless looping of audio
audioClip.addEventListener("timeupdate", () => {
const buffer = 0.44;
if (audioClip.currentTime > audioClip.duration - buffer) {
audioClip.currentTime = 0;
audioClip.play();
}
});
audioClip.addEventListener("pause", () => {
chrome.storage.sync.set({ isSoundPlaying: false });
chrome.runtime.sendMessage({
type: "PAUSE_BUTTON",
lastPlayedFile: audioClip.currentSrc
});
});
audioClip.addEventListener("play", () => {
chrome.storage.sync.set({ isSoundPlaying: true });
chrome.runtime.sendMessage({
type: "PLAY_BUTTON",
lastPlayedFile: audioClip.currentSrc
});
});
audioClip.addEventListener("error", e => {
console.log(e);
});
audioClip.addEventListener("ended", () => {
console.log("Audio Has ended");
});
audioClip.addEventListener("abort", () => {
console.log("Audio has been aborted");
});
}
chrome.windows.onRemoved.addListener(function() {
audioClip.pause();
chrome.storage.sync.clear();
});
chrome.runtime.onMessage.addListener(function(request) {
switch (request.type) {
case "OPEN_TAB":
openTab(request.name);
break;
case "PLAY_SOUND":
{
playBackgroundSound(request.file);
}
break;
default:
break;
}
});