aboutsummaryrefslogtreecommitdiff
path: root/frontend/main.ts
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/main.ts')
-rw-r--r--frontend/main.ts94
1 files changed, 11 insertions, 83 deletions
diff --git a/frontend/main.ts b/frontend/main.ts
index 71d9702..c9cd654 100644
--- a/frontend/main.ts
+++ b/frontend/main.ts
@@ -1,97 +1,25 @@
/// <reference lib="dom" />
+import { load_bangs, process_query } from "./query.ts";
+import { add_page_content } from "./ui.ts"
-interface Bangs { [key: string]: string }
-let bangs: Bangs = {}
-let status_el: HTMLElement
globalThis.addEventListener("hashchange", () => process_url())
-globalThis.addEventListener("load", async () => {
- status_el = document.createElement("p")
- document.body.append(status_el)
-
- const bangs_res = await fetch("/bangs.json")
- if (!bangs_res.ok) document.writeln("error: could not download bangs.json")
- bangs = await bangs_res.json()
+globalThis.addEventListener("load", () => {
+ load_bangs() // not awaiting so we can continue loading
process_url()
})
-function setup_page(engine?: string) {
- document.getElementById("content")?.remove()
- const section = document.createElement("section")
- section.id = "content"
-
- if (engine) {
- 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 = `Laufente (default engine: ${engine})`
- document.head.append(link)
-
- const heading = document.createElement("h1")
- heading.textContent = 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 ?? "")
- })
-
- const info = document.createElement("p")
- info.textContent = `To install this as the default search engine, select "Add <name>" in the context-menu of the URL-bar.`
-
- section.append(heading, input, submit, info)
- } else {
- const info = document.createElement("p")
- info.textContent = `todo`
- section.append(info)
- }
- document.body.append(section)
-}
-
-
-function status(text: string, color?: string) {
- status_el.textContent = text
- if (color) status_el.style.color = color
-}
-
function process_url() {
if (document.location.hash.length != 0) {
const input = document.location.hash.substring(1)
const [default_engine, query_encoded] = input.split("#")
- if (!query_encoded) return setup_page(default_engine)
- const query = decodeURIComponent(query_encoded.replaceAll("+"," "))
- process_query(default_engine, query)
- } else {
- setup_page()
- }
-}
-
-function process_query(default_engine: string, query: string) {
- const bang_prefix = "!"
- let url = ""
- if (query.startsWith(bang_prefix)) {
- const [engine, ...query_parts] = query.substring(bang_prefix.length).split(" ")
- url = search_url(engine, query_parts.join(" "))
+ if (query_encoded) {
+ const query = decodeURIComponent(query_encoded.replaceAll("+", " "))
+ process_query(default_engine, query)
+ } else {
+ return add_page_content(default_engine)
+ }
} else {
- url = search_url(default_engine, query)
+ add_page_content()
}
-
- status(`Forwarding to ${url}`, "green")
- setTimeout(() => document.location.href = url, 0)
-}
-
-function search_url(engine: string, query: string) {
- return bangs[engine].replace("{{{s}}}", encodeURIComponent(query).replaceAll("%20","+"))
}