From 1c46b69a555fe64a5fa82c691c36da08aa34d9eb Mon Sep 17 00:00:00 2001 From: Cade Scroggins Date: Fri, 5 Aug 2022 11:32:34 -0500 Subject: [PATCH] restructure configuration variables --- index.html | 274 +++++++++++++++++++++++++++-------------------------- 1 file changed, 141 insertions(+), 133 deletions(-) diff --git a/index.html b/index.html index 4313b8e..32549e0 100644 --- a/index.html +++ b/index.html @@ -7,125 +7,129 @@ @@ -259,7 +263,8 @@ #renderCommands() { const template = document.getElementById('command-template'); - for (const [key, { name, url }] of Object.entries(CONFIG.commands)) { + for (const [key, { name, url }] of Object.entries(COMMANDS)) { + if (!name || !url) continue; const clone = template.content.cloneNode(true); const command = clone.querySelector('.command'); command.href = url; @@ -453,7 +458,7 @@ static #formatSearchUrl(url, searchPath, search) { if (!searchPath) return url; - const baseUrl = Search.#stripUrlPath(url); + const [baseUrl] = Search.#splitUrl(url); const urlQuery = encodeURIComponent(search); searchPath = searchPath.replace(/{}/g, urlQuery); return baseUrl + searchPath; @@ -475,39 +480,42 @@ return { query, url }; } - if (CONFIG.commands[query]) { - const { key, url } = CONFIG.commands[query]; + if (COMMANDS[query]) { + const { key, url } = COMMANDS[query]; return { key, query, url }; } - let splitBy = CONFIG.searchDelimiter; + let splitBy = CONFIG.commandSearchDelimiter; const [searchKey, rawSearch] = query.split(new RegExp(`${splitBy}(.*)`)); - if (CONFIG.commands[searchKey]) { - const { searchTemplate, url: base } = CONFIG.commands[searchKey]; + if (COMMANDS[searchKey]) { + const { searchTemplate, url: base } = COMMANDS[searchKey]; const search = rawSearch.trim(); const url = Search.#formatSearchUrl(base, searchTemplate, search); return { key: searchKey, query, search, splitBy, url }; } - splitBy = CONFIG.pathDelimiter; + splitBy = CONFIG.commandPathDelimiter; const [pathKey, path] = query.split(new RegExp(`${splitBy}(.*)`)); - if (CONFIG.commands[pathKey]) { - const { url: base } = CONFIG.commands[pathKey]; - const url = `${Search.#stripUrlPath(base)}/${path}`; + if (COMMANDS[pathKey]) { + const { url: base } = COMMANDS[pathKey]; + const [baseUrl] = Search.#splitUrl(base); + const url = `${baseUrl}/${path}`; return { key: pathKey, path, query, splitBy, url }; } - const { searchTemplate, url: base } = CONFIG.defaultCommand; - const url = Search.#formatSearchUrl(base, searchTemplate, query); + const [baseUrl, rest] = Search.#splitUrl(CONFIG.defaultSearchTemplate); + const url = Search.#formatSearchUrl(baseUrl, rest, query); return { query, search: query, url }; }; - static #stripUrlPath(url) { + static #splitUrl(url) { const parser = document.createElement('a'); parser.href = url; - return `${parser.protocol}//${parser.hostname}`; + const baseUrl = `${parser.protocol}//${parser.hostname}`; + const rest = `${parser.pathname}${parser.search}`; + return [baseUrl, rest]; } #close() { @@ -548,7 +556,7 @@ return; } - let suggestions = CONFIG.suggestions[q.query] ?? []; + let suggestions = COMMANDS[q.query]?.suggestions ?? []; if (q.search && suggestions.length < CONFIG.suggestionLimit) { const res = await Search.#fetchDuckDuckGoSuggestions(q.search);