This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (129 loc) · 5.82 KB
/
index.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
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
(function() {
'use strict';
try {
const MIN_SCALE = 0.1,
MAX_SCALE = 1,
TALKING_TEP_SIZE = 0.002,
SILENT_STEP_SIZE = 0.002,
CHECK_TALKING_INTERVAL = 100,
RELOAD_PARTICIPANTS_INTERVAL = 3000,
GOOGLE_MEET_NO_SOUND_ICON_CLASS = 'gjg47c'; // Base state class for active mic, likely to break
let participants = [],
currentScales = {},
checkTalkersInterval,
updateParticipantsInterval,
active = false;
function _addToggleButton() {
try {
const moreOptionsIcon = document.evaluate('//*[text()="more_vert"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (!moreOptionsIcon) {
console.warn('[Google Meet Shrink] Options button not found, retrying…');
return false;
}
console.log('[Google Meet Shrink] Found options menu button. Adding shrinker option');
moreOptionsIcon.closest('button').addEventListener('click', () => _onOptionsMenuOpen());
return true;
} catch(e) {
console.error('[Google Meet Shrink] Error while adding menu handler', e);
return false;
}
}
function _onOptionsMenuOpen(tries = 0) {
try {
const menuList = document.querySelector('body > div > ul');
if (!menuList) {
if (tries < 30) {
setTimeout(() => _onOptionsMenuOpen(++tries), 100);
} else {
console.warn('[Google Meet Shrink] Could not find options menu in time. Stopped trying.');
}
return;
}
const separator = menuList.querySelector('[role="separator"]').cloneNode(true),
toggleButton = document.evaluate('//li/*[contains(translate(text(), "F", "f"), "full screen")]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.closest('li').cloneNode(true);
toggleButton.lastChild.innerText = 'Toggle Shrink';
toggleButton.removeAttribute('jsaction');
toggleButton.addEventListener('click', _onToggleListener);
menuList.appendChild(separator);
menuList.appendChild(toggleButton);
} catch(e) {
console.error('[Google Meet Shrink] Error while adding menu option', e);
}
}
function _onToggleListener() {
if (active) {
_stop();
} else {
_init();
}
}
function _isTalking(participant) {
return getComputedStyle(participant.voiceEl).display !== 'none' && !participant.voiceEl.classList.contains(GOOGLE_MEET_NO_SOUND_ICON_CLASS); // Mic icon visible, and not in base state
}
function _isPerson(participant) {
return !(participant.el.querySelector('[data-self-name="You"]')?.innerText.includes('Presentation') ?? false);
}
function _getParticipantInfo(participantEl) {
return {
id: participantEl.getAttribute('data-requested-participant-id'),
el: participantEl,
voiceEl: participantEl.querySelector(':scope > div + div > div:first-child > div:first-child')
};
}
function _getParticipants() {
participants = [...document.querySelectorAll('[data-requested-participant-id]')].map(_getParticipantInfo);
}
function _checkTalkers() {
participants.forEach((participant) => {
let newScale;
if (!_isPerson(participant)) {
newScale = 1;
} else if (_isTalking(participant)) {
newScale = Math.max(MIN_SCALE, currentScales[participant.id] - TALKING_TEP_SIZE);
} else {
newScale = Math.min(MAX_SCALE, currentScales[participant.id] + SILENT_STEP_SIZE);
}
if (newScale !== currentScales[participant.id]) {
currentScales[participant.id] = newScale;
participant.el.style.transform = `scale(${newScale})`;
}
});
}
function _setupParticipants() {
_getParticipants();
participants.forEach((participant) => {
if(typeof currentScales[participant.id] === 'undefined') {
currentScales[participant.id] = 1;
}
participant.el.style.transition = `transform ${CHECK_TALKING_INTERVAL}ms linear`;
});
}
function _clearScales() {
currentScales = {};
document.querySelectorAll('[data-requested-participant-id]').forEach((el) => {
el.style.transform = '';
});
}
function _init() {
_setupParticipants();
updateParticipantsInterval = setInterval(_setupParticipants, RELOAD_PARTICIPANTS_INTERVAL);
checkTalkersInterval = setInterval(_checkTalkers, CHECK_TALKING_INTERVAL);
active = true;
}
function _stop() {
clearInterval(updateParticipantsInterval);
clearInterval(checkTalkersInterval);
_clearScales();
active = false;
}
const menuInterval = setInterval(() => {
_setupParticipants();
if (participants.length > 0 && _addToggleButton()) {
clearInterval(menuInterval);
}
}, 1000);
console.log('[Google Meet Shrink] Loaded!');
} catch(e) {
console.error('[Google Meet Shrink] General exception', e);
}
})();