diff options
Diffstat (limited to '')
| -rw-r--r-- | src/main.js | 62 |
1 files changed, 61 insertions, 1 deletions
diff --git a/src/main.js b/src/main.js index e9bc9f7..ab8b0b0 100644 --- a/src/main.js +++ b/src/main.js @@ -296,6 +296,7 @@ function init() { setupCalc(); setupListeners(); setupWatchlistListeners(); + handleUrlQueryOnLoad(); } function renderSources() { @@ -382,8 +383,38 @@ function setupWatchlistListeners() { // ═══════════════════════════════════════════════════════════ // SEARCH // ═══════════════════════════════════════════════════════════ +function updateUrlWithQuery(query) { + const url = new URL(window.location); + if (query && query.trim()) { + url.searchParams.set("q", query); + } else { + url.searchParams.delete("q"); + } + // Use replaceState to avoid adding to browser history + window.history.replaceState({}, "", url); +} + +function getQueryFromUrl() { + const params = new URLSearchParams(window.location.search); + return params.get("q") || ""; +} + +function handleUrlQueryOnLoad() { + const query = getQueryFromUrl(); + if (query.trim()) { + // Set the search input value + searchInput.value = query; + // Trigger search + search(query); + } +} + async function search(query) { - if (!query.trim()) return goHome(); + if (!query.trim()) { + updateUrlWithQuery(""); + return goHome(); + } + updateUrlWithQuery(query); // Show results view if (!state.hasSearched) { @@ -459,6 +490,7 @@ function goHome() { mainEl.style.display = "none"; statsBar.classList.remove("visible"); searchInput.value = ""; + updateUrlWithQuery(""); } // ═══════════════════════════════════════════════════════════ @@ -870,6 +902,7 @@ function setupListeners() { clearTimeout(debounce); debounce = setTimeout(() => search(e.target.value), 300); }); + searchInput.addEventListener("keydown", (e) => { if (e.key === "Enter") { clearTimeout(debounce); @@ -877,6 +910,7 @@ function setupListeners() { } if (e.key === "Escape") goHome(); }); + document.addEventListener("keydown", (e) => { if ( e.key === "/" && @@ -888,8 +922,15 @@ function setupListeners() { } if (e.key === "Escape") goHome(); }); + $("clear-btn").onclick = goHome; $("voice-btn").onclick = voiceSearch; + + // Also update URL when clearing with clear button + $("clear-btn").onclick = () => { + goHome(); + searchInput.focus(); + }; } function voiceSearch() { @@ -969,4 +1010,23 @@ function insertBang(bang) { searchInput.focus(); } +window.addEventListener("popstate", (event) => { + const query = getQueryFromUrl(); + if (query !== state.query) { + if (query.trim()) { + searchInput.value = query; + search(query); + } else { + goHome(); + } + } +}); + +// Also update the URL when using shortcuts +window.insertBang = (bang) => { + searchInput.value = `${bang} `; + searchInput.focus(); + updateUrlWithQuery(`${bang} `); +}; + init(); |
