62 行
2.1 KiB
JavaScript
62 行
2.1 KiB
JavaScript
(() => {
|
|
const root = new URL ('../', document.currentScript.src)
|
|
const origin = window.archiveConfig?.origin
|
|
if (typeof origin === 'string')
|
|
{
|
|
try
|
|
{
|
|
const url = new URL (origin)
|
|
if (['http:', 'https:'].includes (url.protocol) && !url.username && !url.password)
|
|
{
|
|
url.search = ''
|
|
url.hash = ''
|
|
document.querySelectorAll ('[data-original]').forEach (link => {
|
|
link.href = url.href.replace (/\/+$/, '') + link.dataset.original
|
|
})
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Each HTML retains its original link when configuration is absent or invalid.
|
|
}
|
|
}
|
|
const form = document.getElementById ('archive-search')
|
|
const results = document.getElementById ('search-results')
|
|
if (!form || !results) return
|
|
form.addEventListener ('submit', async event => {
|
|
event.preventDefault ()
|
|
results.replaceChildren ()
|
|
const query = (new FormData (form).get ('q') ?? '').toString ().toLocaleLowerCase ()
|
|
if (!query.trim ()) return
|
|
try
|
|
{
|
|
const response = await fetch (new URL ('search-index.json', root))
|
|
if (!response.ok) throw new Error ('Search index unavailable')
|
|
const entries = await response.json ()
|
|
for (const entry of entries)
|
|
{
|
|
const text = [entry.title, ...(entry.names ?? [])].join (' ').toLocaleLowerCase ()
|
|
if (!text.includes (query)) continue
|
|
const url = new URL (entry.path, root)
|
|
if (url.origin !== root.origin || !url.pathname.startsWith (root.pathname)) continue
|
|
const item = document.createElement ('li')
|
|
const link = document.createElement ('a')
|
|
link.setAttribute ('href', entry.path)
|
|
link.textContent = entry.title
|
|
item.append (link)
|
|
results.append (item)
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Lists remain available if the index cannot be loaded.
|
|
for (const link of document.querySelectorAll ('nav a:not([data-original])'))
|
|
{
|
|
const item = document.createElement ('li')
|
|
item.append (link.cloneNode (true))
|
|
results.append (item)
|
|
}
|
|
}
|
|
})
|
|
})()
|