-
Notifications
You must be signed in to change notification settings - Fork 4
/
content.js
221 lines (193 loc) · 7.32 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
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
console.log("[alert-me-google-meet] loaded");
try {
(() => {
// Show status on start
chrome.storage.sync.set({
details: {
type: "log",
options: {
status: "danger",
message: "Not on call",
},
},
});
////////////////////////////////////////////////////////////////////////////
// State Variables
////////////////////////////////////////////////////////////////////////////
// Checks if control bar is present
// https://i.imgur.com/tqkyuqG.png
let ON_CALL = false;
let IS_SUBTITLE_ON = false;
////////////////////////////////////////////////////////////////////////////
// Getting Alert Words
////////////////////////////////////////////////////////////////////////////
let ALERT_WORDS = [];
// Gets pre-saved words upon start
chrome.storage.sync.get(["alertWords"], (data) => {
if (data.alertWords) {
const alertWords = data.alertWords;
const lwrAlertWords = alertWords.map((str) => str.toLowerCase());
ALERT_WORDS = lwrAlertWords;
}
});
// Listens to further changes in sync storage
chrome.storage.onChanged.addListener((changes, namespace) => {
if (changes.alertWords) {
const alertWords = changes.alertWords.newValue;
const lwrAlertWords = alertWords.map((str) => str.toLowerCase());
ALERT_WORDS = lwrAlertWords;
}
});
////////////////////////////////////////////////////////////////////////////
// Global Observer
////////////////////////////////////////////////////////////////////////////
const docObserver = new MutationObserver(() => {
if (document.body.querySelector("div[jscontroller='kAPMuc']")) {
ON_CALL = true;
// Remove observer
docObserver.disconnect();
chrome.storage.sync.set({
details: {
type: "log",
options: {
status: "success",
message: "You're in a call, add your keywords",
},
},
});
callStarts();
}
});
docObserver.observe(document.body, { childList: true });
// -------------------------------------------------------------------------
// Invoke after call starts
// -------------------------------------------------------------------------
const callStarts = () => {
const subtitleDiv = document.querySelector("div[jscontroller='TEjq6e']");
// To notify the first time
IS_SUBTITLE_ON = subtitleDiv.style.display === "none" ? false : true;
if (IS_SUBTITLE_ON) whenSubtitleOn();
else whenSubtitleOff();
const subtitleObserver = new MutationObserver(() => {
IS_SUBTITLE_ON = subtitleDiv.style.display === "none" ? false : true;
if (IS_SUBTITLE_ON) whenSubtitleOn();
else whenSubtitleOff();
});
subtitleObserver.observe(subtitleDiv, {
attributes: true,
attributeOldValue: true,
attributeFilter: ["style"],
});
};
// -------------------------------------------------------------------------
// Invoke when subs are off
// -------------------------------------------------------------------------
const whenSubtitleOff = () => {
chrome.storage.sync.set({
details: {
type: "log",
options: {
status: "warning",
message: "Turn on your captions",
},
},
});
};
// -------------------------------------------------------------------------
// Invoke when subs are on (MAIN FUNCTIONALITY)
// -------------------------------------------------------------------------
const whenSubtitleOn = () => {
chrome.storage.sync.set({
details: {
type: "log",
options: {
status: "success",
message: "You're all set, add your keywords and relax",
},
},
});
// DOM element containing all subtitles
const subtitleDiv = document.querySelector("div[jscontroller='TEjq6e']");
const subtitleObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
const newNodes = mutation.addedNodes;
newNodes.forEach((node) => {
if (node.classList && node.classList.contains("CNusmb")) {
const speaker = node?.parentNode?.parentNode?.parentNode?.parentNode?.querySelector(
".zs7s8d.jxFHg"
)?.textContent;
const photo = node?.parentNode?.parentNode?.parentNode?.parentNode
?.querySelector(".KpxDtd.r6DyN")
?.getAttribute("src");
const speech = node.textContent;
// Get image base64 from url
// ref: https://stackoverflow.com/a/20285053/11674552
const toDataURL = (url) =>
fetch(url)
.then((response) => response.blob())
.then(
(blob) =>
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(blob);
})
);
// -------------------------------------------------------------------------
// Check for matches
// -------------------------------------------------------------------------
let lwrSpeech = speech.toLowerCase();
for (let i = 0; i < ALERT_WORDS.length; i++) {
if (lwrSpeech.indexOf(ALERT_WORDS[i]) !== -1) {
toDataURL(photo).then((base64Link) => {
chrome.storage.sync.set({
details: {
type: "log",
options: {
status: "info",
message:
"You were notified. Check your notifications",
},
},
});
// Send notif alert to background.js
chrome.runtime.sendMessage("", {
type: "notification",
options: {
title: "Alert — from Google Meet",
message: `${speaker} just said ${speech}`,
iconUrl: base64Link,
type: "basic",
},
});
});
// Reduces the number of mutations
// (silents the observer for 5 seconds)
subtitleObserver.disconnect();
setTimeout(() => {
subtitleObserver.observe(subtitleDiv, {
childList: true,
subtree: true,
attributes: false,
characterData: false,
});
}, 3000);
}
}
}
});
});
});
// Start observing subtitle div
subtitleObserver.observe(subtitleDiv, {
childList: true,
subtree: true,
attributes: false,
characterData: false,
});
};
})();
} catch (e) {
console.error("init error", e);
}