-
Notifications
You must be signed in to change notification settings - Fork 13
/
lib.ts
168 lines (137 loc) · 4.18 KB
/
lib.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
declare var browser: typeof chrome;
// For Firefox support
if (typeof browser !== 'undefined')
// @ts-ignore
chrome = browser;
const windowLoaded: Promise<void> = new Promise(resolve => window.onload = () => resolve());
const enum ToggleName {
MIC = 'disableMic',
CAM = 'disableCam',
}
type Settings = Record<ToggleName, boolean>;
const defaultSettings: Settings = {
[ToggleName.MIC]: false,
[ToggleName.CAM]: true,
};
let settings: Settings;
const settingsLoaded: Promise<Settings> = (chrome.storage.sync.get() as Promise<Settings>)
.then(storageSettings => settings = ({...defaultSettings, ...storageSettings}));
const enum ToggleDirection {
LEFT = 'left',
RIGHT = 'right',
}
const enum ToggleEmoji {
MIC = '🔇',
CAM = '📷',
}
type ToggleOptions = {
label: string;
name: ToggleName;
index: number;
direction: ToggleDirection;
emoji: ToggleEmoji;
};
type ToggleOnChangeCallback = (callback: HTMLInputElement) => void;
type ExcludeMatchingProperties<TObject, TValue> = Pick<TObject, {
[K in keyof TObject]-?: TObject[K] extends TValue ? never : K;
}[keyof TObject]>;
type ExcludeMethods<TObject> = ExcludeMatchingProperties<TObject, Function>;
type CreateElementOptions<El extends HTMLElement> = Partial<ExcludeMethods<El>>;
class Toggle {
label: string;
name: ToggleName;
index: number;
direction: ToggleDirection;
emoji: ToggleEmoji;
autoDisable: boolean;
#buttonEl: HTMLDivElement | HTMLButtonElement;
#labelEl: HTMLLabelElement;
#checkboxEl: HTMLInputElement;
#spanEl: HTMLSpanElement;
#onChange: ToggleOnChangeCallback;
constructor(options: ToggleOptions) {
Object.assign(this, options, {
autoDisable: settings[options.name],
});
}
get buttonOnDOM(): boolean {
return this.#buttonEl?.isConnected ?? false;
}
get buttonEl(): HTMLDivElement | HTMLButtonElement {
return this.buttonOnDOM ?
this.#buttonEl :
this.#buttonEl = document.querySelectorAll<HTMLDivElement | HTMLButtonElement>(`[role="button"][data-is-muted]`)[this.index];
}
createElement<
TagName extends keyof HTMLElementTagNameMap,
El extends HTMLElementTagNameMap[TagName] = HTMLElementTagNameMap[TagName],
>(
tagName: TagName,
options: CreateElementOptions<El> = {},
) {
return Object.assign(document.createElement(tagName), options);
}
get labelEl(): HTMLLabelElement {
return this.#labelEl ??= (() => {
const labelEl: HTMLLabelElement = this.createElement('label');
labelEl.append(this.checkboxEl, this.spanEl);
return labelEl;
})();
}
set labelStyle(style: Partial<CSSStyleDeclaration>) {
Object.assign(this.labelEl.style, style);
}
get checkboxEl(): HTMLInputElement {
return this.#checkboxEl ??= (() => {
const checkboxEl: HTMLInputElement = this.createElement('input', {
type: 'checkbox',
checked: this.autoDisable,
});
checkboxEl.addEventListener('change', (): void => {
this.#onChange?.(this.checkboxEl);
settings[this.name] = checkboxEl.checked;
chrome.storage.sync.set(settings);
});
return checkboxEl;
})();
}
set checkboxStyle(style: Partial<CSSStyleDeclaration>) {
Object.assign(this.checkboxEl.style, style);
}
get spanEl(): HTMLSpanElement {
return this.#spanEl ??= this.createElement('span', {
textContent: `Auto Disable ${this.label}`,
});
}
onChange(callback: ToggleOnChangeCallback): void {
this.#onChange = callback;
}
get disabled(): boolean {
return this.#buttonEl?.dataset.isMuted === 'true';
}
disable(): void {
if (!this.disabled)
this.#buttonEl?.click();
}
set checked(checked: boolean) {
this.checkboxEl.checked = checked;
if (checked)
this.disable();
}
}
const createToggles = () => <Record<ToggleName, Toggle>>Object.fromEntries(([
{
label: 'Microphone',
name: ToggleName.MIC,
index: 0,
direction: ToggleDirection.RIGHT,
emoji: ToggleEmoji.MIC,
},
{
label: 'Camera',
name: ToggleName.CAM,
index: 1,
direction: ToggleDirection.LEFT,
emoji: ToggleEmoji.CAM,
},
] satisfies ToggleOptions[]).map(options => [options.name, new Toggle(options)]));