setTimeout(() => { let searchField = document.querySelector("#search"); if (searchField === null) return; let client = new XMLHttpRequest(); client.open("GET", "/pages.json"); client.onreadystatechange = () => { if (client.readyState === 4) fuzzyInit(client.responseText); } client.send(); searchField.focus(); }, 50); function fuzzyInit(pagesFileName) { if (pagesFileName == "") return; let searchField = document.querySelector("#search"); let resultBlock = document.querySelector("#results"); if (searchField === null || resultBlock === null) return; var pages; try { pages = JSON.parse(pagesFileName); } catch (e) { console.error(e); document.body.innerHTML = e.message; return; } pages.sort(); searchField.addEventListener("keyup", (e) => { let searchValue = searchField.value ? searchField.value.toLowerCase() : ""; if (e.code === "Enter") { if (resultBlock.childNodes === null || resultBlock.childNodes[0] === null || resultBlock.childNodes[0].href === undefined) return; window.location = resultBlock.childNodes[0].href; return; } // help if (searchValue === "?" || searchValue === "help") { resultBlock.innerHTML = "
Enter
key would take you to the first page in list, if the list is not empty.Up
and Down
arrow keys to select the page you want and use the Enter
key."
return;
}
let results = [];
for (const [i, page] of pages.entries()) {
ret = fuzzySearch(page.title, searchValue);
if (ret === null)
continue;
results.push({formatted: ret.formatted, link: page.link, score: ret.score});
}
results.sort((x, y) => {return x.score - y.score});
resultBlock.innerHTML = "";
for (const res of results) {
linkBlock = document.createElement("a");
linkBlock.classList.add("hyperlink");
linkBlock.href = res.link;
linkBlock.innerHTML = `