// Keep track of active tasks const activeTasks = {}; let pollingInterval = null; // Helper functions for UI const UI = { showLoader: (elementId) => { const el = document.getElementById(elementId); el.innerHTML = '
Processing...'; }, showSuccess: (elementId, message = 'Completed') => { const el = document.getElementById(elementId); el.innerHTML = `✓ ${message}`; }, showError: (elementId, message = 'Failed') => { const el = document.getElementById(elementId); el.innerHTML = `✗ ${message}`; }, showOutput: (content) => { const outputDiv = document.getElementById('output'); outputDiv.textContent = content; outputDiv.classList.remove('hidden'); // Scroll to show output outputDiv.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); }, showImage: (src) => { const img = document.getElementById('result-image'); const container = document.getElementById('image-container'); img.src = src; container.classList.remove('hidden'); // Scroll to show image container.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); }, enableButton: (buttonId) => { document.getElementById(buttonId).disabled = false; }, disableButton: (buttonId) => { document.getElementById(buttonId).disabled = true; } }; // Load PDF files on page load window.addEventListener('DOMContentLoaded', function() { const pdfStatus = document.getElementById('pdf-load-status'); pdfStatus.innerHTML = 'Loading PDF files...'; fetch('/pdf-files') .then(response => { if (!response.ok) { throw new Error('Failed to load PDF files'); } return response.json(); }) .then(data => { const select = document.getElementById('pdf_file'); if (data.length === 0) { UI.showError('pdf-load-status', 'No PDF files found in the current directory'); return; } data.forEach(file => { const option = document.createElement('option'); option.value = file; option.textContent = file; select.appendChild(option); }); UI.showSuccess('pdf-load-status', `Loaded ${data.length} PDF files`); }) .catch(error => { console.error('Error loading PDFs:', error); UI.showError('pdf-load-status', `Error: ${error.message}`); }); // Run an environment check on startup checkEnvironment(); }); // Check the environment function checkEnvironment() { const envDiv = document.getElementById('environment-check'); const envDetails = document.getElementById('env-details'); envDiv.classList.remove('hidden'); envDetails.innerHTML = 'Checking environment...'; fetch('/check-environment') .then(response => response.json()) .then(data => { let html = 'Working directory: ${data.cwd}
Python version: ${data.python_version}
All required scripts found
'; } else { html += `Missing scripts: ${data.missing_scripts.join(', ')}
Found ${data.pdf_files.length} PDF files
`; html += '${file}No PDF files found in the working directory
'; } // Results directory check if (data.results_dir_exists) { html += 'Results directory exists
'; if (data.results_dir_writable) { html += 'Results directory is writable
'; } else { html += 'Results directory is not writable
'; } } else { html += 'Results directory does not exist
'; } html += '${data.files.join('\n')}