-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
143 lines (135 loc) · 4.08 KB
/
options.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var settings;
var messageTimeout;
// if (settings == undefined) {
// settings = {
// enabled: false,
// how_often: 15,
// disable_shelf: false
// };
// }
window.onload = function () {
try {
chrome.storage.sync.get(['enabled', 'how_often', 'disable_shelf'], function (loadedSettings) {
if (loadedSettings) {
settings = loadedSettings;
var firstLoad = false;
if (typeof settings.disable_shelf === "undefined") {
settings.disable_shelf = false;
firstLoad = true;
}
if (typeof settings.enabled === "undefined") {
settings.enabled = false;
firstLoad = true;
}
if (typeof settings.how_often === "undefined") {
settings.how_often = 30;
firstLoad = true;
}
if (!!firstLoad) {
if (!!localStorage["settings"]) {
try {
oldSettings = JSON.parse(localStorage["settings"]);
if (typeof oldSettings == 'object') {
settings = oldSettings;
if (typeof settings.enabled === "undefined") settings.enabled = false;
if (typeof settings.disable_shelf === "undefined") settings.disable_shelf = false;
if (typeof settings.how_often === "undefined") settings.how_often = 30;
}
} catch (err) {
console.log(err);
}
}
saveSettings();
}
} else {
settings = {
enabled: false,
how_often: 30,
disable_shelf: false
};
}
setup();
});
// if(!!localStorage["settings"]) {
// settings = JSON.parse(localStorage["settings"]);
// console.log(settings);
// if(typeof settings.disable_shelf === "undefined") settings.disable_shelf = false;
// }else{
// settings = {
// enabled: false,
// how_often: 15,
// disable_shelf: false
// };
// }
} catch (err) {
console.log(err);
}
};
setup = function () {
var enabled = document.getElementById("enabled");
var howOften = document.getElementById("how_often");
var disableShelf = document.getElementById("disable_shelf");
if (settings.enabled) {
enabled.checked = true;
}
if (settings.how_often) {
howOften.value = settings.how_often;
}
if (settings.disable_shelf) {
disableShelf.checked = true;
}
disableShelf.addEventListener("change", function (e) {
disableShelfRequest();
});
enabled.addEventListener("change", function (e) {
settings.enabled = !!e.target.checked;
saveSettings();
notify();
});
howOften.addEventListener("change", function (e) {
settings.how_often = e.target.value;
saveSettings();
notify();
});
};
function disableShelfRequest() {
// chrome.permissions.request({
// permissions: ['downloads', 'downloads.shelf']
// }, function(granted) {
// // The callback argument will be true if the user granted the permissions.
// if (granted) {
var disableShelf = document.getElementById("disable_shelf");
settings.disable_shelf = !!disableShelf.checked;
chrome.downloads.setShelfEnabled(!settings.disable_shelf);
saveSettings();
notify();
// console.log("yes, have it", granted);
// } else {
// var disableShelf = document.getElementById("disable_shelf");
// settings.disable_shelf = false;
// disableShelf.checked = false;
// saveSettings();
// notify("We don't have the permission to change the download shelf, sorry!");
// console.log("no, don't have it", granted);
// }
// });
}
function saveSettings() {
chrome.storage.sync.set(settings, function (s) {
console.log("saved");
});
}
function notify(msg) {
if (!msg) {
msg = "Changes saved";
}
var message = document.getElementById("message");
message.innerHTML = msg;
message.style.opacity = 1;
if (!!messageTimeout) clearTimeout(messageTimeout);
messageTimeout = setTimeout(hideNotify, 3000);
}
function hideNotify() {
var message = document.getElementById("message");
message.style.opacity = 0.01;
}