|
| 1 | +let suggestions = []; |
| 2 | +let suggestionsLoaded = false; |
1 | 3 |
|
| 4 | +async function loadSuggestions() { |
| 5 | + try { |
| 6 | + const response = await fetch('/data.txt'); |
| 7 | + if (!response.ok) { |
| 8 | + throw new Error('Network response was not ok'); |
| 9 | + } |
| 10 | + const data = await response.text(); |
| 11 | + const lines = data.split('\n'); |
| 12 | + |
| 13 | + suggestions = lines.map(line => { |
| 14 | + const [value, url] = line.split('|'); |
| 15 | + if (value && url) { |
| 16 | + return { value: value.trim(), url: url.trim() }; |
| 17 | + } |
| 18 | + }).filter(Boolean); |
| 19 | + |
| 20 | + suggestionsLoaded = true; |
| 21 | + console.log('Suggestions loaded:', suggestions); |
| 22 | + } catch (error) { |
| 23 | + console.error('Error loading suggestions:', error); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +document.getElementById('search-bar').addEventListener('input', function() { |
| 28 | + if (!suggestionsLoaded) { |
| 29 | + console.log('Suggestions not loaded yet.'); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const input = this.value.trim().toLowerCase(); |
| 34 | + const suggestionsBox = document.getElementById('suggestions-box'); |
| 35 | + const searchBarRect = this.getBoundingClientRect(); |
| 36 | + |
| 37 | + suggestionsBox.innerHTML = ''; |
| 38 | + |
| 39 | + const searchOption = document.createElement('div'); |
| 40 | + searchOption.textContent = `Suchen: ${input}`; |
| 41 | + searchOption.className = 'suggestion'; |
| 42 | + searchOption.style.fontWeight = 'bold'; |
| 43 | + searchOption.addEventListener('click', function() { |
| 44 | + window.location.href = `search?search=${encodeURIComponent(input)}`; |
| 45 | + suggestionsBox.style.display = 'none'; |
| 46 | + }); |
| 47 | + suggestionsBox.appendChild(searchOption); |
| 48 | + |
| 49 | + const filteredSuggestions = suggestions |
| 50 | + .filter(item => item.value.toLowerCase().includes(input)) |
| 51 | + .slice(0, 5); |
| 52 | + |
| 53 | + filteredSuggestions.forEach(suggestion => { |
| 54 | + const div = document.createElement('div'); |
| 55 | + div.className = 'suggestion'; |
| 56 | + div.textContent = suggestion.value; |
| 57 | + div.addEventListener('click', function() { |
| 58 | + window.location.href = suggestion.url; |
| 59 | + document.getElementById('search-bar').value = suggestion.value; |
| 60 | + suggestionsBox.style.display = 'none'; |
| 61 | + }); |
| 62 | + suggestionsBox.appendChild(div); |
| 63 | + }); |
| 64 | + |
| 65 | + suggestionsBox.style.display = 'block'; |
| 66 | + suggestionsBox.style.top = `${searchBarRect.bottom + window.scrollY + 5}px`; |
| 67 | + suggestionsBox.style.left = `${searchBarRect.left}px`; |
| 68 | + suggestionsBox.style.width = `${searchBarRect.width}px`; |
| 69 | + |
| 70 | + console.log('Filtered suggestions:', filteredSuggestions); |
| 71 | +}); |
| 72 | + |
| 73 | +document.addEventListener('click', function(event) { |
| 74 | + const searchBar = document.getElementById('search-bar'); |
| 75 | + const suggestionsBox = document.getElementById('suggestions-box'); |
| 76 | + if (!searchBar.contains(event.target)) { |
| 77 | + suggestionsBox.style.display = 'none'; |
| 78 | + } |
| 79 | +}); |
| 80 | + |
| 81 | +window.onload = loadSuggestions; |
0 commit comments