diff --git a/assets/js/main.js b/assets/js/main.js
index 029c066..f65f923 100644
--- a/assets/js/main.js
+++ b/assets/js/main.js
@@ -68,38 +68,100 @@ document.addEventListener('DOMContentLoaded', function () {
})
.then(response => {
if (!response.ok) {
- throw new Error(`HTTP error! status: ${response.status}`);
+ // Try to get error message from JSON response, otherwise use status text
+ return response.json().then(err => {
+ throw new Error(err.message || response.statusText);
+ }).catch(() => {
+ throw new Error(response.statusText);
+ });
}
return response.json();
})
.then(data => {
- if(processingMessage) {
- processingMessage.style.display = 'none';
- }
- if(responseMessageDiv){
- const alertClass = data.status === 'success' ? 'alert-success' : 'alert-danger';
- responseMessageDiv.innerHTML = `
${data.message}
`;
+ processingMessage.style.display = 'none';
+
+ if (data.status === 'success') {
+ const translatedText = data.data.translatedText;
+ const outputContainer = document.getElementById('translation-output-container');
+ const outputElement = document.getElementById('translation-output');
+
+ if (outputElement && outputContainer) {
+ outputElement.textContent = translatedText;
+ outputContainer.style.display = 'block';
+ }
+
+ responseMessageDiv.innerHTML = `${data.message}
`;
+ responseMessageDiv.style.display = 'block';
+
+ } else {
+ responseMessageDiv.innerHTML = `Error: ${data.message}
`;
responseMessageDiv.style.display = 'block';
- }
- if(translateBtn) {
- translateBtn.disabled = false;
- translateBtn.innerHTML = 'Translate';
}
})
.catch(error => {
- if(processingMessage) {
- processingMessage.style.display = 'none';
- }
- if(responseMessageDiv){
- responseMessageDiv.innerHTML = `An error occurred during the upload. Please try again.
`;
- responseMessageDiv.style.display = 'block';
- }
+ processingMessage.style.display = 'none';
+ responseMessageDiv.innerHTML = `An unexpected error occurred: ${error.message}
`;
+ responseMessageDiv.style.display = 'block';
+ console.error('Fetch Error:', error);
+ })
+ .finally(() => {
if(translateBtn) {
translateBtn.disabled = false;
- translateBtn.innerHTML = 'Translate';
+ translateBtn.innerHTML = ' Translate';
}
- console.error('Error:', error);
});
});
}
+
+ // Handle copy and download buttons
+ const copyBtn = document.getElementById('copy-btn');
+ const downloadBtn = document.getElementById('download-btn');
+ const translationOutput = document.getElementById('translation-output');
+
+ if (copyBtn) {
+ copyBtn.addEventListener('click', () => {
+ if (translationOutput && navigator.clipboard) {
+ navigator.clipboard.writeText(translationOutput.textContent).then(() => {
+ const originalText = copyBtn.innerHTML;
+ copyBtn.innerHTML = ' Copied!';
+ copyBtn.classList.add('btn-success');
+ copyBtn.classList.remove('btn-secondary');
+ setTimeout(() => {
+ copyBtn.innerHTML = originalText;
+ copyBtn.classList.remove('btn-success');
+ copyBtn.classList.add('btn-secondary');
+ }, 2000);
+ }).catch(err => {
+ console.error('Failed to copy text: ', err);
+ alert('Failed to copy text. Please try again.');
+ });
+ }
+ });
+ }
+
+ if (downloadBtn) {
+ downloadBtn.addEventListener('click', () => {
+ if (translationOutput && window.jspdf) {
+ try {
+ const { jsPDF } = window.jspdf;
+ const doc = new jsPDF();
+
+ // Set font that supports a wide range of characters
+ doc.setFont('Helvetica');
+ doc.setFontSize(12);
+
+ const text = translationOutput.textContent;
+ const lines = doc.splitTextToSize(text, 180);
+
+ doc.text(lines, 15, 20);
+ doc.save('translation.pdf');
+ } catch (error) {
+ console.error('Failed to generate PDF:', error);
+ alert('Failed to generate PDF. An error occurred.');
+ }
+ } else if (!window.jspdf) {
+ alert('The PDF generation library is not loaded.');
+ }
+ });
+ }
});
\ No newline at end of file
diff --git a/index.php b/index.php
index 52d8f2c..1f2a902 100644
--- a/index.php
+++ b/index.php
@@ -58,11 +58,20 @@
@@ -73,6 +82,15 @@
+
+
+
+
+
+
+
+
+
@@ -92,6 +110,19 @@
Processing your document... This may take a moment.
+
+
@@ -104,6 +135,7 @@
+