-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.js
452 lines (363 loc) · 16.5 KB
/
custom.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
document.addEventListener('DOMContentLoaded', function() {
// Clear local storage when the page is loaded
localStorage.clear();
let recognition; // Recognition object
let mode = 'Idle'; // Mode of the editor
let editMode = false;
let editBox = -1;
let replaceMode = false;
let currentIndex = 0;
// Function to read text aloud using text-to-speech
function speakText(text) {
const speechSynthesis = window.speechSynthesis;
const utterance = new SpeechSynthesisUtterance(text);
speechSynthesis.speak(utterance);
}
async function startRecording(index) {
const response = await fetch('/starttrans', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({})
});
if (!response.ok) {
throw new Error('Failed to start transcription');
}
const reader = response.body.getReader();
let transcription = '';
while (true) {
const { value, done } = await reader.read();
const text = new TextDecoder().decode(value);
if (text.includes('stop over finish')) {
console.log('Transcription stopped by user');
const fullTranscription = document.getElementById(`ans-${index}`).textContent.trim();
speakText(fullTranscription);
break;
}
const cleanedText = text.replace('select question', '').trim();
transcription += cleanedText + ' ';
document.getElementById(`ans-${index}`).textContent += transcription;
transcription = "";
if (done) {
mode = "Idle";
document.getElementById(`status-ind`).innerHTML = "Status: " + mode;
console.log('Transcription stream ended');
break;
}
}
}
function stopRecognition() {
recognition.stop();
}
function listenForSpeech() {
recognition = new window.webkitSpeechRecognition();
recognition.continuous = true;
recognition.onresult = function(event) {
const transcript = event.results[event.results.length - 1][0].transcript.toLowerCase();
if (transcript.includes('select question')) {
var questionNumber = transcript.split('select question ')[1];
if (questionNumber == 'one.') {
questionNumber = 1;
}
const index = parseInt(questionNumber, 10) - 1;
if (index >= 0 && index < questions.length) {
const ansBox = document.getElementById(`question-${index}`);
mode = 'Answering Question ' + (index + 1);
document.getElementById(`status-ind`).innerHTML = "Status: " + mode;
ansBox.scrollIntoView();
startRecording(index);
}
}
if (transcript.includes("edit question")) {
currentIndex = 0;
var questionNumber = transcript.split('edit question ')[1];
if (questionNumber == 'one.') {
questionNumber = 1;
}
const index = parseInt(questionNumber, 10) - 1;
const contentDiv = document.getElementById(`ans-${index}`);
mode = 'Editing Question ' + (index + 1);
document.getElementById(`status-ind`).innerHTML = "Status: " + mode;
if (contentDiv) {
// Set focus on the contenteditable div
contentDiv.focus();
// Collapse the selection to the start of the contenteditable div
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(contentDiv);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
// Set the edit mode and edit box index
editMode = true;
editBox = index;
} else {
console.error(`Contenteditable div with id 'ans-${index}' not found`);
}
}
if (editMode) {
if (transcript.includes("delete all")) {
const textarea = document.getElementById(`ans-${editBox}`);
textarea.innerHTML = "";
saveChangesAndExitEdit();
}
if (transcript.includes("select this sentence")) {
selectSentence();
}
if (transcript.includes("add text")) {
addText();
}
if (transcript.includes("delete sentence")) {
deleteSentence();
}
if (transcript.includes("highlight text")) {
highlight();
}
if (transcript.includes("save and exit")) {
saveChangesAndExitEdit();
}
if (transcript.includes("move cursor next")) {
moveCursorNext();
}
if (transcript.includes("move cursor back")) {
moveCursorBack();
}
if (transcript.includes("stop over finish")) {
stopRecognition();
console.log("Recording stopped.");
}
}
}
recognition.onend = function() {
if (editMode) {
recognition.start();
}
}
recognition.start();
}
function selectSentence() {
const div = document.getElementById(`ans-${editBox}`);
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(div);
const startIndex = div.textContent.lastIndexOf('.', selection.anchorOffset) + 1;
const endIndex = div.textContent.indexOf('.', selection.focusOffset) + 1;
range.setStart(div.firstChild, startIndex);
range.setEnd(div.firstChild, endIndex);
selection.removeAllRanges();
selection.addRange(range);
div.focus();
}
async function addText() {
const div = document.getElementById(`ans-${editBox}`);
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const cursorPosition = range.startOffset;
const response = await fetch('/starttrans', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({})
});
if (!response.ok) {
throw new Error('Failed to start transcription');
}
const reader = response.body.getReader();
let transcription = '';
while (true) {
const { value, done } = await reader.read();
const text = new TextDecoder().decode(value);
if (text.includes('stop over finish')) {
break;
}
const cleanedText = text.replace('select question', '').trim();
transcription += cleanedText + ' ';
// Insert the recognized text at the cursor position
const textNode = document.createTextNode(transcription);
range.deleteContents();
range.insertNode(textNode);
// Move the cursor to the end of the inserted text
range.setStartAfter(textNode);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
transcription = "";
if (done) {
console.log('Transcription stream ended');
mode = "Idle";
document.getElementById(`status-ind`).innerHTML = "Status: " + mode;
break;
}
}
}
// Function to set cursor position within a contenteditable div
function setCaretPosition(element, position) {
const range = document.createRange();
const sel = window.getSelection();
range.setStart(element.firstChild, position);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
element.focus();
}
function deleteSentence() {
const div = document.getElementById(`ans-${editBox}`);
const selection = window.getSelection();
const range = selection.getRangeAt(0);
range.deleteContents();
}
function saveChangesAndExitEdit() {
const div = document.getElementById(`ans-${editBox}`);
const editedResponse = div.textContent;
localStorage.setItem(`response-${editBox}`, editedResponse);
console.log("Changes saved.");
editMode = false;
editBox = -1;
// Update the mode indicator text to "You can record now"
const modeIndicator = document.getElementById(`mode-indicator-${editBox}`);
if (modeIndicator) {
modeIndicator.textContent = "You can record now";
} else {
console.error(`Mode indicator with id 'mode-indicator-${editBox}' not found`);
}
}
function highlight() {
const div = document.getElementById(`ans-${editBox}`);
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const selectedText = range.toString();
const newNode = document.createElement('strong');
newNode.textContent = selectedText;
range.deleteContents();
range.insertNode(newNode);
}
function moveCursorNext() {
const div = document.getElementById(`ans-${editBox}`);
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const nextPeriodIndex = div.textContent.indexOf('.', range.endOffset);
if (nextPeriodIndex !== -1) {
const nextSentenceStart = nextPeriodIndex + 1;
range.setStart(div.firstChild, nextSentenceStart);
range.setEnd(div.firstChild, nextSentenceStart);
selection.removeAllRanges();
selection.addRange(range);
div.focus();
} else {
console.log("No next sentence found.");
}
}
function moveCursorBack() {
const div = document.getElementById(`ans-${editBox}`);
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const prevPeriodIndex = div.textContent.lastIndexOf('.', range.startOffset);
if (prevPeriodIndex !== -1) {
const prevSentenceStart = div.textContent.lastIndexOf('.', prevPeriodIndex - 1) + 1;
range.setStart(div.firstChild, prevSentenceStart);
range.setEnd(div.firstChild, prevSentenceStart);
selection.removeAllRanges();
selection.addRange(range);
div.focus();
} else {
console.log("No previous sentence found.");
}
}
const questionsDiv = document.getElementById('questions');
const urlParams = new URLSearchParams(window.location.search);
const questions = JSON.parse(urlParams.get('file'));
if (questions == null) {
window.location.href = "login.html";
}
questions.forEach((question, index) => {
const questionElement = document.createElement('div');
questionElement.classList.add('question');
const questionHeading = document.createElement('h2');
questionHeading.classList.add('question-heading');
questionHeading.id = `question-${index}`;
questionHeading.textContent = 'Question ' + (index + 1);
questionElement.appendChild(questionHeading);
const questionPara = document.createElement('p');
questionPara.classList.add('question-text');
questionPara.textContent = question.trim();
questionElement.appendChild(questionPara);
// Create a new container for buttons
const buttonContainer = document.createElement('div');
buttonContainer.classList.add('button-container');
questionElement.appendChild(buttonContainer);
const speakButton = document.createElement('button');
speakButton.classList.add('speak-button');
speakButton.innerHTML = '<i class="fas fa-volume-up"></i> Speak';
buttonContainer.appendChild(speakButton);
const readButton = document.createElement('button');
readButton.classList.add('read-button');
readButton.textContent = 'Read Text';
buttonContainer.appendChild(readButton);
// Add event listeners for both buttons
speakButton.addEventListener('click', function() {
const questionText = questionPara.textContent;
speakText(questionText);
});
readButton.addEventListener('click', function() {
const textToRead = document.getElementById(`ans-${index}`).textContent.trim();
speakText(textToRead);
});
const recordingText = document.createElement('div');
recordingText.id = `ans-${index}`;
recordingText.contentEditable = true; // Make the div editable
recordingText.classList.add('recording-text');
recordingText.placeholder = 'Recording notes...';
// Retrieve the previous response from localStorage, if available
const previousResponse = localStorage.getItem(`response-${index}`);
console.log(previousResponse);
if (previousResponse) {
recordingText.innerHTML = previousResponse; // Use innerHTML to set the content
}
questionElement.appendChild(recordingText);
questionsDiv.appendChild(questionElement);
});
function downloadPDF() {
const doc = new jsPDF();
const questionsDiv = document.getElementById('questions');
const questions = Array.from(questionsDiv.getElementsByClassName('question'));
let yPos = 10; // Initialize y position for text
questions.forEach((question, index) => {
const questionHeading = question.querySelector('.question-heading').textContent;
const questionText = question.querySelector('.question-text').textContent;
const answerText = question.querySelector('.recording-text').textContent;
var username = document.cookie.split('=')[1];
doc.setFontSize(12);
if (index === 0) {
const university = 'Amrita Vishwa Vidyapeetham';
const department = 'Department of Computer Science Semester Exam';
const pageWidth = doc.internal.pageSize.width;
const universityTextWidth = doc.getStringUnitWidth(university) * doc.internal.getFontSize() / doc.internal.scaleFactor;
const departmentTextWidth = doc.getStringUnitWidth(department) * doc.internal.getFontSize() / doc.internal.scaleFactor;
const universityTextPosition = (pageWidth - universityTextWidth) / 2;
const departmentTextPosition = (pageWidth - departmentTextWidth) / 2;
doc.text(university, universityTextPosition, yPos);
yPos += 10;
doc.text(department, departmentTextPosition, yPos);
yPos += 10;
doc.text(username, 10, yPos);
yPos += 10;
}
const content = `Question ${index + 1}\n${questionText}\n\nAnswer:\n${answerText}\n\n`;
doc.text(content, 10, yPos);
yPos += 80;
if (yPos >= doc.internal.pageSize.height) {
doc.addPage(); // Add a new page
yPos = 10; // Reset y position for the new page
}
});
doc.save('questions_and_answers.pdf');
}
const submitButton = document.createElement('button');
submitButton.classList.add('submit-button');
submitButton.textContent = 'Submit';
document.body.appendChild(submitButton);
submitButton.addEventListener('click', downloadPDF);
listenForSpeech();
});