document.addEventListener('DOMContentLoaded', () => { const searchInput = document.getElementById('search-input'); const searchButton = document.getElementById('search-button'); const resultsContainer = document.getElementById('results'); searchButton.addEventListener('click', performSearch); searchInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') { performSearch(); } }); async function performSearch() { const query = searchInput.value; if (!query) return; resultsContainer.innerHTML = '
Загрузка результатов...
'; try { const response = await fetch(`https://cors-anywhere.herokuapp.com/https://www.google.com/search?q=${encodeURIComponent(query)}&tbm=search`); const html = await response.text(); const parser = new DOMParser(); const doc = parser.parseFromString(html, 'text/html'); displayResults(doc); } catch (error) { console.error('Error fetching search results:', error); resultsContainer.innerHTML = 'Произошла ошибка при поиске. Пожалуйста, попробуйте еще раз.
'; } } function displayResults(doc) { resultsContainer.innerHTML = ''; const searchResults = doc.querySelectorAll('.g'); if (searchResults.length === 0) { resultsContainer.innerHTML = 'Результаты не найдены.
'; return; } searchResults.forEach((result, index) => { const title = result.querySelector('h3')?.textContent; const link = result.querySelector('a')?.href; const snippet = result.querySelector('.VwiC3b')?.textContent; if (title && link && snippet) { const resultItem = document.createElement('div'); resultItem.classList.add('result-item'); resultItem.style.animationDelay = `${index * 0.1}s`; resultItem.innerHTML = `${snippet}
`; resultsContainer.appendChild(resultItem); } }); } });