amine_dubs commited on
Commit
91dbc5b
·
1 Parent(s): 841e389
Files changed (1) hide show
  1. static/script.js +38 -38
static/script.js CHANGED
@@ -2,35 +2,36 @@ document.addEventListener('DOMContentLoaded', () => {
2
  // Log when the page loads to confirm JavaScript is working
3
  console.log("Translation app initialized");
4
 
5
- const textForm = document.getElementById('text-translation-form');
6
- const docForm = document.getElementById('doc-translation-form');
7
- const textResultBox = document.getElementById('text-result');
8
- const textOutput = document.getElementById('text-output');
9
- const docResultBox = document.getElementById('doc-result');
10
- const docOutput = document.getElementById('doc-output');
11
- const docFilename = document.getElementById('doc-filename');
12
- const docSourceLang = document.getElementById('doc-source-lang');
13
- const errorMessageDiv = document.getElementById('error-message');
14
- const docLoadingIndicator = document.getElementById('doc-loading');
15
- const debugInfoDiv = document.getElementById('debug-info');
16
 
17
- // Check if all elements are found
18
- if (!textForm) console.error("ERROR: Text translation form not found!");
19
- if (!textResultBox) console.error("ERROR: Text result box not found!");
20
- if (!textOutput) console.error("ERROR: Text output element not found!");
21
- if (!errorMessageDiv) console.error("ERROR: Error message div not found!");
 
 
 
 
 
 
22
 
23
  // Create text loading indicator if it doesn't exist
24
- let textLoadingDiv = document.getElementById('text-loading');
25
- if (!textLoadingDiv) {
26
  console.log("Creating missing text loading indicator");
27
  textLoadingDiv = document.createElement('div');
28
  textLoadingDiv.id = 'text-loading';
29
  textLoadingDiv.className = 'loading-spinner';
30
  textLoadingDiv.textContent = 'Translating...';
31
- if (textForm) {
32
- textForm.appendChild(textLoadingDiv);
33
- }
34
  }
35
 
36
  // Helper function to display debug info
@@ -123,25 +124,24 @@ document.addEventListener('DOMContentLoaded', () => {
123
  // Show debug immediately to confirm the handler is working
124
  showDebug('Translation submission triggered');
125
 
126
- // Use correct field IDs matching the HTML
127
- const textInput = document.getElementById('text-input');
128
- const sourceLangSelect = document.getElementById('source-lang-text');
129
- const targetLangSelect = document.getElementById('target-lang-text');
130
 
131
- if (!textInput) {
132
- displayError('Text input element not found!');
133
- return;
134
- }
135
- if (!sourceLangSelect) {
136
- displayError('Source language select not found!');
137
- return;
138
- }
139
- if (!targetLangSelect) {
140
- displayError('Target language select not found!');
141
  return;
142
  }
143
 
144
- const sourceText = textInput.value.trim();
 
145
  const sourceLang = sourceLangSelect.value;
146
  const targetLang = targetLangSelect.value;
147
 
@@ -240,10 +240,10 @@ document.addEventListener('DOMContentLoaded', () => {
240
  clearFeedback();
241
 
242
  const formData = new FormData(docForm);
243
- const fileInput = document.getElementById('doc-input');
244
  const button = docForm.querySelector('button');
245
 
246
- if (!fileInput.files || fileInput.files.length === 0) {
247
  displayError('Please select a document to upload.');
248
  return;
249
  }
 
2
  // Log when the page loads to confirm JavaScript is working
3
  console.log("Translation app initialized");
4
 
5
+ // Safely get DOM elements with error handling
6
+ function safeGetElement(id) {
7
+ const element = document.getElementById(id);
8
+ if (!element) {
9
+ console.error(`Element not found: #${id}`);
10
+ }
11
+ return element;
12
+ }
 
 
 
13
 
14
+ const textForm = safeGetElement('text-translation-form');
15
+ const docForm = safeGetElement('doc-translation-form');
16
+ const textResultBox = safeGetElement('text-result');
17
+ const textOutput = safeGetElement('text-output');
18
+ const docResultBox = safeGetElement('doc-result');
19
+ const docOutput = safeGetElement('doc-output');
20
+ const docFilename = safeGetElement('doc-filename');
21
+ const docSourceLang = safeGetElement('doc-source-lang');
22
+ const errorMessageDiv = safeGetElement('error-message');
23
+ const docLoadingIndicator = safeGetElement('doc-loading');
24
+ const debugInfoDiv = safeGetElement('debug-info');
25
 
26
  // Create text loading indicator if it doesn't exist
27
+ let textLoadingDiv = safeGetElement('text-loading');
28
+ if (!textLoadingDiv && textForm) {
29
  console.log("Creating missing text loading indicator");
30
  textLoadingDiv = document.createElement('div');
31
  textLoadingDiv.id = 'text-loading';
32
  textLoadingDiv.className = 'loading-spinner';
33
  textLoadingDiv.textContent = 'Translating...';
34
+ textForm.appendChild(textLoadingDiv);
 
 
35
  }
36
 
37
  // Helper function to display debug info
 
124
  // Show debug immediately to confirm the handler is working
125
  showDebug('Translation submission triggered');
126
 
127
+ // Use correct field IDs matching the HTML and check if they exist
128
+ const textInput = safeGetElement('text-input');
129
+ const sourceLangSelect = safeGetElement('source-lang-text');
130
+ const targetLangSelect = safeGetElement('target-lang-text');
131
 
132
+ // Check all required elements exist before proceeding
133
+ if (!textInput || !sourceLangSelect || !targetLangSelect) {
134
+ displayError('One or more required form elements are missing. Check your HTML structure.');
135
+ console.error('Missing elements:',
136
+ !textInput ? 'text-input' : '',
137
+ !sourceLangSelect ? 'source-lang-text' : '',
138
+ !targetLangSelect ? 'target-lang-text' : ''
139
+ );
 
 
140
  return;
141
  }
142
 
143
+ // Safely extract values
144
+ const sourceText = textInput.value ? textInput.value.trim() : '';
145
  const sourceLang = sourceLangSelect.value;
146
  const targetLang = targetLangSelect.value;
147
 
 
240
  clearFeedback();
241
 
242
  const formData = new FormData(docForm);
243
+ const fileInput = safeGetElement('doc-input');
244
  const button = docForm.querySelector('button');
245
 
246
+ if (!fileInput || !fileInput.files || fileInput.files.length === 0) {
247
  displayError('Please select a document to upload.');
248
  return;
249
  }