-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.js
53 lines (50 loc) · 1.85 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
function fetchNotes(cb) {
chrome.storage.local.get(['epoch', 'notes', 'backgroundColor', 'foregroundColor'], (localData) => {
chrome.storage.sync.get(['epoch', 'notes', 'backgroundColor', 'foregroundColor'], (syncData) => {
return cb((localData.epoch || 0) > (syncData.epoch || 0) ? localData : syncData);
});
});
}
// Saves options to chrome.storage
function save_options() {
fetchNotes(({ epoch } = data) => {
var backgroundColor = document.getElementById('backgroundColor').value;
var foregroundColor = document.getElementById('foregroundColor').value;
chrome.storage.local.set({
"epoch": epoch + 1,
"backgroundColor": backgroundColor,
"foregroundColor": foregroundColor,
}, function () {
chrome.storage.sync.set({
"epoch": epoch + 1,
"backgroundColor": backgroundColor,
"foregroundColor": foregroundColor,
}, function () {
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function () {
status.textContent = '';
}, 750);
});
});
});
}
function default_val(val, def) {
if (val == "[object Object]") return def;
else return val;
}
// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
function restore_options() {
chrome.storage.sync.get({
"backgroundColor": backgroundColor,
"foregroundColor": foregroundColor,
}, function (items) {
document.getElementById('backgroundColor').value = default_val(items.backgroundColor, "#35363A");
document.getElementById('foregroundColor').value = default_val(items.foregroundColor, "#DADCE0");
});
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click',
save_options);