// ============================================================ // Configuration // ============================================================ const API_BASE_URL = 'http://localhost:8000'; const ASK_ENDPOINT = '/ask'; // ============================================================ // DOM Elements // ============================================================ const questionInput = document.getElementById('questionInput'); const askBtn = document.getElementById('askBtn'); const btnText = document.getElementById('btnText'); const btnLoader = document.getElementById('btnLoader'); const resultsSection = document.getElementById('resultsSection'); const errorSection = document.getElementById('errorSection'); const answerText = document.getElementById('answerText'); const sourcesText = document.getElementById('sourcesText'); const languageDetected = document.getElementById('languageDetected'); const errorText = document.getElementById('errorText'); // ============================================================ // Event Listeners // ============================================================ askBtn.addEventListener('click', askQuestion); questionInput.addEventListener('keydown', (e) => { if (e.ctrlKey && e.key === 'Enter') { askQuestion(); } }); // ============================================================ // Main Functions // ============================================================ async function askQuestion() { const question = questionInput.value.trim(); if (!question) { showError('Please enter a question'); return; } // Hide previous results and errors resultsSection.style.display = 'none'; errorSection.style.display = 'none'; // Show loading state setLoading(true); try { // Make API request const response = await fetch(API_BASE_URL + ASK_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ question: question }) }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.detail || 'Failed to get answer'); } const data = await response.json(); // Display results displayResults(data); } catch (error) { console.error('Error:', error); showError(`Error: ${error.message}`); } finally { setLoading(false); } } function displayResults(data) { // Set answer answerText.innerHTML = formatText(data.answer); // Set sources sourcesText.innerHTML = formatText(data.sources); // Set language const langMap = { 'en': 'πŸ‡¬πŸ‡§ English', 'ar': 'πŸ‡ΈπŸ‡¦ Arabic', 'ur': 'πŸ‡΅πŸ‡° Urdu', 'fr': 'πŸ‡«πŸ‡· French', 'de': 'πŸ‡©πŸ‡ͺ German' }; languageDetected.textContent = langMap[data.language] || data.language.toUpperCase(); // Show results section resultsSection.style.display = 'block'; // Scroll to results resultsSection.scrollIntoView({ behavior: 'smooth', block: 'start' }); } function showError(message) { errorText.textContent = message; errorSection.style.display = 'block'; errorSection.scrollIntoView({ behavior: 'smooth', block: 'start' }); } function setLoading(loading) { askBtn.disabled = loading; if (loading) { btnText.style.display = 'none'; btnLoader.style.display = 'inline-block'; } else { btnText.style.display = 'inline'; btnLoader.style.display = 'none'; } } function setQuestion(text) { questionInput.value = text; questionInput.focus(); } function formatText(text) { if (!text) return '

No content available

'; // Escape HTML text = text .replace(/&/g, '&') .replace(//g, '>'); // Convert newlines to line breaks text = text.replace(/\n/g, '
'); // Convert links text = text.replace( /\b(https?:\/\/[^\s]+)/g, '$1' ); // Convert bold markers text = text.replace(/\*\*(.*?)\*\*/g, '$1'); text = text.replace(/__(.*?)__/g, '$1'); // Convert italic markers text = text.replace(/\*(.*?)\*/g, '$1'); text = text.replace(/_(.*?)_/g, '$1'); return `

${text}

`; } // ============================================================ // Initialization // ============================================================ console.log('Islamic QA - Static Frontend Loaded'); console.log(`API URL: ${API_BASE_URL}`); // Check API connection on load window.addEventListener('load', async () => { try { const response = await fetch(API_BASE_URL + '/health'); if (response.ok) { console.log('βœ“ API connection successful'); } } catch (error) { console.warn('⚠ API connection failed:', error.message); console.warn('Make sure to run: python main.py'); } });