From 9b15feacb2e0fae087fe2e9416dd84318f30f19d Mon Sep 17 00:00:00 2001 From: Antoni Sawicki Date: Sun, 6 Nov 2022 01:47:59 -0700 Subject: [PATCH] fix chromedp scrollbar capture --- wrp.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/wrp.go b/wrp.go index b375f71..65e953b 100644 --- a/wrp.go +++ b/wrp.go @@ -219,6 +219,22 @@ func navigate(w wrpReq) { } } +// https://github.com/chromedp/chromedp/issues/979 +func chromedpCaptureScreenshot(res *[]byte, h int64) chromedp.Action { + if res == nil { + panic("res cannot be nil") + } + if h == 0 { + return chromedp.CaptureScreenshot(res) + } + + return chromedp.ActionFunc(func(ctx context.Context) error { + var err error + *res, err = page.CaptureScreenshot().Do(ctx) + return err + }) +} + // Capture currently rendered web page to an image and fake ISMAP func capture(w wrpReq) { var err error @@ -248,12 +264,12 @@ func capture(w wrpReq) { if w.height == 0 && h > 0 { height = h + 30 } - chromedp.Run(ctx, emulation.SetDeviceMetricsOverride(int64(float64(w.width)/w.zoom), height, w.zoom, false)) - // Capture screenshot... - err = chromedp.Run(ctx, + chromedp.Run( + ctx, emulation.SetDeviceMetricsOverride(int64(float64(w.width)/w.zoom), height, w.zoom, false), chromedp.Sleep(time.Second*2), - chromedp.CaptureScreenshot(&pngcap), ) + // Capture screenshot... + err = chromedp.Run(ctx, chromedpCaptureScreenshot(&pngcap, w.height)) if err != nil { if err.Error() == "context canceled" { log.Printf("%s Contex cancelled, try again", w.req.RemoteAddr)