-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
407 lines (343 loc) · 14.5 KB
/
script.js
File metadata and controls
407 lines (343 loc) · 14.5 KB
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
// Check if it's the first visit
if (!localStorage.getItem('linguaBridgeVisited')) {
document.getElementById('welcomeModal').style.display = 'flex';
localStorage.setItem('linguaBridgeVisited', 'true');
}
// Close welcome modal
document.getElementById('closeWelcome').addEventListener('click', () => {
document.getElementById('welcomeModal').style.display = 'none';
});
document.getElementById('gotItBtn').addEventListener('click', () => {
document.getElementById('welcomeModal').style.display = 'none';
});
const startBtn = document.getElementById('startBtn');
const btnText = document.getElementById('btnText');
const micIcon = document.getElementById('micIcon');
const statusIndicator = document.getElementById('statusIndicator');
const originalTextDiv = document.getElementById('originalTextDisplay');
const translatedTextDiv = document.getElementById('translatedText');
const searchResultsDiv = document.getElementById('searchResults');
const sourceLangSelect = document.getElementById('sourceLang');
const targetLangSelect = document.getElementById('targetLang');
const clearBtn = document.getElementById('clearBtn');
const talkbackToggle = document.getElementById('talkbackToggle');
const editToggle = document.getElementById('editToggle');
const originalTextDisplay = document.getElementById('originalTextDisplay');
const originalTextEdit = document.getElementById('originalTextEdit');
const refreshTranslation = document.getElementById('refreshTranslation');
let recognition;
let isRecognizing = false;
let talkbackEnabled = false;
let speechSynthesis = window.speechSynthesis;
// Initialize speech synthesis voices
let voices = [];
speechSynthesis.onvoiceschanged = () => {
voices = speechSynthesis.getVoices();
};
editToggle.addEventListener('click', () => {
if (originalTextEdit.classList.contains('hidden')) {
// Switch to edit mode
originalTextDisplay.classList.add('hidden');
originalTextEdit.classList.remove('hidden');
originalTextEdit.value = originalTextDisplay.textContent.trim();
editToggle.textContent = 'Save Changes';
originalTextEdit.focus();
} else {
// Save changes and switch back to display mode
originalTextDisplay.classList.remove('hidden');
originalTextEdit.classList.add('hidden');
originalTextDisplay.textContent = originalTextEdit.value || '<span class="text-blue-200 italic">Start speaking to see your words appear here...</span>';
editToggle.textContent = 'Edit Text';
// Auto-refresh translation if there's content
if (originalTextEdit.value.trim()) {
refreshTranslationFromEdit();
}
}
});
// Refresh translation from edited text
refreshTranslation.addEventListener('click', refreshTranslationFromEdit);
async function refreshTranslationFromEdit() {
const text = originalTextEdit.value.trim() || originalTextDisplay.textContent.trim();
if (!text || text.includes('Start speaking')) return;
translatedTextDiv.innerHTML = '<span class="text-yellow-200 italic">Translating...</span>';
const translated = await translateText(text, targetLangSelect.value);
if (translated) {
translatedTextDiv.textContent = translated;
if (talkbackEnabled) {
speakText(translated, targetLangSelect.value);
}
}
// Refresh Wikipedia results
fetchWikiResults(text);
}
// Allow Enter key to save in edit mode
originalTextEdit.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
editToggle.click();
}
});
// Clear all content
clearBtn.addEventListener('click', () => {
originalTextDiv.innerHTML = '<span class="text-blue-200 italic">Start speaking to see your words appear here...</span>';
translatedTextDiv.innerHTML = '<span class="text-yellow-200 italic">Translation will appear here...</span>';
searchResultsDiv.innerHTML = '<div class="text-green-200 italic text-center py-8">Speak to discover related Wikipedia articles...</div>';
});
// Toggle talkback feature
talkbackToggle.addEventListener('change', (e) => {
talkbackEnabled = e.target.checked;
});
// Enhanced Google Translate API function with error handling
async function translateText(text, targetLang) {
if (!text || text.trim().length === 0) return '';
const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=${targetLang}&dt=t&q=${encodeURIComponent(text)}`;
try {
const res = await fetch(url);
if (!res.ok) throw new Error(`HTTP error! status: ${res.status}`);
const data = await res.json();
if (!data || !data[0]) return text; // Return original if translation fails
return data[0].map(item => item[0]).join('');
} catch (error) {
console.error('Translation error:', error);
return text; // Return original text if translation fails
}
}
// Speak text using Web Speech API
function speakText(text, lang) {
if (!talkbackEnabled || !text) return;
const utterance = new SpeechSynthesisUtterance(text);
// Find a voice that matches the target language
const targetVoice = voices.find(voice =>
voice.lang.startsWith(lang) ||
voice.lang.startsWith(lang.split('-')[0])
);
if (targetVoice) {
utterance.voice = targetVoice;
utterance.lang = targetVoice.lang;
} else {
utterance.lang = lang;
}
speechSynthesis.speak(utterance);
}
// Enhanced Wikipedia search with better error handling and formatting
async function fetchWikiResults(query) {
if (!query || query.trim().length < 2) {
searchResultsDiv.innerHTML = `
<div class="text-center py-8">
<div class="text-green-200 italic">Continue speaking to discover related articles...</div>
</div>
`;
return;
}
// Show loading state
searchResultsDiv.innerHTML = `
<div class="text-center py-8">
<div class="inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-white"></div>
<div class="text-white mt-2">Searching knowledge base...</div>
</div>
`;
const apiUrl = `https://en.wikipedia.org/w/api.php?action=opensearch&format=json&origin=*&search=${encodeURIComponent(query)}&limit=5`;
try {
const res = await fetch(apiUrl);
if (!res.ok) throw new Error(`HTTP error! status: ${res.status}`);
const data = await res.json();
if (!data[1] || data[1].length === 0) {
searchResultsDiv.innerHTML = `
<div class="text-center py-8">
<div class="text-yellow-200">No articles found for "<span class="font-semibold">${query}</span>"</div>
<div class="text-blue-200 text-sm mt-2">Try speaking about different topics</div>
</div>
`;
return;
}
searchResultsDiv.innerHTML = '';
data[1].forEach((title, i) => {
const description = data[2][i] || 'No description available.';
const link = data[3][i];
const card = document.createElement('a');
card.href = link;
card.target = "_blank";
card.rel = "noopener noreferrer";
card.className = "block p-4 rounded-xl bg-white/10 hover:bg-white/20 transition-all duration-300 transform hover:scale-[1.02] border border-white/20 fade-in";
card.innerHTML = `
<h4 class="text-white font-semibold text-base mb-2 flex items-center">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-2 text-green-300" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14" />
</svg>
${title}
</h4>
<p class="text-blue-100 text-sm leading-relaxed">${description}</p>
`;
searchResultsDiv.appendChild(card);
});
} catch (error) {
console.error('Wikipedia API error:', error);
searchResultsDiv.innerHTML = `
<div class="text-center py-8">
<div class="text-red-300">Unable to fetch search results</div>
<div class="text-blue-200 text-sm mt-2">Please check your internet connection</div>
</div>
`;
}
}
// Initialize enhanced speech recognition
function initRecognition() {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if (!SpeechRecognition) {
alert('Sorry, your browser does not support Speech Recognition. Please use Chrome, Edge, or Safari.');
return null;
}
const recog = new SpeechRecognition();
recog.lang = sourceLangSelect.value;
recog.interimResults = true;
recog.continuous = true;
recog.maxAlternatives = 1;
return recog;
}
// Enhanced button click handler
startBtn.addEventListener('click', () => {
if (isRecognizing) {
stopRecognition();
return;
}
startRecognition();
});
let finalTranscript = '';
let isProcessing = false;
let mobileDevice = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
function startRecognition() {
recognition = initRecognition();
if (!recognition) return;
// Reset displays
originalTextDiv.innerHTML = '<span class="text-blue-200 italic">Listening...</span>';
translatedTextDiv.innerHTML = '<span class="text-yellow-200 italic">Translation will appear here...</span>';
searchResultsDiv.innerHTML = `
<div class="text-center py-8">
<div class="text-green-200 italic">Speak to discover related articles...</div>
</div>
`;
recognition.lang = sourceLangSelect.value;
finalTranscript = '';
isProcessing = false;
// Mobile-specific adjustments
if (mobileDevice) {
recognition.continuous = false; // Disable continuous mode on mobile
recognition.interimResults = false; // Disable interim results on mobile
}
try {
recognition.start();
updateUIForRecording(true);
} catch (error) {
console.error('Recognition start error:', error);
}
recognition.onresult = async (event) => {
if (isProcessing) return;
isProcessing = true;
let interimTranscript = '';
let currentTranscript = '';
for (let i = event.resultIndex; i < event.results.length; i++) {
if (event.results[i].isFinal) {
currentTranscript += event.results[i][0].transcript;
} else {
interimTranscript += event.results[i][0].transcript;
}
}
// Mobile-specific processing
if (mobileDevice) {
// Only use final results on mobile
if (currentTranscript) {
finalTranscript += currentTranscript + ' ';
originalTextDiv.textContent = finalTranscript;
const translated = await translateText(finalTranscript, targetLangSelect.value);
if (translated) {
translatedTextDiv.textContent = translated;
if (talkbackEnabled) {
speakText(translated, targetLangSelect.value);
}
}
if (finalTranscript.trim().length > 2) {
fetchWikiResults(finalTranscript.trim());
}
}
}
// Desktop processing (original logic)
else {
const combinedTranscript = (finalTranscript + interimTranscript).trim();
if (combinedTranscript) {
originalTextDiv.textContent = combinedTranscript;
const translated = await translateText(combinedTranscript, targetLangSelect.value);
if (translated) {
translatedTextDiv.textContent = translated;
if (talkbackEnabled) {
speakText(translated, targetLangSelect.value);
}
}
if (finalTranscript.trim().length > 2) {
fetchWikiResults(finalTranscript.trim());
}
}
}
isProcessing = false;
// Restart recognition on mobile after processing
if (mobileDevice && isRecognizing) {
setTimeout(() => {
if (isRecognizing) {
try {
recognition.start();
} catch (e) {
console.log('Restart error:', e);
}
}
}, 100);
}
};
// Keep existing error and end handlers
recognition.onerror = (event) => {
// ... (keep your existing error handler)
};
recognition.onend = () => {
if (!mobileDevice || !isRecognizing) {
updateUIForRecording(false);
}
};
}
function stopRecognition() {
isRecognizing = false;
if (recognition) {
recognition.stop();
}
updateUIForRecording(false);
}
function updateUIForRecording(recording) {
isRecognizing = recording;
if (recording) {
btnText.textContent = 'Stop Speaking';
startBtn.className = 'w-full bg-gradient-to-r from-red-500 to-pink-600 hover:from-red-600 hover:to-pink-700 text-white font-semibold py-4 px-6 rounded-xl transition duration-300 transform hover:scale-105 shadow-lg flex items-center justify-center space-x-2 pulse-animation';
micIcon.innerHTML = `<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 10a1 1 0 011-1h4a1 1 0 011 1v4a1 1 0 01-1 1h-4a1 1 0 01-1-1v-4z" />`;
statusIndicator.classList.remove('opacity-0');
statusIndicator.classList.add('opacity-100');
} else {
btnText.textContent = 'Start Speaking';
startBtn.className = 'w-full bg-gradient-to-r from-green-500 to-blue-600 hover:from-green-600 hover:to-blue-700 text-white font-semibold py-4 px-6 rounded-xl transition duration-300 transform hover:scale-105 shadow-lg flex items-center justify-center space-x-2';
micIcon.innerHTML = `<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11a7 7 0 01-7 7m0 0a7 7 0 01-7-7m7 7v4m0 0H8m4 0h4m-4-8a3 3 0 01-3-3V5a3 3 0 116 0v6a3 3 0 01-3 3z" />`;
statusIndicator.classList.remove('opacity-100');
statusIndicator.classList.add('opacity-0');
}
}
// Update recognition language when source language changes
sourceLangSelect.addEventListener('change', () => {
if (recognition && isRecognizing) {
recognition.lang = sourceLangSelect.value;
}
});
// Add keyboard shortcuts
document.addEventListener('keydown', (event) => {
if (event.code === 'Space' && event.ctrlKey) {
event.preventDefault();
startBtn.click();
}
});
// Add responsive behavior for mobile
if (window.innerWidth < 768) {
document.body.addEventListener('touchstart', () => {}, { passive: true });
}