amine_dubs commited on
Commit
f01bab8
·
1 Parent(s): 60da4f1
Files changed (1) hide show
  1. static/script.js +24 -57
static/script.js CHANGED
@@ -6,35 +6,25 @@ window.onload = function() {
6
  const textTranslationForm = document.querySelector('#text-translation-form');
7
  const docTranslationForm = document.querySelector('#doc-translation-form');
8
 
9
- // Get text form INPUT elements ONCE
10
- const textInput = document.getElementById('text-input');
11
- const sourceLangText = document.getElementById('source-lang-text');
12
- const targetLangText = document.getElementById('target-lang-text');
13
  const textLoadingElement = document.getElementById('text-loading');
14
  const debugElement = document.getElementById('debug-info');
15
  const errorElement = document.getElementById('error-message');
16
  const textResultBox = document.getElementById('text-result');
17
  const textOutputElement = document.getElementById('text-output');
18
 
19
- // Check if essential text elements were found on load
20
- if (!textTranslationForm || !textInput || !sourceLangText || !targetLangText) {
21
- console.error('CRITICAL: Essential text form elements not found on window load!', {
22
- form: !!textTranslationForm,
23
- input: !!textInput,
24
- source: !!sourceLangText,
25
- target: !!targetLangText
26
- });
27
  if (errorElement) {
28
- errorElement.textContent = 'Error: Could not find essential text translation form elements. Check HTML IDs.';
29
  errorElement.style.display = 'block';
30
  }
31
- // Optionally disable the form if elements are missing
32
- if (textTranslationForm) textTranslationForm.style.opacity = '0.5';
33
- return; // Stop further initialization if critical elements missing
34
  }
35
 
36
  // Set up text translation form
37
- console.log('Text translation form and elements found on load');
38
  textTranslationForm.addEventListener('submit', function(event) {
39
  event.preventDefault();
40
  console.log('Text translation form submitted');
@@ -44,53 +34,25 @@ window.onload = function() {
44
  if (errorElement) errorElement.style.display = 'none';
45
  if (debugElement) debugElement.style.display = 'none';
46
 
47
- // --- FINAL DEFENSIVE CHECK using variable captured onload ---
48
- console.log('[DEBUG] Checking textInput variable captured onload:', textInput);
49
-
50
- if (!textInput) {
51
- // This should ideally not happen if the check on load passed
52
- console.error('FATAL: textInput variable (captured onload) is null INSIDE handler.');
53
- showError('Internal error: Text input reference lost.');
54
- return;
55
- } else {
56
- console.log('[DEBUG] textInput variable IS NOT NULL. Type:', typeof textInput);
57
- let inputValue = null;
58
-
59
- try {
60
- console.log('[DEBUG] Attempting to access .value from textInput variable...');
61
- inputValue = textInput.value; // Potential error point (Line 65 approx)
62
- console.log('[DEBUG] Accessed .value successfully. Raw Value:', inputValue);
63
- } catch (e) {
64
- // This catch block should now definitively catch the error if it happens during value access
65
- console.error('FATAL: Error occurred accessing .value from textInput variable:', e);
66
- console.error('[DEBUG] textInput variable state just before error:', textInput);
67
- try {
68
- // Attempt to log outerHTML for more context if possible
69
- console.error('[DEBUG] Element outerHTML at time of error:', textInput.outerHTML);
70
- } catch (htmlError) {
71
- console.error('[DEBUG] Could not get outerHTML:', htmlError);
72
- }
73
- showError('Internal error: Failed to read text input value. Check console.');
74
- return; // Stop execution
75
- }
76
-
77
- // Now use the stored value
78
- const text = inputValue ? inputValue.trim() : '';
79
- console.log('[DEBUG] Trimmed text value:', text);
80
- // --- END FINAL DEFENSIVE CHECK ---
81
 
82
  if (!text) {
83
  showError('Please enter text to translate');
84
  return;
85
  }
86
 
87
- // Fetch other elements needed here
88
- const sourceLangValue = sourceLangText ? sourceLangText.value : null;
89
- const targetLangValue = targetLangText ? targetLangText.value : null;
90
-
91
  if (!sourceLangValue || !targetLangValue) {
92
- console.error('Source or Target language select element is null inside handler!');
93
- showError('Internal error: Language select element missing.');
94
  return;
95
  }
96
 
@@ -143,7 +105,12 @@ window.onload = function() {
143
  .finally(() => {
144
  if (textLoadingElement) textLoadingElement.style.display = 'none';
145
  });
 
 
 
 
146
  }
 
147
  });
148
 
149
  // Document translation handler
 
6
  const textTranslationForm = document.querySelector('#text-translation-form');
7
  const docTranslationForm = document.querySelector('#doc-translation-form');
8
 
9
+ // Get text form OUTPUT elements ONCE
 
 
 
10
  const textLoadingElement = document.getElementById('text-loading');
11
  const debugElement = document.getElementById('debug-info');
12
  const errorElement = document.getElementById('error-message');
13
  const textResultBox = document.getElementById('text-result');
14
  const textOutputElement = document.getElementById('text-output');
15
 
16
+ // Check if essential text elements were found on load (excluding inputs used via FormData)
17
+ if (!textTranslationForm) { // Only check form itself now
18
+ console.error('CRITICAL: Text translation form not found on window load!');
 
 
 
 
 
19
  if (errorElement) {
20
+ errorElement.textContent = 'Error: Could not find the text translation form element. Check HTML ID.';
21
  errorElement.style.display = 'block';
22
  }
23
+ return;
 
 
24
  }
25
 
26
  // Set up text translation form
27
+ console.log('Text translation form found on load');
28
  textTranslationForm.addEventListener('submit', function(event) {
29
  event.preventDefault();
30
  console.log('Text translation form submitted');
 
34
  if (errorElement) errorElement.style.display = 'none';
35
  if (debugElement) debugElement.style.display = 'none';
36
 
37
+ // --- Use FormData Approach ---
38
+ try {
39
+ const formData = new FormData(event.target); // event.target is the form
40
+
41
+ // Get values using the 'name' attributes from the HTML
42
+ const text = formData.get('text') ? formData.get('text').trim() : '';
43
+ const sourceLangValue = formData.get('source_lang');
44
+ const targetLangValue = formData.get('target_lang');
45
+
46
+ console.log('[DEBUG] FormData values - Text:', text, 'Source:', sourceLangValue, 'Target:', targetLangValue);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  if (!text) {
49
  showError('Please enter text to translate');
50
  return;
51
  }
52
 
 
 
 
 
53
  if (!sourceLangValue || !targetLangValue) {
54
+ console.error('Could not read language values from FormData!');
55
+ showError('Internal error: Language selection missing.');
56
  return;
57
  }
58
 
 
105
  .finally(() => {
106
  if (textLoadingElement) textLoadingElement.style.display = 'none';
107
  });
108
+
109
+ } catch (e) {
110
+ console.error('FATAL: Error processing form data or submitting:', e);
111
+ showError('Internal error processing form submission. Check console.');
112
  }
113
+ // --- End FormData Approach ---
114
  });
115
 
116
  // Document translation handler