aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.js
diff options
context:
space:
mode:
authorPetri Hienonen <petri.hienonen@gmail.com>2025-12-14 12:19:33 +0200
committerPetri Hienonen <petri.hienonen@gmail.com>2025-12-14 12:19:33 +0200
commit869d2c46b3f15a695a3327b764fcab84224f5cba (patch)
treec6208c7f6f2fa9afc97fd020af71e78d7e76c649 /src/main.js
parent87b32ba483204cded170b3b41d651cfddf777856 (diff)
downloadpage-869d2c46b3f15a695a3327b764fcab84224f5cba.tar.zst
Update
Diffstat (limited to '')
-rw-r--r--src/main.js62
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();