aboutsummaryrefslogtreecommitdiff
path: root/frontend/search.ts
blob: 374cccfc7e32d24d7cc0c0ac428b9540a5446700 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { e } from "./helper.ts";
import { bangs, process_query } from "./query.ts";
import { status } from "./ui.ts"

export function section_info_search() {
    return e("section", { class: "info" },
        e("h2", {}, "Setup"),
        e("p", {}, "To install this as the default search engine, select \"Add Fastbangs\" in the context-menu of the URL-bar.")
    )
}

export function section_search(engine: string) {
    link_engine(engine)

    bangs.then(bangs => {
        if (!bangs[engine.toLowerCase()]) {
            status("error", "Engine does not exist")
            window.location.hash = "#"
        }
    })

    const heading = document.createElement("h1")
    heading.textContent = engine
    bangs.then(bangs => heading.textContent = bangs[engine]?.name ?? engine)

    const input = document.createElement("input")
    input.type = "input"
    input.addEventListener("keydown", ev => {
        if (ev.code == "Enter") process_query(engine, input.value ?? "")
    })

    const submit = document.createElement("button")
    submit.textContent = "Search"
    submit.addEventListener("click", () => {
        process_query(engine, input.value ?? "")
    })

    return e("section", { class: "search" },
        heading,
        e("div", { class: "bar" },
            input,
            submit
        )
    )
}

function link_engine(engine: string) {
    if (document.getElementById("search-link")) {
        // <link /> is already present, we need reload because browser wont notice otherwise
        window.location.reload()
    }
    const link = document.createElement("link")
    link.rel = "search"
    link.id = "search-link"
    link.type = "application/opensearchdescription+xml"
    link.href = `/search.xml?default=${encodeURIComponent(engine)}`
    link.title = `Fastbangs (default engine: ${engine})`
    document.head.append(link)
}