-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
340 lines (315 loc) · 10 KB
/
app.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
var submit_button = document.getElementById("submit");
var start_button = document.getElementById("start");
var stop_button = document.getElementById("stop");
var resume_button = document.getElementById("resume");
var ss_button = document.getElementById("ss");
var clear_button = document.getElementById("clear");
var is_google_meet = false;
var spanClassName = "CNusmb";
var div = document.getElementById("span_text");
var idx = 1;
var catch_meet = false;
var google_meet_transcript_data = "";
var take_snapshot = false;
var turned_on_captions_flag = false;
function get_data(turned_on_captions_flag) {
var turnOnMeetCaptionsClass = "I98jWb";
console.log("TOCF:",turned_on_captions_flag);
if(turned_on_captions_flag == true){
var turnOnMeetCaptionsDiv = document.getElementsByClassName(turnOnMeetCaptionsClass);
if(turnOnMeetCaptionsDiv.length == 0){
// alert("Please Turn On your Captions for Recording");
}else{
turnOnMeetCaptionsDiv[0].click();
}
}
//console.log('came here');
var idx = 1;
var spans = document.getElementsByClassName("CNusmb");
// console.log(spans);
var take_snapshot = false;
var global_text = "";
for (let i = 0; i < spans.length; i++) {
console.log(spans[i].innerText, spans[i].classList.contains("from-cs"));
if (!spans[i].classList.contains("from-cs")) {
spans[i].classList.add("from-cs");
var text = spans[i].innerText.toLowerCase();
var res = text.split(" ");
//console.log(res);
for (let i = 0; i < res.length; i++) {
if (res[i] == "take") {
if (
i != res.length - 1 &&
(res[i + 1] == "snapshot" || res[i + 1] == "snapshot.")
) {
console.log("detected take snapshot");
take_snapshot = true;
}
}
}
global_text += text;
global_text += " ";
// console.log(text);
// div.textContent += text;
}
}
return { text: global_text, flag: take_snapshot };
}
function start_fetching_meet() {
console.log("started");
// while(true){
if (catch_meet) {
//console.log('inside');
setTimeout(() => {
chrome.tabs.executeScript(
null,
{ code: "(" + get_data + ")("+turned_on_captions_flag+");" },
(results) => {
// console.log('FUN RES');
// console.log(results);
// div.innerText += " ";
// div.innerText += results;
turned_on_captions_flag = false;
google_meet_transcript_data += " ";
google_meet_transcript_data += results[0].text;
localStorage.setItem("meet-data", google_meet_transcript_data);
console.log(results[0].flag, results, "value of take snapshot bool");
if (results[0].flag) {
console.log("take snapshot!! :)");
takeScreenShot();
acknowledgeSSSuccess();
}
start_fetching_meet();
}
);
}, 5000);
} else return;
// }
}
function get_host_name() {
return window.location.host;
}
if(localStorage.getItem('meet-data-store')=="true"){
document.getElementById("subject_name").value = localStorage.getItem('meet-name');
document.getElementById("lecture_name").value = localStorage.getItem('meet-topic');
document.getElementById("num_lines").value = localStorage.getItem('meet-lines');
start_helper();
}
async function start_helper(){
if(localStorage.getItem('meet-data-store') == null || localStorage.getItem("meet-data-store")==false){
localStorage.setItem("images", JSON.stringify([]));
localStorage.setItem("meet-data", " ");
}
//keep it waiting
localStorage.setItem('meet-data-store',true);
const subject_name = document.getElementById("subject_name").value;
const lecture_name = document.getElementById("lecture_name").value;
const num_lines = document.getElementById("num_lines").value;
localStorage.setItem('meet-name',subject_name);
localStorage.setItem('meet-topic',lecture_name);
localStorage.setItem('meet-lines',num_lines);
await chrome.tabs.executeScript(
null,
{ code: "(" + get_host_name + ")();" },
(results) => {
console.log(results);
if (results[0] == "meet.google.com") {
is_google_meet = true;
console.log("It is a G Meet");
}
if (is_google_meet) {
catch_meet = true;
turned_on_captions_flag = true;
console.log("starting recording...");
submit_button.style.display = "inline-block";
stop_button.style.display = "inline-block";
start_button.style.display = "none";
start_fetching_meet();
} else toggleSpeechRecognition();
}
);
}
start_button.addEventListener("click", async function () {
start_helper();
});
submit_button.addEventListener("click", function (e) {
localStorage.setItem('meet-data-store',false);
e.preventDefault();
if (!is_google_meet) toggleSpeechRecognition();
else {
recognizing = false;
submit_button.style.display = "none";
stop_button.style.display = "none";
resume_button.style.display = "none";
start_button.style.display = "inline-block";
}
const req = new XMLHttpRequest();
const baseUrl = BASE_URL_VS + "/text/1";
const subject_name = document.getElementById("subject_name").value;
const lecture_name = document.getElementById("lecture_name").value;
const num_lines = document.getElementById("num_lines").value;
if (num_lines == null) num_lines = -1;
// const transcript = document.getElementById('transcript').value;
// const transcript_data = localStorage.getItem('current_session');
if (is_google_meet) {
catch_meet = false;
transcript_data = localStorage.getItem("google-meet-data");
}
const urlParams = {
subject_name: subject_name,
lecture_name: lecture_name,
transcript: localStorage.getItem("meet-data"),
user_id: JSON.parse(localStorage.getItem("user")).userId,
email:
(JSON.parse(localStorage.getItem("user")) &&
JSON.parse(localStorage.getItem("user")).email) ||
TEMP_EMAIL,
images: JSON.parse(localStorage.getItem("images")),
is_gmeet: is_google_meet,
num_lines: num_lines,
};
console.log(urlParams);
req.open("POST", baseUrl);
req.setRequestHeader("Content-Type", "application/json");
req.send(JSON.stringify(urlParams));
req.onreadystatechange = function () {
// Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
console.log("Got response 200!");
transcript_data = [];
localStorage.setItem("images", null);
}
};
is_google_meet = false;
});
stop_button.addEventListener("click", function (e) {
e.preventDefault();
if (!is_google_meet) toggleSpeechRecognition();
else {
recognizing = false;
catch_meet = false;
stop_button.style.display = "none";
resume_button.style.display = "inline-block";
}
});
resume_button.addEventListener("click", function (e) {
e.preventDefault();
if (!is_google_meet) toggleSpeechRecognition();
else {
recognizing = true;
catch_meet = true;
stop_button.style.display = "inline-block";
resume_button.style.display = "none";
console.log("resume");
}
start_fetching_meet();
});
clear_button.addEventListener('click',function(){
localStorage.setItem('meet-data-store',false);
})
var recognizing = false;
var recognition = new webkitSpeechRecognition();
var transcript_data = [];
var start_time;
var end_time;
recognition.continuous = true;
recognition.interimResults = true;
recognition.onstart = function () {
recognizing = true;
recordSession();
};
recognition.onerror = function (event) {
console.log(
"Recognition error: ",
event.message ? event.message : event.error
);
if (event.error == "not-allowed") {
window.open("chrome-extension://" + CHROME_EXTENSION_ID + "/perms.html");
}
if (
event.error == "no-speech" ||
event.error == "audio-capture" ||
event.error == "network" ||
event.error == "bad-grammar"
) {
refresh();
}
};
recognition.onend = function () {
recognizing = false;
};
recognition.onresult = function (event) {
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i][0].confidence > 0.1) {
if (event.results[i].isFinal) {
console.log(event.results);
var text = event.results[i][0].transcript;
addToTranscript("current_session", text);
var res = text.split(" ");
for (let i = 0; i < res.length; i++) {
if (res[i] == "take") {
if (i != res.length - 1 && (res[i + 1] == "snapshot" || res[i+1]=="snapshot.")) {
takeScreenShot();
acknowledgeSSSuccess();
}
}
}
}
}
}
};
function toggleSpeechRecognition(event) {
if (recognizing) {
recognition.stop();
submit_button.style.display = "none";
stop_button.style.display = "inline-block";
start_button.style.display = "inline-block";
return;
} else {
submit_button.style.display = "inline-block";
start_button.style.display = "none";
recognition.start();
}
}
function recordSession() {
createSession();
beginTime = new Date();
window.location.hash = currentSession.name;
}
function createSession() {
var id, name, startTime;
name = "new_session";
startTime = new Date();
currentSession = { id: id, name: name, startTime: startTime };
// saveToLocalStorage('current_session', currentSession);
}
function addToTranscript(sessionName, text) {
if (text) {
var endTime = new Date();
transcript_data.push({
startTime: beginTime,
endTime: endTime,
text: text,
});
// Reset the beginTime
beginTime = endTime;
console.log(transcript_data);
// saveToLocalStorage(sessionName, JSON.stringify(transcript));
}
}
function saveToLocalStorage(key, data) {
if (data && localStorage) {
if (typeof data === "object") {
localStorage.setItem(key, JSON.stringify(data));
} else {
localStorage.setItem(key, data);
}
}
}
function refresh(event) {
recognizing = false;
recognition.abort();
try {
recognition.start();
} catch (e) {}
}