| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
const input = document.getElementById("q"); |
| 8 |
const names = document.getElementById("drop-names"); |
| 9 |
|
| 10 |
|
| 11 |
let inFlight = null; |
| 12 |
|
| 13 |
if (input && names) { |
| 14 |
input.addEventListener("input", debounce(suggest, 150)); |
| 15 |
|
| 16 |
|
| 17 |
if (input.value.trim()) suggest(); |
| 18 |
} |
| 19 |
|
| 20 |
async function suggest() { |
| 21 |
const query = input.value.trim(); |
| 22 |
if (query.length < 2) { |
| 23 |
names.replaceChildren(); |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
inFlight?.abort(); |
| 29 |
const request = inFlight = new AbortController(); |
| 30 |
|
| 31 |
try { |
| 32 |
const response = await fetch(`/api/warframe/names?q=${encodeURIComponent(query)}`, {signal: request.signal}); |
| 33 |
if (!response.ok) return; |
| 34 |
|
| 35 |
const suggestions = await response.json(); |
| 36 |
names.replaceChildren(...suggestions.map(name => { |
| 37 |
const option = document.createElement("option"); |
| 38 |
option.value = name; |
| 39 |
return option; |
| 40 |
})); |
| 41 |
} catch (error) { |
| 42 |
|
| 43 |
|
| 44 |
if (error.name !== "AbortError") console.debug("suggest failed", error); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
function debounce(callback, wait) { |
| 49 |
let timeoutId = null; |
| 50 |
return (...args) => { |
| 51 |
window.clearTimeout(timeoutId); |
| 52 |
timeoutId = window.setTimeout(() => callback(...args), wait); |
| 53 |
}; |
| 54 |
} |