mirror of
https://github.com/tenox7/wrp.git
synced 2024-05-28 22:41:36 +00:00
refactor stuff to improve readability
This commit is contained in:
parent
259f998787
commit
2c2fbd11a6
60
wrp.go
60
wrp.go
|
@ -252,51 +252,55 @@ func haltServer(out http.ResponseWriter, req *http.Request) {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process Keyboard and Mouse events or Navigate to the desired URL.
|
// Determine what action to take
|
||||||
func (w wrpReq) navigate() {
|
func (w wrpReq) action() chromedp.Action {
|
||||||
var err error
|
|
||||||
// Mouse Click
|
// Mouse Click
|
||||||
if w.mouseX > 0 && w.mouseY > 0 {
|
if w.mouseX > 0 && w.mouseY > 0 {
|
||||||
log.Printf("%s Mouse Click %d,%d\n", w.req.RemoteAddr, w.mouseX, w.mouseY)
|
log.Printf("%s Mouse Click %d,%d\n", w.req.RemoteAddr, w.mouseX, w.mouseY)
|
||||||
err = chromedp.Run(ctx, chromedp.MouseClickXY(float64(w.mouseX)/float64(w.scale), float64(w.mouseY)/float64(w.scale)))
|
return chromedp.MouseClickXY(float64(w.mouseX)/float64(w.scale), float64(w.mouseY)/float64(w.scale))
|
||||||
// Buttons
|
}
|
||||||
} else if len(w.buttons) > 0 {
|
// Buttons
|
||||||
|
if len(w.buttons) > 0 {
|
||||||
log.Printf("%s Button %v\n", w.req.RemoteAddr, w.buttons)
|
log.Printf("%s Button %v\n", w.req.RemoteAddr, w.buttons)
|
||||||
switch w.buttons {
|
switch w.buttons {
|
||||||
case "Bk":
|
case "Bk":
|
||||||
err = chromedp.Run(ctx, chromedp.NavigateBack())
|
return chromedp.NavigateBack()
|
||||||
case "Bs":
|
case "Bs":
|
||||||
err = chromedp.Run(ctx, chromedp.KeyEvent("\b"))
|
return chromedp.KeyEvent("\b")
|
||||||
case "Rt":
|
case "Rt":
|
||||||
err = chromedp.Run(ctx, chromedp.KeyEvent("\r"))
|
return chromedp.KeyEvent("\r")
|
||||||
case "<":
|
case "<":
|
||||||
err = chromedp.Run(ctx, chromedp.KeyEvent("\u0302"))
|
return chromedp.KeyEvent("\u0302")
|
||||||
case "^":
|
case "^":
|
||||||
err = chromedp.Run(ctx, chromedp.KeyEvent("\u0304"))
|
return chromedp.KeyEvent("\u0304")
|
||||||
case "v":
|
case "v":
|
||||||
err = chromedp.Run(ctx, chromedp.KeyEvent("\u0301"))
|
return chromedp.KeyEvent("\u0301")
|
||||||
case ">":
|
case ">":
|
||||||
err = chromedp.Run(ctx, chromedp.KeyEvent("\u0303"))
|
return chromedp.KeyEvent("\u0303")
|
||||||
}
|
}
|
||||||
// Keys
|
|
||||||
} else if len(w.keys) > 0 {
|
|
||||||
log.Printf("%s Sending Keys: %#v\n", w.req.RemoteAddr, w.keys)
|
|
||||||
err = chromedp.Run(ctx, chromedp.KeyEvent(w.keys))
|
|
||||||
// Navigate to URL
|
|
||||||
} else {
|
|
||||||
log.Printf("%s Processing Capture Request for %s\n", w.req.RemoteAddr, w.url)
|
|
||||||
err = chromedp.Run(ctx, chromedp.Navigate(w.url))
|
|
||||||
}
|
}
|
||||||
|
// Keys
|
||||||
|
if len(w.keys) > 0 {
|
||||||
|
log.Printf("%s Sending Keys: %#v\n", w.req.RemoteAddr, w.keys)
|
||||||
|
return chromedp.KeyEvent(w.keys)
|
||||||
|
}
|
||||||
|
// Navigate to URL
|
||||||
|
log.Printf("%s Processing Capture Request for %s\n", w.req.RemoteAddr, w.url)
|
||||||
|
return chromedp.Navigate(w.url)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process Keyboard and Mouse events or Navigate to the desired URL.
|
||||||
|
func (w wrpReq) navigate() {
|
||||||
|
err := chromedp.Run(ctx, w.action())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.Error() == "context canceled" {
|
if err.Error() == "context canceled" {
|
||||||
log.Printf("%s Contex cancelled, try again", w.req.RemoteAddr)
|
log.Printf("%s Contex cancelled, try again", w.req.RemoteAddr)
|
||||||
fmt.Fprintf(w.out, "<BR>%s<BR> -- restarting, try again", err)
|
fmt.Fprintf(w.out, "<BR>%s<BR> -- restarting, try again", err)
|
||||||
ctx, cancel = chromedp.NewContext(context.Background())
|
ctx, cancel = chromedp.NewContext(context.Background())
|
||||||
} else {
|
return
|
||||||
log.Printf("%s %s", w.req.RemoteAddr, err)
|
|
||||||
fmt.Fprintf(w.out, "<BR>%s<BR>", err)
|
|
||||||
}
|
}
|
||||||
return
|
log.Printf("%s %s", w.req.RemoteAddr, err)
|
||||||
|
fmt.Fprintf(w.out, "<BR>%s<BR>", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -339,10 +343,10 @@ func (w wrpReq) capture() {
|
||||||
log.Printf("%s Contex cancelled, try again", w.req.RemoteAddr)
|
log.Printf("%s Contex cancelled, try again", w.req.RemoteAddr)
|
||||||
fmt.Fprintf(w.out, "<BR>%s<BR> -- restarting, try again", err)
|
fmt.Fprintf(w.out, "<BR>%s<BR> -- restarting, try again", err)
|
||||||
ctx, cancel = chromedp.NewContext(context.Background())
|
ctx, cancel = chromedp.NewContext(context.Background())
|
||||||
} else {
|
return
|
||||||
log.Printf("%s Failed to capture screenshot: %s\n", w.req.RemoteAddr, err)
|
|
||||||
fmt.Fprintf(w.out, "<BR>Unable to capture screenshot:<BR>%s<BR>\n", err)
|
|
||||||
}
|
}
|
||||||
|
log.Printf("%s Failed to capture screenshot: %s\n", w.req.RemoteAddr, err)
|
||||||
|
fmt.Fprintf(w.out, "<BR>Unable to capture screenshot:<BR>%s<BR>\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
seq := rand.Intn(9999)
|
seq := rand.Intn(9999)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user