-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
49 lines (45 loc) · 1.49 KB
/
popup.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
// Define a very basic set of colors
const colors = {
white: [255, 255, 255],
silver: [192, 192, 192],
gray: [128, 128, 128],
black: [0, 0, 0],
red: [255, 0, 0],
maroon: [128, 0, 0],
yellow: [255, 255, 0],
olive: [128, 128, 0],
lime: [0, 255, 0],
green: [0, 128, 0],
cyan: [0, 255, 255],
teal: [0, 128, 128],
blue: [0, 0, 255],
navy: [0, 0, 128],
fuchsia: [255, 0, 255],
purple: [128, 0, 128],
};
// Save selected colors to local extension storage
function save_options() {
chrome.storage.local.set({
selectedColors: [...document.getElementById('colors').selectedOptions]
.map(o => { return { name: o.value, rgb: colors[o.value] } })
});
}
// Add colors to the select. Load selected colors and mark them as selected
async function restore_options() {
const colorsDOM = document.getElementById('colors');
const selectedColors = await chrome.storage.local.get("selectedColors")
.then(r =>
Array.isArray(r.selectedColors) ?
r.selectedColors.map(c => c.name) :
[]
);
for (const color in colors) {
const option = document.createElement("option");
option.innerHTML = color;
option.value = color;
option.selected = selectedColors.includes(color);
colorsDOM.appendChild(option);
}
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click', save_options);