This repository has been archived by the owner on Nov 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.ts
88 lines (70 loc) · 2.33 KB
/
options.ts
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
// This module is loaded as part of "the view". It instantiates the option
// checkboxes, and handles any changes by sending them to the Service Worker
// (which will then save the changes).
import { EventName, SyncStorageKey } from '../../types/enums';
import { getOptionDefaults } from '../constants';
interface Option {
key: SyncStorageKey;
label: string;
}
export class Options {
protected static readonly OPTIONS: Option[] = [
{
key: SyncStorageKey.OPTION_SYNC_UNRANKED_WINGMAN,
label: 'Sync unranked Wingman matches',
},
{
key: SyncStorageKey.OPTION_SYNC_RANKED_WINGMAN,
label: 'Sync ranked Wingman matches',
},
{
key: SyncStorageKey.OPTION_SYNC_UNRANKED_5V5,
label: 'Sync unranked 5v5 matches',
},
{
key: SyncStorageKey.OPTION_SYNC_ON_INTERVAL,
label: 'Sync matches every 15 minutes (when your browser is open)',
},
{
key: SyncStorageKey.OPTION_SYNC_ON_VISIT_LEETIFY,
label: 'Sync matches when visiting Leetify',
},
{
key: SyncStorageKey.OPTION_SYNC_ON_VISIT_GCPD,
label: 'Sync matches when visiting Steam GCPD',
},
];
protected readonly checkboxContainer = document.querySelector('div#checkbox-container') as HTMLDivElement | null;
public constructor() {
this.initCheckboxes();
}
protected async initCheckboxes(): Promise<void> {
if (!this.checkboxContainer) return;
const values = await this.getValues();
for (const option of Options.OPTIONS) {
const checkbox = document.createElement('input');
checkbox.id = `option-${option.key}`;
checkbox.type = 'checkbox';
checkbox.checked = !!values[option.key];
// eslint-disable-next-line @typescript-eslint/no-loop-func
checkbox.addEventListener('change', async () => {
await chrome.storage.sync.set({ [option.key]: checkbox.checked });
await chrome.runtime.sendMessage({
event: EventName.OPTION_UPDATED,
data: { key: option.key, value: checkbox.checked },
});
});
const label = document.createElement('label');
label.innerText = option.label;
label.setAttribute('for', checkbox.id);
this.checkboxContainer.appendChild(checkbox);
this.checkboxContainer.appendChild(label);
}
}
protected async getValues(): Promise<{ [key in SyncStorageKey]?: boolean }> {
return {
...getOptionDefaults(),
...await chrome.storage.sync.get(Options.OPTIONS.map(({ key }) => key)),
};
}
}