// API Configuration const API_BASE_URL = 'http://localhost:8000'; // DOM Elements const navLinks = document.querySelectorAll('.nav-link'); const pages = document.querySelectorAll('.page'); const scrapeForm = document.getElementById('scrape-form'); const modelForm = document.getElementById('model-form'); const modelSelect = document.getElementById('model-select'); const modelsList = document.getElementById('models-list'); const resultsSection = document.getElementById('results-section'); const loadingSection = document.getElementById('loading'); const copyJsonBtn = document.getElementById('copy-json'); // Navigation navLinks.forEach(link => { link.addEventListener('click', (e) => { e.preventDefault(); const targetPage = link.dataset.page; // Update active nav link navLinks.forEach(l => l.classList.remove('active')); link.classList.add('active'); // Show target page pages.forEach(page => page.classList.remove('active')); document.getElementById(`${targetPage}-page`).classList.add('active'); // Load data for the page if (targetPage === 'models') { loadModels(); } else if (targetPage === 'requests') { loadSavedRequests(); } }); }); // Scrape Form Handler document.getElementById('extract-btn').addEventListener('click', async (e) => { e.preventDefault(); // Scroll to results section immediately when button is clicked document.getElementById('results-section').scrollIntoView({ behavior: 'smooth', block: 'start' }); const url = document.getElementById('url').value; const query = document.getElementById('query').value; const headless = true; // Always use headless mode const model_name = document.getElementById('model-select').value || null; const scraping_approach = document.getElementById('scraping-approach').value; if (!url || !query) { showToast('Please fill in both URL and query fields', 'error'); return; } if (!model_name) { showToast('Please select a model from the dropdown or add one from the Models page', 'error'); return; } const data = { url: url, query: query, headless: headless, model_name: model_name }; // Show loading state showLoading(true); hideResults(); try { // Choose endpoint based on scraping approach const endpoint = scraping_approach === 'llm' ? '/scrape-with-llm' : '/scrape'; const response = await fetch(`${API_BASE_URL}${endpoint}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); const result = await response.json(); if (response.ok) { displayResults(result); showToast(`Data extracted successfully using ${scraping_approach === 'llm' ? 'LLM-based' : 'Schema-based'} approach!`, 'success'); } else { throw new Error(result.detail || 'Failed to extract data'); } } catch (error) { console.error('Scraping error:', error); showToast(`Error: ${error.message}`, 'error'); } finally { showLoading(false); } }); // Model Form Handler modelForm.addEventListener('submit', async (e) => { e.preventDefault(); const formData = new FormData(modelForm); const data = { model_name: formData.get('model_name'), provider: formData.get('provider'), api_token: formData.get('api_token') }; try { const response = await fetch(`${API_BASE_URL}/models`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); const result = await response.json(); if (response.ok) { showToast('Model saved successfully!', 'success'); modelForm.reset(); loadModels(); loadModelSelect(); } else { throw new Error(result.detail || 'Failed to save model'); } } catch (error) { console.error('Model save error:', error); showToast(`Error: ${error.message}`, 'error'); } }); // Copy JSON Button copyJsonBtn.addEventListener('click', () => { const actualJsonOutput = document.getElementById('actual-json-output'); const textToCopy = actualJsonOutput.textContent; navigator.clipboard.writeText(textToCopy).then(() => { showToast('JSON copied to clipboard!', 'success'); }).catch(() => { showToast('Failed to copy JSON', 'error'); }); }); // Load Models async function loadModels() { try { const response = await fetch(`${API_BASE_URL}/models`); const result = await response.json(); if (response.ok) { displayModels(result.models); } else { throw new Error(result.detail || 'Failed to load models'); } } catch (error) { console.error('Load models error:', error); showToast(`Error: ${error.message}`, 'error'); } } // Display Models function displayModels(models) { if (models.length === 0) { modelsList.innerHTML = '
No models saved yet. Add your first model above!
'; return; } modelsList.innerHTML = models.map(model => `No saved API requests yet. Make your first request from the Scrape page!
'; return; } requestsList.innerHTML = requests.map(request => { const url = request.body.url; const query = request.body.query; const model = request.body.model_name || 'Default Model'; const endpoint = request.endpoint; // Create curl command const curlCommand = `curl -X POST http://localhost:8000${endpoint} \\ -H "Content-Type: application/json" \\ -d '{ "url": "${url}", "query": "${query}", "model_name": "${model}" }'`; return `${curlCommand}