Toowired commited on
Commit
4a96638
·
verified ·
1 Parent(s): 42f611e

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +79 -14
index.html CHANGED
@@ -6,6 +6,9 @@
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
  <title>Google TTS Document Reader (BYOK)</title>
8
  <script src="https://cdn.tailwindcss.com"></script>
 
 
 
9
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
10
  <style>
11
  /* Existing styles... */
@@ -557,8 +560,10 @@
557
  </div>
558
  </div>
559
 
560
- <!-- PDF.js library for PDF support -->
561
- <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.12.313/pdf.min.js"></script>
 
 
562
 
563
  <script>
564
  document.addEventListener('DOMContentLoaded', () => {
@@ -1440,9 +1445,9 @@
1440
  loadVoices();
1441
  }
1442
 
1443
- // Set PDF.js worker path
1444
  if (typeof pdfjsLib !== 'undefined') {
1445
- pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.12.313/pdf.worker.min.js';
1446
  }
1447
 
1448
  // Initialize AudioContext on user interaction
@@ -2789,14 +2794,21 @@
2789
  // Enhanced PDF reader with progress
2790
  function readPDFFile(file) {
2791
  if (typeof pdfjsLib === 'undefined') {
2792
- showToast('PDF.js not loaded. Refreshing page...', 'error');
2793
- setTimeout(() => window.location.reload(), 2000);
 
2794
  return;
2795
  }
2796
 
2797
  const fileURL = URL.createObjectURL(file);
2798
 
2799
- pdfjsLib.getDocument(fileURL).promise.then(pdf => {
 
 
 
 
 
 
2800
  let text = '';
2801
  const numPages = pdf.numPages;
2802
  const pagePromises = [];
@@ -2819,7 +2831,15 @@
2819
  <p class="text-xs text-gray-400 mt-2">${pagesProcessed}/${numPages} pages</p>
2820
  </div>
2821
  `;
2822
- return textContent.items.map(item => item.str).join(' ');
 
 
 
 
 
 
 
 
2823
  });
2824
  }));
2825
  }
@@ -2838,7 +2858,7 @@
2838
  });
2839
  }).catch(error => {
2840
  console.error('Error loading PDF:', error);
2841
- showToast('Failed to load PDF file', 'error');
2842
  URL.revokeObjectURL(fileURL);
2843
  showFileProgress(file.name, 'error');
2844
  });
@@ -2846,24 +2866,69 @@
2846
 
2847
  // Word document reader (.doc/.docx)
2848
  function readWordDocument(file) {
 
 
 
 
 
 
 
 
2849
  const reader = new FileReader();
2850
  reader.onload = async (e) => {
2851
  try {
 
 
2852
  // Use mammoth to convert docx to text
2853
- const result = await mammoth.extractRawText({arrayBuffer: e.target.result});
2854
- setDocumentContent(result.value);
 
 
 
 
 
2855
 
2856
- if (result.messages.length > 0) {
 
 
 
 
 
 
 
 
 
 
 
2857
  console.warn('Word processing warnings:', result.messages);
 
 
 
 
 
2858
  }
2859
  } catch (error) {
2860
  console.error('Error reading Word document:', error);
2861
- showToast('Error reading Word document. Please try saving as a different format.', 'error');
 
 
 
 
 
 
 
 
 
 
 
2862
  }
2863
  };
 
2864
  reader.onerror = () => {
2865
- showToast('Error reading Word document', 'error');
 
2866
  };
 
2867
  reader.readAsArrayBuffer(file);
2868
  }
2869
 
 
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
  <title>Google TTS Document Reader (BYOK)</title>
8
  <script src="https://cdn.tailwindcss.com"></script>
9
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.16.105/pdf.min.js"></script>
10
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/mammoth/1.4.21/mammoth.browser.min.js"></script>
11
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
12
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
13
  <style>
14
  /* Existing styles... */
 
560
  </div>
561
  </div>
562
 
563
+ <!-- Required libraries for document processing -->
564
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.16.105/pdf.min.js"></script>
565
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/mammoth/1.4.21/mammoth.browser.min.js"></script>
566
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
567
 
568
  <script>
569
  document.addEventListener('DOMContentLoaded', () => {
 
1445
  loadVoices();
1446
  }
1447
 
1448
+ // Set PDF.js worker path - Use CDN worker
1449
  if (typeof pdfjsLib !== 'undefined') {
1450
+ pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.16.105/pdf.worker.min.js';
1451
  }
1452
 
1453
  // Initialize AudioContext on user interaction
 
2794
  // Enhanced PDF reader with progress
2795
  function readPDFFile(file) {
2796
  if (typeof pdfjsLib === 'undefined') {
2797
+ showToast('PDF.js not loaded. Please refresh the page and try again.', 'error');
2798
+ documentContent.innerHTML = '<p class="text-gray-500 italic">PDF.js library not available.</p>';
2799
+ showFileProgress(file.name, 'error');
2800
  return;
2801
  }
2802
 
2803
  const fileURL = URL.createObjectURL(file);
2804
 
2805
+ // Get the document
2806
+ const loadingTask = pdfjsLib.getDocument({
2807
+ url: fileURL,
2808
+ verbosity: 0 // Reduce console logging
2809
+ });
2810
+
2811
+ loadingTask.promise.then(pdf => {
2812
  let text = '';
2813
  const numPages = pdf.numPages;
2814
  const pagePromises = [];
 
2831
  <p class="text-xs text-gray-400 mt-2">${pagesProcessed}/${numPages} pages</p>
2832
  </div>
2833
  `;
2834
+
2835
+ // Extract text items and join them
2836
+ return textContent.items.map(item => {
2837
+ // Handle different item types
2838
+ if (item.str) {
2839
+ return item.str;
2840
+ }
2841
+ return '';
2842
+ }).join(' ');
2843
  });
2844
  }));
2845
  }
 
2858
  });
2859
  }).catch(error => {
2860
  console.error('Error loading PDF:', error);
2861
+ showToast('Failed to load PDF file: ' + error.message, 'error');
2862
  URL.revokeObjectURL(fileURL);
2863
  showFileProgress(file.name, 'error');
2864
  });
 
2866
 
2867
  // Word document reader (.doc/.docx)
2868
  function readWordDocument(file) {
2869
+ // Check if mammoth is available
2870
+ if (typeof mammoth === 'undefined') {
2871
+ showToast('Mammoth.js not loaded. Please refresh the page and try again.', 'error');
2872
+ documentContent.innerHTML = '<p class="text-gray-500 italic">Mammoth.js library not available for Word documents.</p>';
2873
+ showFileProgress(file.name, 'error');
2874
+ return;
2875
+ }
2876
+
2877
  const reader = new FileReader();
2878
  reader.onload = async (e) => {
2879
  try {
2880
+ showFileProgress(file.name, 'processing');
2881
+
2882
  // Use mammoth to convert docx to text
2883
+ const options = {
2884
+ convertImage: mammoth.images.ignoreImages // Ignore images for TTS
2885
+ };
2886
+
2887
+ const result = await mammoth.extractRawText({
2888
+ arrayBuffer: e.target.result
2889
+ }, options);
2890
 
2891
+ if (result.value && result.value.trim()) {
2892
+ setDocumentContent(result.value);
2893
+ showFileProgress(file.name, 'success');
2894
+ showToast(`Loaded Word document: ${file.name}`, 'success');
2895
+ } else {
2896
+ showToast('No text content found in Word document', 'error');
2897
+ documentContent.innerHTML = '<p class="text-gray-500 italic">No readable text found in document.</p>';
2898
+ showFileProgress(file.name, 'error');
2899
+ }
2900
+
2901
+ // Log any warnings
2902
+ if (result.messages && result.messages.length > 0) {
2903
  console.warn('Word processing warnings:', result.messages);
2904
+ // Show non-error messages as info
2905
+ const nonErrorMessages = result.messages.filter(m => m.type !== 'error');
2906
+ if (nonErrorMessages.length > 0) {
2907
+ showToast(`Processed with ${nonErrorMessages.length} warnings. Check console for details.`, 'success', 3000);
2908
+ }
2909
  }
2910
  } catch (error) {
2911
  console.error('Error reading Word document:', error);
2912
+
2913
+ // Provide more specific error messages
2914
+ let errorMessage = 'Error reading Word document.';
2915
+ if (error.message.includes('Unsupported file format')) {
2916
+ errorMessage = 'Unsupported Word format. Please use .docx format.';
2917
+ } else if (error.message.includes('corrupted')) {
2918
+ errorMessage = 'Document appears to be corrupted. Please try saving it again.';
2919
+ }
2920
+
2921
+ showToast(errorMessage, 'error');
2922
+ documentContent.innerHTML = '<p class="text-gray-500 italic">Error loading Word document. Please try a different format.</p>';
2923
+ showFileProgress(file.name, 'error');
2924
  }
2925
  };
2926
+
2927
  reader.onerror = () => {
2928
+ showToast('Error reading file', 'error');
2929
+ showFileProgress(file.name, 'error');
2930
  };
2931
+
2932
  reader.readAsArrayBuffer(file);
2933
  }
2934