fix for #40 hang on page with no nodes, added timeout for cdp.Nodes

This commit is contained in:
Antoni Sawicki 2019-06-17 00:38:52 -07:00
parent ce51eb6226
commit 1e58c94263

6
wrp.go
View File

@ -177,8 +177,7 @@ func capture(gourl string, w int64, h int64, s float64, co int, p int64, i bool,
chromedp.Evaluate(fmt.Sprintf("window.scrollTo(0, %d);", p*int64(float64(h)*float64(0.9))), &res), chromedp.Evaluate(fmt.Sprintf("window.scrollTo(0, %d);", p*int64(float64(h)*float64(0.9))), &res),
chromedp.Sleep(time.Second*1), chromedp.Sleep(time.Second*1),
chromedp.CaptureScreenshot(&pngbuf), chromedp.CaptureScreenshot(&pngbuf),
chromedp.Location(&loc), chromedp.Location(&loc))
chromedp.Nodes("a", &nodes, chromedp.ByQueryAll))
if err != nil { if err != nil {
if err.Error() == "context canceled" { if err.Error() == "context canceled" {
@ -216,6 +215,9 @@ func capture(gourl string, w int64, h int64, s float64, co int, p int64, i bool,
gifmap[imgpath] = gifbuf gifmap[imgpath] = gifbuf
// Process Nodes // Process Nodes
tctx, cancel := context.WithTimeout(ctx, time.Second*2) // a context with timeout is needed for pages without nodes
defer cancel()
chromedp.Run(tctx, chromedp.Nodes("a", &nodes, chromedp.ByQueryAll))
base, _ := url.Parse(loc) base, _ := url.Parse(loc)
if i { if i {
fmt.Fprintf(out, "<A HREF=\"%s\"><IMG SRC=\"%s\" ALT=\"wrp\" BORDER=\"0\" ISMAP></A>", mappath, imgpath) fmt.Fprintf(out, "<A HREF=\"%s\"><IMG SRC=\"%s\" ALT=\"wrp\" BORDER=\"0\" ISMAP></A>", mappath, imgpath)