-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings_js.js
More file actions
124 lines (89 loc) · 2.95 KB
/
settings_js.js
File metadata and controls
124 lines (89 loc) · 2.95 KB
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
115
116
117
118
119
120
121
122
123
124
function addSliderEventListeners(){
// First Slider
var startActiveHSlider = document.getElementById("activeStartH");
startActiveHSlider.addEventListener("change", function() {
document.getElementById("startHourValue").textContent = startActiveHSlider.value;
}, false);
// Second Slider
var stopActiveSlider = document.getElementById("activeStopH");
stopActiveSlider.addEventListener("change", function() {
document.getElementById("stopHourValue").textContent = stopActiveSlider.value;
}, false);
}
function saveOptions(e){
e.preventDefault();
// Getting handlers of html elms ag
startHour = Number(document.getElementById("activeStartH").value);
stopHour = Number(document.getElementById("activeStopH").value);
urlField = document.getElementById("urlInputField").value;
// Saving settings
setSettings(startHour,0,stopHour,0,urlField);
}
function setSettings(sh,sm,oh,om,vidUrl){
// Takes a key object containing one or more key/value pairs
function setItem(){
}
function onError(error) {
console.error(error)
}
var obj = {"settings":{"startActiveH":sh,"startActiveM":sm,"stopActiveH":oh,"stopActiveM":om,"replaceVideoUrl":vidUrl}};
var promise = browser.storage.local.set(obj);
promise.then(setItem,onError);
}
function resetValues(){
// Takes a key object containing one or more key/value pairs
function setItem(){
}
function onError(error) {
console.error(error)
}
var obj = {"settings":{"startActiveH":22,"startActiveM":0,"stopActiveH":6,"stopActiveM":0,"replaceVideoUrl":"https://www.youtube.com/watch?v=KtXDvqZi6dI"}};
var promise = browser.storage.local.set(obj);
promise.then(setItem,onError);
}
function prefillSettings(settings){
// Getting handles of html objects
slider1 = document.getElementById("activeStartH");
slider2 = document.getElementById("activeStopH");
urlField = document.getElementById("urlInputField");
// Setting values to stored ones
slider1.value = settings.startActiveH;
slider2.value = settings.stopActiveH;
urlField.value = settings.replaceVideoUrl;
}
function getWrapper(callbackFunc){
function onSucc(item){
retValue = item.settings;
callbackFunc(retValue);
}
function onError(error){
console.info(error);
}
let result = browser.storage.local.get("settings");
result.then(onSucc,onError);
}
function clearSettings(){
var res = browser.storage.local.clear();
res.then(()=>{console.log("Cleared");},()=>{});
}
function isInited(notInitedCaller){
function onSucc(item){
if (item.settings === undefined){
// Means that the data is not yet initialised
notInitedCaller();
}
}
function onError(e){
console.error("Error:",e);
}
var result = browser.storage.local.get("settings");
result.then(onSucc,onError);
}
function main(){
isInited(resetValues);
document.addEventListener("DOMContentLoaded", ()=>{getWrapper(prefillSettings);});
document.querySelector("form").addEventListener("submit", saveOptions);
addSliderEventListeners();
}
//clearSettings();
main();