Spaces:
Runtime error
Runtime error
| async function performSearch() { | |
| const searchInput = document.getElementById('searchInput'); | |
| const query = searchInput.value.trim(); | |
| if (!query) return; | |
| try { | |
| const response = await fetch(`/api/search?query=${encodeURIComponent(query)}`); | |
| const results = await response.json(); | |
| displayResults(results); | |
| } catch (error) { | |
| console.error('Search failed:', error); | |
| alert('Failed to perform search'); | |
| } | |
| } | |
| function displayResults(results) { | |
| const resultsContainer = document.getElementById('results'); | |
| resultsContainer.innerHTML = ''; | |
| results.forEach(result => { | |
| const resultElement = document.createElement('div'); | |
| resultElement.className = 'result-item'; | |
| resultElement.textContent = result.title; | |
| resultElement.onclick = () => playAudio(result.id); | |
| resultsContainer.appendChild(resultElement); | |
| }); | |
| } | |
| async function playAudio(id) { | |
| try { | |
| const response = await fetch(`/api/audio/${id}`); | |
| const { audioUrl, title } = await response.json(); | |
| const audioPlayer = document.getElementById('audioPlayer'); | |
| audioPlayer.src = audioUrl; | |
| audioPlayer.style.display = 'block'; | |
| audioPlayer.play(); | |
| } catch (error) { | |
| console.error('Failed to play audio:', error); | |
| alert('Failed to play audio'); | |
| } | |
| } | |
| // Add enter key support for search | |
| document.getElementById('searchInput').addEventListener('keypress', (e) => { | |
| if (e.key === 'Enter') { | |
| performSearch(); | |
| } | |
| }); |