-
Notifications
You must be signed in to change notification settings - Fork 11
/
options.js
121 lines (111 loc) · 5.06 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
function restoreOptions() {
chrome.storage.sync.get({
defaultUser: '0',
pollInterval: '3',
quietHours: '',
distractionFreeMinutes: '30',
useSnoozeColor: true,
useDesktopNotifications: true,
showPageMenu: true,
focusExistingInboxTab: false,
openInEmptyTab: false
}, function (items) {
document.getElementById('defaultUser').value = items.defaultUser;
document.getElementById('pollInterval').value = items.pollInterval || 3;
document.getElementById('quietHours').value = items.quietHours;
document.getElementById('distractionFreeMinutes').value = items.distractionFreeMinutes || 30;
document.getElementById('useSnoozeColor').checked = !!items.useSnoozeColor;
document.getElementById('showPageMenu').checked = !!items.showPageMenu;
document.getElementById('useDesktopNotifications').checked = !!items.useDesktopNotifications;
document.getElementById('openInEmptyTab').checked = !!items.openInEmptyTab;
chrome.permissions.contains({ permissions: ['tabs'] }, function (result) {
document.getElementById('focusExistingInboxTabError').textContent = items.focusExistingInboxTab && !result ?
'Error: "tabs" permission not granted. Re-check this option to try again.' :
'';
document.getElementById('focusExistingInboxTab').checked = !!items.focusExistingInboxTab;
});
});
}
function saveOptions(e) {
e.preventDefault();
// Normalize
var defaultUser = Math.max(0, parseInt(document.getElementById('defaultUser').value) || 0);
var pollInterval = Math.max(0, Math.min(3600, parseInt(document.getElementById('pollInterval').value) || 3));
var quietHours = document.getElementById('quietHours').value;
var distractionFreeMinutes = Math.max(1, Math.min(1440, parseInt(document.getElementById('distractionFreeMinutes').value) || 30));
var useSnoozeColor = document.getElementById('useSnoozeColor').checked;
var useDesktopNotifications = document.getElementById('useDesktopNotifications').checked;
var showPageMenu = document.getElementById('showPageMenu').checked;
var focusExistingInboxTab = document.getElementById('focusExistingInboxTab').checked;
var openInEmptyTab = document.getElementById('openInEmptyTab').checked;
chrome.storage.sync.set({
defaultUser: defaultUser,
pollInterval: pollInterval,
quietHours: quietHours,
distractionFreeMinutes: distractionFreeMinutes,
useSnoozeColor: useSnoozeColor,
useDesktopNotifications: useDesktopNotifications,
showPageMenu: showPageMenu,
focusExistingInboxTab: focusExistingInboxTab,
openInEmptyTab: openInEmptyTab
}, function () {
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function () {
status.textContent = '';
}, 3000);
});
// Show normalized values
restoreOptions();
// Remove unused permissions
if (!focusExistingInboxTab) {
chrome.permissions.remove({ permissions: ['tabs'] });
}
return false;
}
function defaultOptions() {
document.getElementById('defaultUser').value = 0;
document.getElementById('pollInterval').value = 3;
document.getElementById('quietHours').value = '';
document.getElementById('distractionFreeMinutes').value = 30;
document.getElementById('useSnoozeColor').checked = true;
document.getElementById('useDesktopNotifications').checked = true;
document.getElementById('showPageMenu').checked = true;
document.getElementById('focusExistingInboxTab').checked = false;
document.getElementById('openInEmptyTab').checked = false;
}
function main() {
document.getElementById('version').innerHTML = chrome.runtime.getManifest().version;
// Ensure permissions
if (!chrome || !chrome.storage || !chrome.storage.sync) {
var status = document.getElementById('status');
status.style.cssText = 'color:red;';
status.textContent = 'Error: Could not load settings. Please upgrade Chrome.';
return;
}
document.addEventListener('DOMContentLoaded', restoreOptions);
document.getElementById('defaults').addEventListener('click', defaultOptions);
document.getElementById('optionsForm').addEventListener('submit', saveOptions);
document.getElementById('exampleQuietHours').addEventListener('click', function (e) {
e.preventDefault();
document.getElementById('quietHours').value = '9,10,11,12,13,14,15,16,17,18';
return false;
});
document.getElementById('focusExistingInboxTab').addEventListener('click', function (e) {
if (document.getElementById('focusExistingInboxTab').checked) {
chrome.permissions.contains({ permissions: ['tabs'] }, function (result) {
if (!result) {
alert('This option requires the following permission in order to locate Inbox tabs:\n\n \u2022 "tabs"\n\nIf prompted, select "Allow" to enable this feature.');
chrome.permissions.request({ permissions: ['tabs'] }, function (granted) {
if (!granted) {
document.getElementById('focusExistingInboxTab').checked = false;
}
});
}
});
}
return false;
});
}
main();