-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
65 lines (56 loc) · 2.44 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
var namespace = typeof chrome !== 'undefined' ? chrome : browser;
var selectedTheme;
function toggleCheckboxes(checked) {
var checkboxes = document.querySelectorAll('input[type="checkbox"]');
checkboxes.forEach(checkbox => checkbox.checked = checked);
}
document.getElementById('checkAll').addEventListener('click', () => toggleCheckboxes(true));
document.getElementById('uncheckAll').addEventListener('click', () => toggleCheckboxes(false));
document.getElementById('optionsForm').addEventListener('submit', function(e) {
e.preventDefault();
var options = Array.from(document.querySelectorAll('input[type="checkbox"]'))
.reduce((options, checkbox) => {
options[checkbox.id] = checkbox.checked;
return options;
}, {});
options.theme = selectedTheme || document.getElementById('themeSelect').value;
namespace.storage.sync.set(options).then(function() {
document.getElementById('save-message').textContent = "YOUR SETTINGS HAVE BEEN SAVED!";
saveThemeColors(options.theme);
setTimeout(function() {
window.location.href = 'popup.html';
}, 1250);
}).catch(console.log);
});
document.querySelectorAll('.dropdown-item').forEach(item => {
item.addEventListener('click', function() {
selectedTheme = this.dataset.value;
applyThemeColors(selectedTheme);
document.querySelector('.dropdown-button').innerText = selectedTheme;
});
});
window.onload = function() {
namespace.storage.sync.get().then(function(options) {
Object.keys(options).forEach(key => {
var element = document.getElementById(key);
if (element && element.type === 'checkbox') {
element.checked = options[key];
}
});
if (options.theme) {
var dropdownItems = document.querySelectorAll('.dropdown-item');
dropdownItems.forEach(item => {
if (item.dataset.value === options.theme) {
item.classList.add('selected');
document.querySelector('.dropdown-button').innerText = options.theme;
} else {
item.classList.remove('selected');
}
});
selectedTheme = options.theme;
applyThemeColors(selectedTheme);
} else {
document.querySelector('.dropdown-button').innerText = "Select theme";
}
}).catch(console.log);
};