-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.js
62 lines (53 loc) · 1.33 KB
/
settings.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
let defaultSettings = {
fontSize: 30,
opacity: 0.8,
leftHeight: 40,
leftXOffset: 25,
leftYOffset: 0,
leftStyle: 'A-E',
bottomWidth: 45,
bottomXOffset: 0,
bottomYOffset: 20,
bottomStyle: '1-9',
};
function populateForm(data) {
Object.keys(defaultSettings).forEach((id) => {
let el = document.getElementById(id);
if (el) {
el.value = data[id];
}
});
}
function notifyTab(settings) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs
.sendMessage(tabs[0].id, { type: 'settings updated' })
.catch(() => {});
});
}
async function save() {
let newSettings = {};
Object.keys(defaultSettings).forEach((id) => {
let val = document.getElementById(id).value;
newSettings[id] = val === '' ? defaultSettings[id] : val;
});
chrome.storage.local.set({ settings: newSettings }, function () {
console.log('Saved');
});
notifyTab(newSettings);
}
function reset() {
populateForm(defaultSettings);
save();
}
document.addEventListener('DOMContentLoaded', function () {
chrome.storage.local.get(['settings'], function (result) {
let settings = { ...defaultSettings, ...(result?.settings ?? {}) };
populateForm(settings);
let ctrls = document.querySelectorAll('input, select');
for (let el of ctrls) {
el.onchange = save;
}
document.getElementById('reset').onclick = reset;
});
});