-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeet_script.js
258 lines (248 loc) · 7.32 KB
/
meet_script.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// End call listener is in endCall-script.js
const toast = document.createElement("div");
toast.classList.add("error-toast");
if (location.pathname !== "/") {
document.body.insertAdjacentElement("beforeend", toast);
}
// There should be a better way to wait for the page
// to load completely but lemme use this for now
console.log(location.toString());
let meetingName;
setTimeout(() => {
chrome.storage.sync.get(null, (s) => {
findJoinButton();
let executeScript = false;
// let meetName;
for (const subjectName in s) {
console.log(s[subjectName]);
if (window.location.href.includes(s[subjectName].meetUrl)) {
executeScript = true;
meetingName = subjectName;
break;
}
}
if (!executeScript) return;
executeJoin(s.autoJoin, s.showQuickMessage);
displayMeetingName();
});
}, 5000);
function showNameOnJoinScreen() {
console.log("showing name");
document.querySelectorAll('div[jsaction^="mouseover"]').forEach(div => {
if (/join\b/.test(div.innerText)) {
div.insertAdjacentHTML("afterend", `<div style="color:teal">${meetingName}</div>`);
// div.style.color = "red";
}
})
}
function executeJoin(autoJoin, showQuickMessage) {
// Join the meet
try {
// console.log(location.pathname);
if (location.pathname !== "/") {
prepareToJoin();
if (autoJoin) joinMeeting();
addButton(showQuickMessage);
}
} catch (e) {
setTimeout(() => {
try {
prepareToJoin();
if (autoJoin) joinMeeting();
addButton(showQuickMessage);
} catch (e) {
console.log(e);
_showError("Could not turn off microphone and camera and join");
setTimeout(() => {
try {
prepareToJoin();
} catch (e) {
console.log(e);
}
}, 1500);
}
}, 2000);
}
}
function _showError(message) {
toast.innerText = message;
toast.style.display = "block";
setTimeout(() => {
toast.style.display = "none";
}, 8000);
}
/**
* Turn off microphone and camera
*/
function prepareToJoin() {
showNameOnJoinScreen();
try {
document.querySelector('div[aria-label^="Turn off microphone ("]').click();
document.querySelector('div[aria-label^="Turn off camera ("]').click();
} catch (e) {
(
document.querySelector('div[aria-label="Turn off camera (CTRL + E)"]') ||
document.querySelector('div[aria-label="Turn off camera (ctrl + e)"]')
).click();
(
document.querySelector(
'div[aria-label="Turn off microphone (CTRL + D)"]'
) ||
document.querySelector('div[aria-label="Turn off microphone (ctrl + d)"]')
).click();
}
}
let joinButton;
function findJoinButton() {
document.querySelectorAll("div[role='button']").forEach((button) => {
// console.log(button);
const span = button.querySelector(" span span");
// console.log(span.innerHTML);
const regex = /(ask to join)|(join now)/gi;
if (regex.test(span.innerHTML)) {
joinButton = button;
return button;
}
});
}
function joinMeeting() {
joinButton.click();
// document.querySelectorAll("div[role='button']").forEach((button) => {
// // console.log(button);
// const span = button.querySelector(" span span");
// // console.log(span.innerHTML);
// const regex = /(ask to join)|(join now)/gi;
// if (regex.test(span.innerHTML)) {
// joinButton = button;
// button.click();
// button.style.background = "red";
// }
// });
}
function addButton(showQuickMessage) {
if (showQuickMessage)
fetch(chrome.runtime.getURL("/google-meet.html"))
.then((r) => r.text())
.then((html) => {
try {
// console.log(html);
document.body
// .querySelector('textarea[aria-label="Send a message to everyone"]')
.insertAdjacentHTML("beforeend", html);
document
.getElementById("send-message-google-meet")
.addEventListener("click", sendIntroMessage);
// addListeners();
// not using innerHTML as it would break js event listeners of the page}
} catch (e) {
console.log(e);
// setTimeout(() => {
// document
// .querySelector('textarea[aria-label="Send a message to everyone"]')
// .insertAdjacentHTML("beforeend", html);
// }, 2000);
}
});
}
function sendIntroMessage() {
const self = this;
chrome.storage.sync.get("showQuickMessage", (s) => {
if (s.showQuickMessage)
chrome.storage.sync.get("quickMessage", (m) => {
const message = m["quickMessage"] ?? "Good day";
function sendIt() {
document.querySelector(
'textarea[aria-label="Send a message to everyone"]'
).value = message;
const sendButton = document.querySelector(
'button[aria-label="Send a message to everyone"]'
);
sendButton.disabled = false;
sendButton.click();
}
try {
sendIt();
} catch (e) {
try {
document.querySelector('[aria-label="Chat with everyone"]').click();
self.style.display = "none";
setTimeout(() => {
sendIt();
}, 800);
} catch (e) {
console.log(e);
_showError("Could not send message. Did you join the meeting?");
}
}
});
});
}
function endMeeting() {
if (document.querySelector('button[aria-label="Leave call"]'))
document.querySelector('button[aria-label="Leave call"]').click();
else {
chrome.runtime.sendMessage({ closeTab: true });
}
try {
document.getElementById("send-message-google-meet").style.display = "none";
} catch (e) {
console.log(e);
}
}
// Replace link code with meeting name
function displayMeetingName() {
//joinButton?
if (!nameWasInserted)
document.addEventListener("click", () => {
findDivAndInsertName(meetingName);
});
// findJoinButton().addEventListener("click", (e) => {
// console.log("event listener");
// setTimeout(() => {
// if (!findDivAndInsertName()) {
// setTimeout(() => {
// findDivAndInsertName();
// }, 2000);
// }
// }, 5000);
// })
}
let nameWasInserted = false;
/**
* Finds the code link div(bottom left) and replaces
* the link with provided subject name
* @param {String} n Meeting name
* @returns whether it was successful
*/
function findDivAndInsertName(n) {
if (nameWasInserted) return;
document.querySelectorAll('div[jsaction^="mouseover"]').forEach(div => {
if (/\w{3}-\w{4}-\w{3}/.test(div.innerText)) {
div.innerText = n;
nameWasInserted = true;
return true;
}
})
return false;
}
//draggable button
// window.onload = addListeners;
// window.onload = addListeners;
function addListeners() {
document
.getElementById("send-message-google-meet")
.addEventListener("mousedown", mouseDown, false);
window.addEventListener("mouseup", mouseUp, false);
}
function mouseUp() {
window.removeEventListener("mousemove", divMove, true);
}
function mouseDown(e) {
window.addEventListener("mousemove", divMove, true);
}
function divMove(e) {
var div = document.getElementById("send-message-google-meet");
div.style.position = "fixed";
div.style.top = e.clientY + "px";
div.style.left = e.clientX + "px";
}