File tree Expand file tree Collapse file tree 2 files changed +25
-27
lines changed
Expand file tree Collapse file tree 2 files changed +25
-27
lines changed Original file line number Diff line number Diff line change 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 / ^ h t t p ( 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+ } ) ;
Original file line number Diff line number Diff 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 >
You can’t perform that action at this time.
0 commit comments