-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpopup.js
114 lines (90 loc) · 3.45 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const STORAGE_KEY = "video-volume";
const MAX_VOL_STORAGE_KEY = "max-global-video-volume";
const STATUS_KEY = "video-volume-status-per-website";
let store = chrome.storage || window.storage;
let tbs = chrome.tabs || window.tabs;
let runtime = chrome.runtime || window.runtime;
document.addEventListener('DOMContentLoaded', function() {
var host = "";
const websiteSlider = new mdc.slider.MDCSlider(document.querySelector('#website-slider'));
const maxGlobalSlider = new mdc.slider.MDCSlider(document.querySelector('#max-global-slider'));
const status = document.getElementById('status');
const statusLabel = document.getElementById('status-label');
mdc.checkbox.MDCCheckbox.attachTo(document.querySelector('.mdc-checkbox'));
var websiteText = document.getElementById("volume-value-label");
var maxGlobalText = document.getElementById("max-volume-value-label");
var setWebsiteLabel = function(volume) {
websiteText.textContent = volume + '%';
}
var setMaxGlobalLabel = function(volume) {
maxGlobalText.textContent = volume + '%';
}
var currentTab;
tbs.query({active: true, currentWindow: true}, function(tabs) {
currentTab = tabs[0];
host = parseHostFromURL(tabs[0].url);
let key = STATUS_KEY + '_' + host;
store.local.get(key, function(data) {
if (key in data) {
status.checked = data[key];
} else {
status.checked = false;
}
websiteSlider.disabled = !status.checked;
maxGlobalSlider.disabled = status.checked;
setStatusLabel(!status.checked, host);
});
store.local.get(STORAGE_KEY + '_' + host, function(data) {
var volume = data[STORAGE_KEY + '_' + host];
if (isNaN(volume)) {
volume = 50;
}
websiteSlider.value = volume;
setWebsiteLabel(volume);
});
store.local.get(MAX_VOL_STORAGE_KEY, function(data) {
var volume = data[MAX_VOL_STORAGE_KEY];
if (!volume) {
volume = 50;
}
maxGlobalSlider.value = volume;
setMaxGlobalLabel(volume)
});
});
maxGlobalSlider.listen('MDCSlider:change', () => {
var key = {};
key[MAX_VOL_STORAGE_KEY] = maxGlobalSlider.value;
store.local.set(key, function() {
setMaxGlobalLabel(maxGlobalSlider.value);
});
});
websiteSlider.listen('MDCSlider:change', () => {
var key = {};
key[STORAGE_KEY + '_' + host] = websiteSlider.value;
store.local.set(key, function() {
setWebsiteLabel(websiteSlider.value);
});
tbs.sendMessage(currentTab.id, websiteSlider.value);
});
status.addEventListener('change', () => {
let enabled = status.checked;
websiteSlider.disabled = !enabled;
maxGlobalSlider.disabled = enabled;
let key = {};
key[STATUS_KEY + '_' + host] = enabled;
store.local.set(key);
setStatusLabel(!enabled, host);
});
function parseHostFromURL(url) {
var parser = document.createElement('a');
parser.href = url;
return parser.host;
}
function setStatusLabel(disabled, host) {
if (!disabled) {
//statusLabel.textContent = "Enabled for " + host
} else {
//statusLabel.textContent = "Disabled for " + host
}
}
});