-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpopup.js
86 lines (77 loc) · 2.05 KB
/
popup.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
86
function listenForEvents() {
function err(error) {
console.error(`Volume Control: Error: ${error}`);
}
function setSlider(dB) {
document.querySelector("#volume-slider").value = dB;
var text = document.querySelector("#volume-text");
text.textContent = "";
if (dB >= 0) {
text.textContent = "+";
}
text.textContent += dB + " dB";
}
function getVolume(tabs) {
browser.tabs.sendMessage(tabs[0].id, {
command: "getVolume"
}).then((message) => {
setSlider(message.response);
}).catch(err);
}
browser.tabs.query({active:true, currentWindow:true})
.then(getVolume)
.catch(err);
document.addEventListener("input", (e) => {
function sendVolume(tabs) {
setSlider(e.target.value);
browser.tabs.sendMessage(tabs[0].id, {
command: "setVolume",
dB: e.target.value
});
}
if (e.target.id == "volume-slider") {
browser.tabs.query({active:true, currentWindow:true})
.then(sendVolume)
.catch(err);
}
});
function getMono(tabs) {
browser.tabs.sendMessage(tabs[0].id, {
command: "getMono"
}).then((message) => {
document.querySelector("#mono-checkbox").checked = message.response;
}).catch(err);
}
browser.tabs.query({active:true, currentWindow:true})
.then(getMono)
.catch(err);
document.addEventListener("change", (e) => {
function sendMono(tabs) {
browser.tabs.sendMessage(tabs[0].id, {
command: "setMono",
mono: e.target.checked
});
}
if (e.target.id == "mono-checkbox") {
browser.tabs.query({active:true, currentWindow:true})
.then(sendMono)
.catch(err);
}
});
}
function showError(error) {
document.querySelector("#popup-content").classList.add("hidden");
document.querySelector("#error-content").classList.remove("hidden");
console.error(`Volume Control: Error: ${error.message}`);
}
browser.tabs.query({active:true, currentWindow:true, audible:true})
.then(function(tabs) {
if (tabs.length != 0) {
browser.tabs.executeScript({file: "cs.js"})
.then(listenForEvents)
.catch(showError);
} else {
showError("No audio playing.");
}
})
.catch(showError);