-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.js
106 lines (85 loc) · 2.94 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
// Saves options to chrome.storage.
function saveOptions() {
const newOptions = extractOptionsFromInputs();
console.log("Options saving: ");
console.log(newOptions);
localStorage.setItem("hideMeaning", newOptions.hideMeaning);
localStorage.setItem("vocabularyList", newOptions.vocabularyList);
console.log('Options saved.');
displayStatus("Options saved.");
chrome.storage.sync.set({ period: newOptions.period, activated: newOptions.activated }, () => {
// Send message to refresh alarm
chrome.runtime.sendMessage({ message: "newOptionsSaved" }, function (response) {
console.log(response.message);
});
});
}
// Get alarm options from Chrome Storage API
function getAlarmOptions(callback) {
console.log("Getting options...");
chrome.storage.sync.get({
period: 5,
activated: false
}, function (options) {
callback(options);
});
}
// Restores settings using the stored data in chrome.storage.
function restoreOptions() {
const $period = periodInput();
const $activated = activatedInput();
const $hideMeaning = hideMeaningInput();
const $vocabularyList = vocabularyListSelect();
console.log("Options restoring...");
const options = {
hideMeaning: localStorage.getItem("hideMeaning") === "true",
vocabularyList: localStorage.getItem("vocabularyList") || "globish"
};
getAlarmOptions((optionsFromStorage) => {
options.period = optionsFromStorage.period;
options.activated = optionsFromStorage.activated;
$period.val(options.period);
$activated.prop('checked', options.activated);
$hideMeaning.prop('checked', options.hideMeaning);
$vocabularyList
.find('option[value="' + options.vocabularyList + '"]')
.attr('selected', true);
console.log("Options restored: ");
console.log(options);
});
}
// Helper methods
function periodInput() {
return $('input[name="period"]');
}
function activatedInput() {
return $('input[name="activated"]');
}
function hideMeaningInput() {
return $('input[name="hideMeaning"]');
}
function vocabularyListSelect() {
return $('select[name="vocabularyList"]');
}
function extractOptionsFromInputs() {
const $period = periodInput();
const $activated = activatedInput();
const $hideMeaning = hideMeaningInput();
const $vocabularyList = vocabularyListSelect();
return {
period: parseInt($period.val()),
activated: $activated.prop('checked'),
hideMeaning: $hideMeaning.prop('checked'),
vocabularyList: $vocabularyList.val()
};
}
function displayStatus(statusText) {
const $statusLabel = $('#statusLabel');
$statusLabel.text(statusText);
setTimeout(function () {
$statusLabel.text('');
}, 750);
}
// Set event bindings
document.addEventListener('DOMContentLoaded', restoreOptions);
$('input , select').change(saveOptions);