diff --git a/main.go b/main.go index bdc19c3..c5f27e9 100644 --- a/main.go +++ b/main.go @@ -42,14 +42,53 @@ func main() { } func handleSearch(w http.ResponseWriter, r *http.Request) { + var query, safe, lang string + + // Differentiate between GET and POST requests to correctly extract query, safe, and lang. if r.Method == "GET" { + // Serve the search page if no query is provided for a GET request + // Or extract the query parameters directly from the URL if present + query = r.URL.Query().Get("q") + if query == "" { + http.ServeFile(w, r, "static/search.html") + return + } + safe = r.URL.Query().Get("safe") + lang = r.URL.Query().Get("lang") + } else if r.Method == "POST" { + // For a POST request, extract form values + query = r.FormValue("q") + safe = r.FormValue("safe") + lang = r.FormValue("lang") + } + + // Early return if query is empty + if query == "" { http.ServeFile(w, r, "static/search.html") return } - query := r.FormValue("q") - if query == "" { - http.ServeFile(w, r, "static/search.html") + // Time + start := time.Now() + + // Adjust the search URL based on safe search and language settings + var safeParam string + if safe == "active" { + safeParam = "&safe=active" + } else { + safeParam = "&safe=off" + } + + var langParam string + if lang != "" { + langParam = "&lr=" + lang + } + + searchURL := "https://www.google.com/search?q=" + url.QueryEscape(query) + safeParam + langParam + print(searchURL+"\n") + req, err := http.NewRequest("GET", searchURL, nil) + if err != nil { + http.Error(w, "Failed to create request", http.StatusInternalServerError) return } @@ -65,16 +104,6 @@ func handleSearch(w http.ResponseWriter, r *http.Request) { randIndex := rand.Intn(len(userAgents)) userAgent := userAgents[randIndex] - // Time - start := time.Now() - - // Search query - searchURL := "https://www.google.com/search?q=" + url.QueryEscape(query) - req, err := http.NewRequest("GET", searchURL, nil) - if err != nil { - http.Error(w, "Failed to create request", http.StatusInternalServerError) - return - } req.Header.Set("User-Agent", userAgent) resp, err := http.DefaultClient.Do(req) if err != nil { @@ -95,12 +124,9 @@ func handleSearch(w http.ResponseWriter, r *http.Request) { doc.Find(".yuRUbf").Each(func(i int, s *goquery.Selection) { link := s.Find("a") href, _ := link.Attr("href") - // Extract header/title header := link.Find("h3").Text() - // Remove the unwanted text from the header/title header = strings.TrimSpace(strings.TrimSuffix(header, "›")) - // Retrieve corresponding description descSelection := doc.Find(".VwiC3b").Eq(i) description := "" if descSelection.Length() > 0 { @@ -114,44 +140,18 @@ func handleSearch(w http.ResponseWriter, r *http.Request) { }) }) - // Retrieve kno-rdesc - kno := "" - knoLink := "" - rdesc := doc.Find(".kno-rdesc") - if rdesc.Length() > 0 { - span := rdesc.Find("span") - kno = span.Text() - descLink := rdesc.Find("a") - knoLink, _ = descLink.Attr("href") - } - - // Retrieve featured snippet - snip := "" - snipSpan := doc.Find(".hgKElc") - if snipSpan.Length() > 0 { - snip = snipSpan.Text() - } - elapsed := time.Since(start) // Prepare data for rendering template data := struct { Results []SearchResult - Title string Query string Fetched string - Snippet string - KnoRdesc string - RdescLink string ElapsedTime string }{ Results: results, - Title: query + " - TailsGo", Query: query, Fetched: fmt.Sprintf("Fetched the results in %.2f seconds", elapsed.Seconds()), - Snippet: snip, - KnoRdesc: kno, - RdescLink: knoLink, ElapsedTime: strconv.FormatFloat(elapsed.Seconds(), 'f', 2, 64), } diff --git a/static/css/style.css b/static/css/style.css index 46ddd70..bdd1722 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -1336,6 +1336,69 @@ p { letter-spacing: normal; } +/* Variables for light theme */ +:root { + --background-color: #ffffff; + --text-color: #000000; + --highlight: #007bff; + --border-color: #dddddd; + --search-bg: #f1f3f4; + --search-bg-input: #ffffff; + --search-bg-input-border: #dfe1e5; + --button: #f8f9fa; + --link: #1a0dab; + --fg: #202124; + --html-bg: #ffffff; + --snip-border: #dfe1e5; + --snip-background: #ffffff; + --image-view: #ffffff; + --image-view-titlebar: #f1f3f4; + --search-button: #5f6368; + --image-select: #e8f0fe; + --view-image-color: #f8f9fa; + --footer-bg: #f2f2f2; + --footer-font: #70757a; + --border: #e0e0e0; + --link-visited: #660099; + --publish-info: #70757a; + --green: #3c802c; +} + +/* Styles for dark theme */ +@media (prefers-color-scheme: dark) { + :root { + --background-color: #202124; + --text-color: #e8eaed; + --highlight: #8ab4f8; + --border-color: #5f6368; + --search-bg: #303134; + --search-bg-input: #202124; + --search-bg-input-border: #5f6368; + --button: #3c4043; + --link: #8ab4f8; + --fg: #e8eaed; + --html-bg: #202124; + --snip-border: #5f6368; + --snip-background: #303134; + --image-view: #202124; + --image-view-titlebar: #303134; + --search-button: #e8eaed; + --image-select: #5f6368; + --view-image-color: #202124; + --footer-bg: #303134; + --footer-font: #e8eaed; + --border: #5f6368; + --link-visited: #c79ff; + --publish-info: #e8eaed; + --green: #8ab4f8; + } +} + +body, h1, p, a, input, button { + color: var(--text-color); /* Applies the text color based on theme */ + background-color: var(--background-color); /* Applies the background color based on theme */ +} + @media only screen and (max-width: 1320px) { .snip { diff --git a/templates/results.html b/templates/results.html index d50250f..548997b 100644 --- a/templates/results.html +++ b/templates/results.html @@ -7,20 +7,29 @@ -
-
-

TailsGo

-
+
+

TailsGo

+ + -
- +
+
+ + + + +
+ {{range .Results}}

{{.Header}}

@@ -28,6 +37,6 @@
{{end}}
- +