From 0ae49044c237505d3e26186bc966c4c738d834fc Mon Sep 17 00:00:00 2001 From: Antoni Sawicki Date: Fri, 31 May 2019 00:36:53 -0700 Subject: [PATCH] simple google search if url doesnt start with http --- wrp.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/wrp.go b/wrp.go index 956762a..196b3cb 100644 --- a/wrp.go +++ b/wrp.go @@ -21,6 +21,7 @@ import ( "net/url" "os" "strconv" + "strings" "time" "github.com/chromedp/cdproto/emulation" @@ -56,7 +57,7 @@ func pageServer(out http.ResponseWriter, r *http.Request) { log.Printf("%s Page Reqest for url=\"%s\" [%s]\n", r.RemoteAddr, u, r.URL.Path) out.Header().Set("Content-Type", "text/html") fmt.Fprintf(out, "\nWRP %s\n", u) - fmt.Fprintf(out, "
URL: ", u) + fmt.Fprintf(out, "URL/Search: ", u) fmt.Fprintf(out, "

\n") fmt.Fprintf(out, "Width: \n", w) fmt.Fprintf(out, "Height: \n", h) @@ -64,9 +65,13 @@ func pageServer(out http.ResponseWriter, r *http.Request) { fmt.Fprintf(out, "Scroll: \n", y) fmt.Fprintf(out, "

") if len(u) > 4 { - capture(u, w, h, s, y, out) + if strings.HasPrefix(u, "http") { + capture(u, w, h, s, y, out) + } else { + capture(fmt.Sprintf("http://www.google.com/search?q=%s", url.QueryEscape(u)), w, h, s, y, out) + } } else { - fmt.Fprintf(out, "No URL specified") + fmt.Fprintf(out, "No URL or search query specified") } fmt.Fprintf(out, "\n\n") } @@ -101,11 +106,10 @@ func capture(gourl string, w int64, h int64, s float64, y int64, out http.Respon log.Printf("Processing Caputure Request for %s\n", gourl) // Run ChromeDP Magic - scrl := fmt.Sprintf("window.scrollTo(0, %d);", y) chromedp.Run(ctx, emulation.SetDeviceMetricsOverride(w, h, s, false), chromedp.Navigate(gourl), - chromedp.Evaluate(scrl, &res), + chromedp.Evaluate(fmt.Sprintf("window.scrollTo(0, %d);", y), &res), chromedp.Sleep(time.Second*1), chromedp.CaptureScreenshot(&pngbuf), chromedp.Location(&loc),