-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
74 lines (67 loc) · 2.54 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
/*
** The javascript behind options.html, a popup where users can define
** the core settings.
** Sends a message to background.js to update the settings.
** Handles the behavior of the two lower buttons.
*/
window.onload = function () {
/** Add an event listener for each checkbox. */
document.querySelectorAll('.switch').forEach(item => {
item.firstElementChild.addEventListener('click', save);
})
document.getElementById("websiteOptions").addEventListener("click", showWebsiteOptions);
document.getElementById("showData").addEventListener("click", showData);
}
/** Return a string form of today's date based on the user's OS timezone.
** Ex: January 7, 2020 === x200107. */
function today() {
let date = new Date();
let m = date.getMonth() + 1;
m = m.toString();
if (m.length == 1) {
m = "0" + m;
}
let d = date.getDate().toString();
if (d.length == 1) {
d = "0" + d;
}
let y = date.getFullYear().toString();
y = y.substring(2);
return "x" + y + m + d;
}
/* Update the options html to have the previously saved options. */
chrome.storage.sync.get(["hideRecc", "hideSidebar", "showNumber", "showNotifications", "minutesAlertInterval", "silent", today()],
(data) => {
document.getElementById("hideRecc").checked = data.hideRecc;
document.getElementById("hideSidebar").checked = data.hideSidebar;
document.getElementById("showNumber").checked = data.showNumber;
document.getElementById("showNotifications").checked = data.showNotifications;
document.getElementById("silent").checked = data.silent;
document.getElementById("timeSpent").textContent = data[today()] ? (Math.floor(data[today()] / 60) + "+") : "No";
})
/* Update chrome storage to have the new defined options. */
function save() {
var hideRecc = document.getElementById("hideRecc").checked;
var hideSidebar = document.getElementById("hideSidebar").checked;
var showNumber = document.getElementById("showNumber").checked;
var showNotifications = document.getElementById("showNotifications").checked;
var silent = document.getElementById("silent").checked;
chrome.storage.sync.set(
{
"hideRecc": hideRecc,
"hideSidebar": hideSidebar,
"showNumber": showNumber,
"showNotifications": showNotifications,
"silent": silent
});
var message = {
txt: "savedOptions"
}
chrome.runtime.sendMessage("", message);
}
function showWebsiteOptions() {
chrome.tabs.create({url: chrome.extension.getURL("websites.html")})
}
function showData() {
chrome.tabs.create({url: chrome.extension.getURL("analytics.html")})
}