-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
executable file
·73 lines (66 loc) · 1.76 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
const audioElement = new Audio();
audioElement.addEventListener("play", updateIcon);
audioElement.addEventListener("pause", updateIcon);
audioElement.addEventListener("load", updateIcon);
audioElement.addEventListener("loadend", updateIcon);
audioElement.addEventListener("emptied", updateIcon);
let clickMode = "pause";
async function togglePlayback() {
if (audioElement.src) {
if (audioElement.paused) {
if (clickMode !== "pause") {
audioElement.load();
}
await audioElement.play();
} else {
if (clickMode === "pause") {
audioElement.pause();
} else {
audioElement.load();
}
}
} else {
chrome.runtime.openOptionsPage();
}
}
function updateIcon() {
chrome.browserAction.setIcon({ path: iconPath() });
}
function iconPath() {
if (audioElement.paused) {
return "images/ic_play_arrow_black_24dp_2x.png";
} else {
return "images/ic_" + clickMode + "_black_24dp_2x.png";
}
}
chrome.browserAction.onClicked.addListener(togglePlayback);
chrome.commands.onCommand.addListener(async function(command) {
switch (command) {
case "play-pause":
await togglePlayback();
break;
case "stop":
audioElement.load();
break;
}
});
document.addEventListener("DOMContentLoaded", function() {
chrome.runtime.getBackgroundPage(function() {
chrome.storage.sync.get(
{
audioUrl: "https://audiocdn.mainstreaming.tv/101156/mmc",
autoPlay: false,
clickMode: "pause",
volume: 1
},
async function(items) {
clickMode = items.clickMode;
audioElement.src = items.audioUrl;
audioElement.volume = items.volume;
if (items.autoPlay) {
await audioElement.play();
}
}
);
});
});