forked from burkybang/Google-Meet-Auto-Disable-Mic-Cam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
109 lines (89 loc) · 2.69 KB
/
content.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
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
/**
* @typedef Settings
* @property {boolean} disableMic
* @property {boolean} disableCam
*/
/** @type {Settings} */
const defaultSettings = {
disableMic: false,
disableCam: true,
};
/**
* @typedef ButtonProps
* @property {string} label
* @property {string} key
* @property {string} storageName
* @property {'left'|'right'} direction
* @property {HTMLDivElement} element
*/
/** @type {ButtonProps[]} */
const buttons = [
{
label: 'Microphone',
storageName: 'disableMic',
key: 'd',
direction: 'right',
element: null,
},
{
label: 'Camera',
storageName: 'disableCam',
key: 'e',
direction: 'left',
element: null,
},
];
/** @type {Promise<Settings>} */
const settingsLoaded = new Promise(resolve => chrome.storage.sync.get(resolve));
/** @type {Promise<void>} */
const windowLoaded = new Promise(resolve => window.onload = () => resolve());
/** @type {Promise<void>} */
const buttonsLoaded = new Promise(async resolve => {
await windowLoaded;
/** @type {MutationObserver} */
const observer = new MutationObserver(() => {
if (!buttons.every(button =>
button.element = document.body.querySelector(`div[role="button"][aria-label$=" + ${button.key})" i][data-is-muted]`),
)) return;
observer.disconnect();
resolve();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
Promise.all([
settingsLoaded,
buttonsLoaded,
]).then(async ([/** Settings */ settings = {}]) => {
settings = {...defaultSettings, ...settings};
buttons.forEach(({label, storageName, direction, element}) => {
/** @type {boolean} */
const autoDisable = settings[storageName] === true;
/** @return {void} */
const disable = () => {
if (element.dataset.isMuted === 'false')
element.click();
};
/** @type {HTMLDivElement} */
const tempDivEl = document.createElement('div');
tempDivEl.innerHTML = `
<label style="color:white; position:absolute; bottom:0; ${direction}:100px; z-index:1; cursor:pointer; white-space:nowrap;">
<input type="checkbox" ${autoDisable ? 'checked' : ''} style="cursor:pointer; margin:0 4px 0 0; position:relative; top:1px;"/>
<span>Auto Disable ${label}</span>
</label>
`;
/** @type {HTMLInputElement} */
const checkboxEl = tempDivEl.querySelector('input');
checkboxEl.addEventListener('change', ({currentTarget}) => {
if (currentTarget.checked)
disable();
settings[storageName] = currentTarget.checked;
chrome.storage.sync.set(settings);
});
element.parentElement.append(tempDivEl.children[0]);
if (!autoDisable) return;
disable();
});
});