-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
571 lines (493 loc) · 19.2 KB
/
Copy pathscript.js
File metadata and controls
571 lines (493 loc) · 19.2 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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// Get DOM elements
const textInput = document.getElementById('text-input');
const voiceSelect = document.getElementById('voice-select');
const rateSlider = document.getElementById('rate-slider');
const pitchSlider = document.getElementById('pitch-slider');
const volumeSlider = document.getElementById('volume-slider');
const rateValue = document.getElementById('rate-value');
const pitchValue = document.getElementById('pitch-value');
const volumeValue = document.getElementById('volume-value');
const charCount = document.getElementById('char-count');
const speakBtn = document.getElementById('speak-btn');
const pauseBtn = document.getElementById('pause-btn');
const resumeBtn = document.getElementById('resume-btn');
const stopBtn = document.getElementById('stop-btn');
const status = document.getElementById('status');
const textDisplay = document.getElementById('text-display');
const darkModeToggle = document.getElementById('dark-mode-toggle');
const moon = document.getElementById('moon');
const starsContainer = document.getElementById('stars');
const fileInput = document.getElementById('file-input');
// PDF.js worker (required for parsing PDFs in the browser)
if (typeof pdfjsLib !== 'undefined') {
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js';
}
// Initialize Speech Synthesis
const synth = window.speechSynthesis;
let currentUtterance = null;
let voices = [];
let words = [];
let currentWordIndex = -1;
let highlightInterval = null;
// Load voices when available
function loadVoices() {
voices = synth.getVoices();
// Clear existing options
voiceSelect.innerHTML = '';
// Add voices to select
voices.forEach((voice, index) => {
const option = document.createElement('option');
option.value = index;
option.textContent = `${voice.name} (${voice.lang})`;
voiceSelect.appendChild(option);
});
// Set default to first English voice if available
const defaultVoice = voices.find(voice => voice.lang.startsWith('en')) || voices[0];
if (defaultVoice) {
const defaultIndex = voices.indexOf(defaultVoice);
voiceSelect.value = defaultIndex;
}
}
// Load voices immediately if available
if (synth.onvoiceschanged !== undefined) {
synth.onvoiceschanged = loadVoices;
}
loadVoices();
// Update character count and text display
textInput.addEventListener('input', () => {
const count = textInput.value.length;
charCount.textContent = count.toLocaleString();
updateTextDisplay();
});
// PDF: extract text
async function extractTextFromPDF(file) {
if (typeof pdfjsLib === 'undefined') {
throw new Error('PDF library not loaded. Please refresh the page.');
}
const arrayBuffer = await file.arrayBuffer();
const pdf = await pdfjsLib.getDocument(arrayBuffer).promise;
const numPages = pdf.numPages;
const textParts = [];
for (let i = 1; i <= numPages; i++) {
const page = await pdf.getPage(i);
const content = await page.getTextContent();
const strings = content.items.map(item => item.str);
textParts.push(strings.join(' '));
}
return textParts.join('\n\n');
}
// TXT: read as plain text
function extractTextFromTxt(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result || '');
reader.onerror = () => reject(new Error('Could not read file'));
reader.readAsText(file);
});
}
// Word .docx: extract text (mammoth supports .docx; .doc has limited support)
async function extractTextFromWord(file) {
if (typeof mammoth === 'undefined') {
throw new Error('Word library not loaded. Please refresh the page.');
}
const arrayBuffer = await file.arrayBuffer();
const result = await mammoth.extractRawText({ arrayBuffer });
return result.value;
}
// Route file by type and extract text
async function extractTextFromFile(file) {
const type = file.type.toLowerCase();
const name = (file.name || '').toLowerCase();
if (type === 'application/pdf' || name.endsWith('.pdf')) {
return extractTextFromPDF(file);
}
if (type === 'text/plain' || name.endsWith('.txt')) {
return extractTextFromTxt(file);
}
if (
type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ||
type === 'application/msword' ||
name.endsWith('.docx') ||
name.endsWith('.doc')
) {
try {
return await extractTextFromWord(file);
} catch (err) {
if (name.endsWith('.doc') && !name.endsWith('.docx')) {
throw new Error('Legacy .doc is not supported. Save as .docx and try again.');
}
throw err;
}
}
throw new Error('Unsupported file type. Use PDF, Word (.doc/.docx), or TXT.');
}
fileInput.addEventListener('change', async (e) => {
const file = e.target.files?.[0];
if (!file) return;
status.textContent = 'Reading file...';
try {
const text = await extractTextFromFile(file);
textInput.value = text;
charCount.textContent = text.length.toLocaleString();
updateTextDisplay();
status.textContent = `Loaded ${file.name} (${text.length.toLocaleString()} characters)`;
} catch (err) {
status.textContent = 'Failed to read file: ' + (err.message || 'Unknown error');
}
fileInput.value = '';
});
// Function to create word-highlighted display
function updateTextDisplay() {
const text = textInput.value.trim();
if (!text) {
textDisplay.style.display = 'none';
return;
}
// Split text into words while preserving spaces and punctuation
words = text.match(/\S+|\s+/g) || [];
// Create HTML with word spans
const html = words.map((word, index) => {
// Skip pure whitespace words for highlighting
if (/^\s+$/.test(word)) {
return `<span class="word whitespace">${escapeHtml(word)}</span>`;
}
return `<span class="word" data-index="${index}">${escapeHtml(word)}</span>`;
}).join('');
textDisplay.innerHTML = html;
textDisplay.style.display = 'block';
}
// Escape HTML to prevent XSS
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Highlight word at index
function highlightWord(index) {
// Remove highlight from previous word
if (currentWordIndex >= 0) {
const prevWordEl = document.querySelector(`.word[data-index="${currentWordIndex}"]`);
if (prevWordEl && !prevWordEl.classList.contains('whitespace')) {
prevWordEl.classList.remove('highlighted');
prevWordEl.classList.add('spoken');
}
}
// Mark words before current as spoken (if not already)
for (let i = currentWordIndex + 1; i < index; i++) {
const wordEl = document.querySelector(`.word[data-index="${i}"]`);
if (wordEl && !wordEl.classList.contains('whitespace')) {
wordEl.classList.add('spoken');
}
}
// Highlight current word
const currentWordEl = document.querySelector(`.word[data-index="${index}"]`);
if (currentWordEl && !currentWordEl.classList.contains('whitespace')) {
currentWordEl.classList.remove('spoken');
currentWordEl.classList.add('highlighted');
// Scroll into view (only if word is not visible)
const rect = currentWordEl.getBoundingClientRect();
const displayRect = textDisplay.getBoundingClientRect();
if (rect.bottom > displayRect.bottom || rect.top < displayRect.top) {
currentWordEl.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}
currentWordIndex = index;
}
// Clear all highlights
function clearHighlights() {
document.querySelectorAll('.word').forEach(word => {
word.classList.remove('highlighted', 'spoken');
});
currentWordIndex = -1;
if (highlightInterval) {
clearInterval(highlightInterval);
highlightInterval = null;
}
}
// Update slider value displays
rateSlider.addEventListener('input', (e) => {
rateValue.textContent = parseFloat(e.target.value).toFixed(1);
});
pitchSlider.addEventListener('input', (e) => {
pitchValue.textContent = parseFloat(e.target.value).toFixed(1);
});
volumeSlider.addEventListener('input', (e) => {
volumeValue.textContent = parseFloat(e.target.value).toFixed(1);
});
// Speak function
function speak() {
const text = textInput.value.trim();
if (!text) {
status.textContent = 'Please enter some text to speak.';
status.classList.remove('active');
return;
}
// Cancel any ongoing speech
synth.cancel();
clearHighlights();
// Update text display
updateTextDisplay();
// Create new utterance
currentUtterance = new SpeechSynthesisUtterance(text);
// Set voice
const selectedVoiceIndex = parseInt(voiceSelect.value);
if (voices[selectedVoiceIndex]) {
currentUtterance.voice = voices[selectedVoiceIndex];
}
// Set properties
currentUtterance.rate = parseFloat(rateSlider.value);
currentUtterance.pitch = parseFloat(pitchSlider.value);
currentUtterance.volume = parseFloat(volumeSlider.value);
// Reset word tracking
currentWordIndex = -1;
const nonWhitespaceWords = words.filter((w, i) => {
const el = document.querySelector(`.word[data-index="${i}"]`);
return el && !el.classList.contains('whitespace');
});
// Calculate timing for word highlighting (fallback if onboundary not supported)
const rate = parseFloat(rateSlider.value);
const avgCharsPerSecond = 150 * rate; // Average speaking rate
let wordStartTime = 0;
// Event handlers
currentUtterance.onstart = () => {
status.textContent = 'Speaking...';
status.classList.add('active');
speakBtn.disabled = true;
pauseBtn.disabled = false;
resumeBtn.disabled = true;
stopBtn.disabled = false;
// Start word highlighting
const startTime = Date.now();
let boundaryEventFired = false;
let lastBoundaryTime = startTime;
// Try to use onboundary event (better accuracy)
currentUtterance.onboundary = (event) => {
if (event.name === 'word') {
boundaryEventFired = true;
lastBoundaryTime = Date.now();
// Clear timing interval if it exists (boundary events are more accurate)
if (highlightInterval) {
clearInterval(highlightInterval);
highlightInterval = null;
}
// Find the word index based on character position
const charIndex = event.charIndex;
let charCount = 0;
let foundIndex = -1;
for (let i = 0; i < words.length; i++) {
if (charCount <= charIndex && charIndex < charCount + words[i].length) {
foundIndex = i;
break;
}
charCount += words[i].length;
}
if (foundIndex >= 0) {
const wordEl = document.querySelector(`.word[data-index="${foundIndex}"]`);
if (wordEl && !wordEl.classList.contains('whitespace')) {
highlightWord(foundIndex);
}
}
}
};
// Fallback: use timing-based highlighting if onboundary doesn't work
// Check after 1 second if boundary events are firing
setTimeout(() => {
if (!boundaryEventFired && highlightInterval === null) {
// onboundary not supported, use timing-based approach
highlightInterval = setInterval(() => {
if (!synth.speaking || synth.paused) {
return;
}
const elapsed = (Date.now() - startTime) / 1000;
let charCount = 0;
let targetIndex = -1;
// Estimate which word should be highlighted based on elapsed time
// Skip whitespace-only words
for (let i = 0; i < words.length; i++) {
const word = words[i];
const wordLength = word.length;
// Skip whitespace words
if (/^\s+$/.test(word)) {
charCount += wordLength;
continue;
}
const wordStartTime = charCount / avgCharsPerSecond;
const wordEndTime = (charCount + wordLength) / avgCharsPerSecond;
if (elapsed >= wordStartTime && elapsed < wordEndTime) {
targetIndex = i;
break;
}
charCount += wordLength;
}
if (targetIndex >= 0 && targetIndex !== currentWordIndex) {
const wordEl = document.querySelector(`.word[data-index="${targetIndex}"]`);
if (wordEl && !wordEl.classList.contains('whitespace')) {
highlightWord(targetIndex);
}
}
}, 50); // Update every 50ms for smooth highlighting
}
}, 1000);
};
currentUtterance.onend = () => {
status.textContent = 'Finished speaking.';
status.classList.remove('active');
speakBtn.disabled = false;
pauseBtn.disabled = true;
resumeBtn.disabled = true;
stopBtn.disabled = true;
currentUtterance = null;
clearHighlights();
// Mark all words as spoken
document.querySelectorAll('.word').forEach(word => {
if (!word.classList.contains('whitespace')) {
word.classList.add('spoken');
}
});
};
currentUtterance.onerror = (event) => {
status.textContent = `Error: ${event.error}`;
status.classList.remove('active');
speakBtn.disabled = false;
pauseBtn.disabled = true;
resumeBtn.disabled = true;
stopBtn.disabled = true;
currentUtterance = null;
clearHighlights();
};
// Speak
synth.speak(currentUtterance);
}
// Pause function
function pause() {
if (synth.speaking && !synth.paused) {
synth.pause();
status.textContent = 'Paused.';
pauseBtn.disabled = true;
resumeBtn.disabled = false;
}
}
// Resume function
function resume() {
if (synth.speaking && synth.paused) {
synth.resume();
status.textContent = 'Speaking...';
pauseBtn.disabled = false;
resumeBtn.disabled = true;
}
}
// Stop function
function stop() {
synth.cancel();
status.textContent = 'Stopped.';
status.classList.remove('active');
speakBtn.disabled = false;
pauseBtn.disabled = true;
resumeBtn.disabled = true;
stopBtn.disabled = true;
currentUtterance = null;
clearHighlights();
}
// Button event listeners
speakBtn.addEventListener('click', speak);
pauseBtn.addEventListener('click', pause);
resumeBtn.addEventListener('click', resume);
stopBtn.addEventListener('click', stop);
// Keyboard shortcuts
textInput.addEventListener('keydown', (e) => {
// Ctrl/Cmd + Enter to speak
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
e.preventDefault();
speak();
}
// Escape to stop
if (e.key === 'Escape') {
stop();
}
});
// Initialize character count
charCount.textContent = '0';
// Initialize text display on page load if there's existing text
if (textInput.value.trim()) {
updateTextDisplay();
}
// Generate stars with random positions
function generateStars() {
const numStars = 50;
starsContainer.innerHTML = '';
for (let i = 0; i < numStars; i++) {
const star = document.createElement('div');
star.className = 'star';
// Random positions
const top = Math.random() * 100;
const left = Math.random() * 100;
const delay = Math.random() * 3;
const size = Math.random() * 2 + 1;
star.style.top = `${top}%`;
star.style.left = `${left}%`;
star.style.animationDelay = `${delay}s`;
star.style.width = `${size}px`;
star.style.height = `${size}px`;
starsContainer.appendChild(star);
}
}
// Set moon to always show waxing crescent
function setMoonPhase() {
moon.className = 'moon waxing-crescent';
}
// Toggle dark mode
function toggleDarkMode() {
const wasDark = document.body.classList.contains('dark-mode');
const systemDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
document.body.classList.remove('dark-mode', 'force-light');
if (!wasDark) {
// Switching to dark
document.body.classList.add('dark-mode');
darkModeToggle.querySelector('.toggle-icon').textContent = '☀️';
localStorage.setItem('darkMode', 'true');
generateStars();
setMoonPhase();
} else {
// Switching to light
if (systemDark) {
// System is dark, so force light mode
document.body.classList.add('force-light');
}
// If system is light, no class needed - CSS handles it
darkModeToggle.querySelector('.toggle-icon').textContent = '🌙';
localStorage.setItem('darkMode', 'false');
}
}
// Init: use saved preference or system preference
function initDarkMode() {
const saved = localStorage.getItem('darkMode');
const systemDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
document.body.classList.remove('dark-mode', 'force-light');
if (saved !== null) {
// User has a saved preference
const isDark = saved === 'true';
if (isDark) {
document.body.classList.add('dark-mode');
darkModeToggle.querySelector('.toggle-icon').textContent = '☀️';
generateStars();
setMoonPhase();
} else {
// User explicitly chose light - force it even if system is dark
document.body.classList.add('force-light');
darkModeToggle.querySelector('.toggle-icon').textContent = '🌙';
}
} else {
// No saved preference - use system preference
if (systemDark) {
document.body.classList.add('dark-mode');
darkModeToggle.querySelector('.toggle-icon').textContent = '☀️';
generateStars();
setMoonPhase();
} else {
// System is light - no class needed, CSS media query handles it
darkModeToggle.querySelector('.toggle-icon').textContent = '🌙';
}
}
}
darkModeToggle.addEventListener('click', toggleDarkMode);
initDarkMode();