amine_dubs commited on
Commit ·
a92ab1c
1
Parent(s): 626d52e
- static/script.js +85 -70
static/script.js
CHANGED
|
@@ -44,86 +44,101 @@ window.onload = function() {
|
|
| 44 |
if (errorElement) errorElement.style.display = 'none';
|
| 45 |
if (debugElement) debugElement.style.display = 'none';
|
| 46 |
|
| 47 |
-
// ---
|
| 48 |
-
// Re-fetch the element *right before* using it inside the handler
|
| 49 |
const currentTextInput = document.getElementById('text-input');
|
| 50 |
-
console.log('
|
| 51 |
|
| 52 |
-
// Check if it's null *immediately* before accessing .value
|
| 53 |
if (!currentTextInput) {
|
| 54 |
-
console.error('FATAL:
|
| 55 |
-
showError('Internal error: Text input element
|
| 56 |
-
return; // Stop execution
|
| 57 |
-
}
|
| 58 |
-
// --- END CRITICAL CHECK ---
|
| 59 |
-
|
| 60 |
-
// Get values
|
| 61 |
-
// Use the locally fetched element reference
|
| 62 |
-
const text = currentTextInput.value ? currentTextInput.value.trim() : '';
|
| 63 |
-
if (!text) {
|
| 64 |
-
showError('Please enter text to translate');
|
| 65 |
return;
|
| 66 |
-
}
|
| 67 |
-
|
| 68 |
-
// Fetch other elements needed here (can also use stored references if they are reliable)
|
| 69 |
-
const sourceLangValue = sourceLangText ? sourceLangText.value : null;
|
| 70 |
-
const targetLangValue = targetLangText ? targetLangText.value : null;
|
| 71 |
-
|
| 72 |
-
if (!sourceLangValue || !targetLangValue) {
|
| 73 |
-
console.error('Source or Target language select element is null inside handler!');
|
| 74 |
-
showError('Internal error: Language select element missing.');
|
| 75 |
-
return;
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
-
// Show loading indicator
|
| 79 |
-
if (textLoadingElement) textLoadingElement.style.display = 'block';
|
| 80 |
-
|
| 81 |
-
// Create payload
|
| 82 |
-
const payload = {
|
| 83 |
-
text: text,
|
| 84 |
-
source_lang: sourceLangValue,
|
| 85 |
-
target_lang: targetLangValue
|
| 86 |
-
};
|
| 87 |
-
|
| 88 |
-
console.log('Sending payload:', payload);
|
| 89 |
-
|
| 90 |
-
// Send API request
|
| 91 |
-
fetch('/translate/text', {
|
| 92 |
-
method: 'POST',
|
| 93 |
-
headers: { 'Content-Type': 'application/json' },
|
| 94 |
-
body: JSON.stringify(payload)
|
| 95 |
-
})
|
| 96 |
-
.then(response => {
|
| 97 |
-
if (!response.ok) throw new Error('Server error: ' + response.status);
|
| 98 |
-
return response.text(); // Get raw text first
|
| 99 |
-
})
|
| 100 |
-
.then(responseText => {
|
| 101 |
-
console.log('Response received:', responseText);
|
| 102 |
-
let data;
|
| 103 |
try {
|
| 104 |
-
|
| 105 |
-
} catch (
|
| 106 |
-
console.error(
|
| 107 |
-
throw new Error('Invalid response format from server');
|
| 108 |
}
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
}
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
textResultBox.style.display = 'block';
|
| 118 |
}
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
});
|
| 128 |
|
| 129 |
// Document translation handler
|
|
|
|
| 44 |
if (errorElement) errorElement.style.display = 'none';
|
| 45 |
if (debugElement) debugElement.style.display = 'none';
|
| 46 |
|
| 47 |
+
// --- ULTRA-DEFENSIVE CHECK ---
|
|
|
|
| 48 |
const currentTextInput = document.getElementById('text-input');
|
| 49 |
+
console.log('[DEBUG] Element fetched inside handler:', currentTextInput);
|
| 50 |
|
|
|
|
| 51 |
if (!currentTextInput) {
|
| 52 |
+
console.error('FATAL: getElementById(\'text-input\') returned null INSIDE handler.');
|
| 53 |
+
showError('Internal error: Text input element not found.');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
return;
|
| 55 |
+
} else {
|
| 56 |
+
console.log('[DEBUG] Element IS NOT NULL. Type:', typeof currentTextInput);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
try {
|
| 58 |
+
console.log('[DEBUG] Element outerHTML:', currentTextInput.outerHTML);
|
| 59 |
+
} catch (e) {
|
| 60 |
+
console.error('[DEBUG] Error accessing outerHTML:', e);
|
|
|
|
| 61 |
}
|
| 62 |
|
| 63 |
+
// Now try accessing the value
|
| 64 |
+
let text = '';
|
| 65 |
+
try {
|
| 66 |
+
console.log('[DEBUG] Attempting to access .value...');
|
| 67 |
+
text = currentTextInput.value ? currentTextInput.value.trim() : '';
|
| 68 |
+
console.log('[DEBUG] Accessed .value successfully. Value:', text);
|
| 69 |
+
} catch (e) {
|
| 70 |
+
console.error('FATAL: Error occurred accessing .value:', e);
|
| 71 |
+
console.error('[DEBUG] Element state just before error:', currentTextInput);
|
| 72 |
+
showError('Internal error: Failed to read text input value. Check console.');
|
| 73 |
+
return; // Stop execution
|
| 74 |
}
|
| 75 |
+
// --- END ULTRA-DEFENSIVE CHECK ---
|
| 76 |
|
| 77 |
+
if (!text) {
|
| 78 |
+
showError('Please enter text to translate');
|
| 79 |
+
return;
|
|
|
|
| 80 |
}
|
| 81 |
+
|
| 82 |
+
// Fetch other elements needed here
|
| 83 |
+
const sourceLangValue = sourceLangText ? sourceLangText.value : null;
|
| 84 |
+
const targetLangValue = targetLangText ? targetLangText.value : null;
|
| 85 |
+
|
| 86 |
+
if (!sourceLangValue || !targetLangValue) {
|
| 87 |
+
console.error('Source or Target language select element is null inside handler!');
|
| 88 |
+
showError('Internal error: Language select element missing.');
|
| 89 |
+
return;
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
// Show loading indicator
|
| 93 |
+
if (textLoadingElement) textLoadingElement.style.display = 'block';
|
| 94 |
+
|
| 95 |
+
// Create payload
|
| 96 |
+
const payload = {
|
| 97 |
+
text: text,
|
| 98 |
+
source_lang: sourceLangValue,
|
| 99 |
+
target_lang: targetLangValue
|
| 100 |
+
};
|
| 101 |
+
|
| 102 |
+
console.log('Sending payload:', payload);
|
| 103 |
+
|
| 104 |
+
// Send API request
|
| 105 |
+
fetch('/translate/text', {
|
| 106 |
+
method: 'POST',
|
| 107 |
+
headers: { 'Content-Type': 'application/json' },
|
| 108 |
+
body: JSON.stringify(payload)
|
| 109 |
+
})
|
| 110 |
+
.then(response => {
|
| 111 |
+
if (!response.ok) throw new Error('Server error: ' + response.status);
|
| 112 |
+
return response.text(); // Get raw text first
|
| 113 |
+
})
|
| 114 |
+
.then(responseText => {
|
| 115 |
+
console.log('Response received:', responseText);
|
| 116 |
+
let data;
|
| 117 |
+
try {
|
| 118 |
+
data = JSON.parse(responseText);
|
| 119 |
+
} catch (error) {
|
| 120 |
+
console.error("Failed to parse JSON:", responseText);
|
| 121 |
+
throw new Error('Invalid response format from server');
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
if (!data.success || !data.translated_text) {
|
| 125 |
+
throw new Error(data.error || 'No translation returned or success flag false');
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
// Show result
|
| 129 |
+
if (textResultBox && textOutputElement) {
|
| 130 |
+
textOutputElement.textContent = data.translated_text;
|
| 131 |
+
textResultBox.style.display = 'block';
|
| 132 |
+
}
|
| 133 |
+
})
|
| 134 |
+
.catch(error => {
|
| 135 |
+
showError(error.message || 'Translation failed');
|
| 136 |
+
console.error('Error:', error);
|
| 137 |
+
})
|
| 138 |
+
.finally(() => {
|
| 139 |
+
if (textLoadingElement) textLoadingElement.style.display = 'none';
|
| 140 |
+
});
|
| 141 |
+
}
|
| 142 |
});
|
| 143 |
|
| 144 |
// Document translation handler
|