From 7a2f673fd04273d6af0941b39f6c69a460d32d90 Mon Sep 17 00:00:00 2001
From: Antoni Sawicki <tenox@google.com>
Date: Fri, 31 May 2019 18:20:55 -0700
Subject: [PATCH] more ismap stuff

---
 wrp.go | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)

diff --git a/wrp.go b/wrp.go
index 625e7e8..f477bac 100644
--- a/wrp.go
+++ b/wrp.go
@@ -72,22 +72,22 @@ 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, "<HTML>\n<HEAD><TITLE>WRP %s</TITLE>\n<BODY BGCOLOR=\"#F0F0F0\">", u)
+	fmt.Fprintf(out, "<HTML>\n<HEAD><TITLE>WRP %s</TITLE></HEAD>\n<BODY BGCOLOR=\"#F0F0F0\">", u)
 	fmt.Fprintf(out, "<FORM ACTION=\"/\">URL/Search: <INPUT TYPE=\"TEXT\" NAME=\"url\" VALUE=\"%s\" SIZE=\"40\">", u)
 	fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" VALUE=\"Go\"><P>\n")
-	fmt.Fprintf(out, "ISMAP:<INPUT TYPE=\"CHECKBOX\" NAME=\"i\" %s> [%v]\n", istr, i)
+	fmt.Fprintf(out, "ISMAP:<INPUT TYPE=\"CHECKBOX\" NAME=\"i\" %s> \n", istr)
 	fmt.Fprintf(out, "Width:<INPUT TYPE=\"TEXT\" NAME=\"w\" VALUE=\"%d\" SIZE=\"4\"> \n", w)
 	fmt.Fprintf(out, "Height:<INPUT TYPE=\"TEXT\" NAME=\"h\" VALUE=\"%d\" SIZE=\"4\"> \n", h)
 	fmt.Fprintf(out, "Scale:<INPUT TYPE=\"TEXT\" NAME=\"s\" VALUE=\"%1.2f\" SIZE=\"3\"> \n", s)
 	fmt.Fprintf(out, "Page:<INPUT TYPE=\"HIDDEN\" NAME=\"p\" VALUE=\"%d\"> \n", p)
 	fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" NAME=\"pg\" VALUE=\"<<\"> %d \n", p)
 	fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" NAME=\"pg\" VALUE=\">>\"> \n")
-	fmt.Fprintf(out, "</FORM><P>")
+	fmt.Fprintf(out, "</FORM><P>\n")
 	if len(u) > 4 {
 		if strings.HasPrefix(u, "http") {
-			capture(u, w, h, s, p, out)
+			capture(u, w, h, s, p, i, out)
 		} else {
-			capture(fmt.Sprintf("http://www.google.com/search?q=%s", url.QueryEscape(u)), w, h, s, p, out)
+			capture(fmt.Sprintf("http://www.google.com/search?q=%s", url.QueryEscape(u)), w, h, s, p, i, out)
 		}
 	} else {
 		fmt.Fprintf(out, "No URL or search query specified")
@@ -96,7 +96,7 @@ func pageServer(out http.ResponseWriter, r *http.Request) {
 }
 
 func imgServer(out http.ResponseWriter, req *http.Request) {
-	log.Printf("%s Img Request for %s\n", req.RemoteAddr, req.URL.Path)
+	log.Printf("%s IMG Request for %s\n", req.RemoteAddr, req.URL.Path)
 	gifbuf := gifmap[req.URL.Path]
 	defer delete(gifmap, req.URL.Path)
 	out.Header().Set("Content-Type", "image/gif")
@@ -105,6 +105,11 @@ func imgServer(out http.ResponseWriter, req *http.Request) {
 	out.(http.Flusher).Flush()
 }
 
+func mapServer(out http.ResponseWriter, req *http.Request) {
+	log.Printf("%s MAP Request for %s [%v]\n", req.RemoteAddr, req.URL.Path, req.URL.Query())
+}
+
+
 func haltServer(out http.ResponseWriter, req *http.Request) {
 	log.Printf("%s Shutdown request received [%s]\n", req.RemoteAddr, req.URL.Path)
 	out.Header().Set("Content-Type", "text/plain")
@@ -114,7 +119,7 @@ func haltServer(out http.ResponseWriter, req *http.Request) {
 	os.Exit(0)
 }
 
-func capture(gourl string, w int64, h int64, s float64, p int64, out http.ResponseWriter) {
+func capture(gourl string, w int64, h int64, s float64, p int64, ismap bool, out http.ResponseWriter) {
 	var nodes []*cdp.Node
 	ctxx := chromedp.FromContext(ctx)
 	var pngbuf []byte
@@ -163,7 +168,11 @@ func capture(gourl string, w int64, h int64, s float64, p int64, out http.Respon
 
 	// Process Nodes
 	base, _ := url.Parse(loc)
-	fmt.Fprintf(out, "<IMG SRC=\"%s\" ALT=\"wrp\" USEMAP=\"#map\">\n<MAP NAME=\"map\">\n", imgpath)
+	if ismap {
+		fmt.Fprintf(out, "<A HREF=\"/map/123.map\"><IMG SRC=\"%s\" ALT=\"wrp\" ISMAP></A>", imgpath)
+	} else {
+		fmt.Fprintf(out, "<IMG SRC=\"%s\" ALT=\"wrp\" USEMAP=\"#map\">\n<MAP NAME=\"map\">\n", imgpath)
+	}
 
 	for _, n := range nodes {
 		b, err := dom.GetBoxModel().WithNodeID(n.NodeID).Do(cdp.WithExecutor(ctx, ctxx.Target))
@@ -177,12 +186,18 @@ func capture(gourl string, w int64, h int64, s float64, p int64, out http.Respon
 		target := fmt.Sprintf("/?url=%s&w=%d&h=%d&s=%1.2f&", tgt, w, h, s) // no page# here
 
 		if len(b.Content) > 6 && len(target) > 7 {
+			if ismap {
+
+			} else {
 			fmt.Fprintf(out, "<AREA SHAPE=\"RECT\" COORDS=\"%.f,%.f,%.f,%.f\" ALT=\"%s\" TITLE=\"%s\" HREF=\"%s\">\n",
 				b.Content[0]*s, b.Content[1]*s, b.Content[4]*s, b.Content[5]*s, n.AttributeValue("href"), n.AttributeValue("href"), target)
+			}
 		}
 	}
 
-	fmt.Fprintf(out, "</MAP>\n")
+	if !ismap {
+		fmt.Fprintf(out, "</MAP>\n")
+	}
 	out.(http.Flusher).Flush()
 	log.Printf("Done with caputure for %s\n", gourl)
 }
@@ -196,6 +211,7 @@ func main() {
 	rand.Seed(time.Now().UnixNano())
 	http.HandleFunc("/", pageServer)
 	http.HandleFunc("/img/", imgServer)
+	http.HandleFunc("/map/", mapServer)
 	http.HandleFunc("/favicon.ico", http.NotFound)
 	http.HandleFunc("/halt", haltServer)
 	log.Printf("Starting WRP http server on %s\n", addr)