Skip to content

Commit 06e423a

Browse files
committed
Move home scripts to a seperate file
1 parent 5a00fb2 commit 06e423a

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

src/lib/home.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
let searchUrl = localStorage.getItem("engine") || "https://www.google.com/search?q=";
2+
let input = document.getElementById("search") as HTMLInputElement | null;
3+
4+
function isUrl(val: string = ""): boolean {
5+
return /^http(s?):\/\//.test(val) || (val.includes(".") && !val.includes(" "));
6+
}
7+
8+
document.addEventListener("astro:page-load", () => {
9+
if (input) {
10+
input.focus();
11+
input.addEventListener("keydown", (e) => {
12+
if (e.key === "Enter") {
13+
let url = input?.value || "";
14+
if (!isUrl(url)) {
15+
url = searchUrl + url;
16+
} else if (!(url.startsWith("https://") || url.startsWith("http://"))) {
17+
url = `https://${url}`;
18+
}
19+
sessionStorage.setItem("goUrl", url);
20+
location.replace("/tabs");
21+
}
22+
});
23+
}
24+
});

src/pages/index.astro

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,4 @@ import { Search } from "lucide-astro";
2121
</div>
2222
</div>
2323
</Layout>
24-
<script is:inline>
25-
let searchUrl = localStorage.getItem("engine") || "https://www.google.com/search?q=";
26-
let input = document.getElementById("search");
27-
function isUrl(val = "") {
28-
if (/^http(s?):\/\//.test(val) || (val.includes(".") && !val.includes(" "))) {
29-
return true;
30-
}
31-
return false;
32-
}
33-
document.addEventListener("astro:page-load", () => {
34-
if (input) {
35-
input.focus();
36-
input.addEventListener("keydown", (e) => {
37-
if (e.key === "Enter") {
38-
let url = input.value;
39-
if (!isUrl(url)) {
40-
url = searchUrl + url;
41-
} else if (!(url.startsWith("https://") || url.startsWith("http://"))) {
42-
url = `https://${url}`;
43-
}
44-
sessionStorage.setItem("goUrl", url);
45-
location.replace("/tabs");
46-
}
47-
});
48-
}
49-
});
50-
</script>
24+
<script src="@/lib/home.ts"></script>

0 commit comments

Comments
 (0)