Skip to content

Commit

Permalink
Force viewport emulation
Browse files Browse the repository at this point in the history
  • Loading branch information
web-flow committed Jun 27, 2021
1 parent afd233f commit c4aef71
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/wabarc/screenshot
go 1.16

require (
github.com/chromedp/cdproto v0.0.0-20210526005521-9e51b9051fd0
github.com/chromedp/cdproto v0.0.0-20210625233425-810000e4a4fc
github.com/chromedp/chromedp v0.7.3
github.com/wabarc/helper v0.0.0-20210613000316-cc3df6340ab5
github.com/wabarc/logger v0.0.0-20210417045349-d0d82e8e99ee
golang.org/x/sys v0.0.0-20210601080250-7ecdf8ef093b // indirect
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 // indirect
)
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
github.com/chromedp/cdproto v0.0.0-20210526005521-9e51b9051fd0 h1:aIcgRshD5I1MfJfB92KBDKpaXrYqj3fkqI8bHdtP3zA=
github.com/chromedp/cdproto v0.0.0-20210526005521-9e51b9051fd0/go.mod h1:At5TxYYdxkbQL0TSefRjhLE3Q0lgvqKKMSFUglJ7i1U=
github.com/chromedp/cdproto v0.0.0-20210625233425-810000e4a4fc h1:Gr9CJ/+rF6ZUXoP83j05KIh/HHBl3i3nXwXPqpLQaw0=
github.com/chromedp/cdproto v0.0.0-20210625233425-810000e4a4fc/go.mod h1:At5TxYYdxkbQL0TSefRjhLE3Q0lgvqKKMSFUglJ7i1U=
github.com/chromedp/chromedp v0.7.3 h1:FvgJICfjvXtDX+miuMUY0NHuY8zQvjS/TcEQEG6Ldzs=
github.com/chromedp/chromedp v0.7.3/go.mod h1:9gC521Yzgrk078Ulv6KIgG7hJ2x9aWrxMBBobTFk30A=
github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic=
Expand All @@ -24,8 +25,8 @@ github.com/wabarc/logger v0.0.0-20210417045349-d0d82e8e99ee h1:MMIp++7eem2CI1jIY
github.com/wabarc/logger v0.0.0-20210417045349-d0d82e8e99ee/go.mod h1:4uYr9fnQaQoDk1ttTzLnSB3lZm3i/vrJwN8EZIB2YuI=
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210601080250-7ecdf8ef093b h1:qh4f65QIVFjq9eBURLEYWqaEXmOyqdUyiBSgaXWccWk=
golang.org/x/sys v0.0.0-20210601080250-7ecdf8ef093b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 h1:RqytpXGR1iVNX7psjB3ff8y7sNFinVFvkx1c8SjBkio=
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
Expand Down
41 changes: 25 additions & 16 deletions screenshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"encoding/json"
"fmt"
"log"
"math"
"net/http"
"os"
"strings"
"sync"
"time"

"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
Expand Down Expand Up @@ -72,8 +74,7 @@ func (s *chromeRemoteScreenshoter) Screenshot(ctx context.Context, urls []string
return nil, fmt.Errorf("Can't connect to headless browser")
}

ctx, _ = chromedp.NewRemoteAllocator(ctx, s.url)
ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
ctx, cancel := chromedp.NewRemoteAllocator(ctx, s.url)
defer cancel()

return screenshotStart(ctx, urls, options...)
Expand All @@ -99,8 +100,7 @@ func Screenshot(ctx context.Context, urls []string, options ...ScreenshotOption)
if disableGPU := os.Getenv("CHROMEDP_DISABLE_GPU"); disableGPU != "" && disableGPU != "false" {
allocOpts = append(allocOpts, chromedp.DisableGPU)
}
ctx, _ = chromedp.NewExecAllocator(ctx, allocOpts...)
ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
ctx, cancel := chromedp.NewExecAllocator(ctx, allocOpts...)
defer cancel()

return screenshotStart(ctx, urls, options...)
Expand Down Expand Up @@ -164,7 +164,7 @@ func screenshotStart(ctx context.Context, urls []string, options ...ScreenshotOp
})

ctx, _ = chromedp.NewContext(ctx)
captureAction := screenshotAction(url, &buf, opts)
captureAction := screenshotAction(&buf, opts)
exportHTML := exportHTML(&raw, opts)
saveAsPDF := printPDF(&pdf, opts)
if err := chromedp.Run(ctx, chromedp.Tasks{
Expand All @@ -176,7 +176,7 @@ func screenshotStart(ctx context.Context, urls []string, options ...ScreenshotOp
// chromedp.Navigate(url),
chromedp.WaitReady("body"),
chromedp.Title(&title),
evaluate(&buf),
evaluate(nil),
captureAction,
exportHTML,
saveAsPDF,
Expand Down Expand Up @@ -222,7 +222,7 @@ func evaluate(res interface{}) chromedp.EvaluateAction {
}

// Note: this will override the viewport emulation settings.
func screenshotAction(url string, res *[]byte, options ScreenshotOptions) chromedp.Action {
func screenshotAction(res *[]byte, options ScreenshotOptions) chromedp.Action {
return chromedp.Tasks{
chromedp.ActionFunc(func(ctx context.Context) (err error) {
// get layout metrics
Expand All @@ -233,9 +233,23 @@ func screenshotAction(url string, res *[]byte, options ScreenshotOptions) chrome
if cssContentSize != nil {
contentSize = cssContentSize
}
if contentSize == nil {
return nil

width, height := int64(math.Ceil(contentSize.Width)), int64(math.Ceil(contentSize.Height))

// force viewport emulation
err = emulation.SetDeviceMetricsOverride(width, height, 1, false).
WithScreenOrientation(&emulation.ScreenOrientation{
Type: emulation.OrientationTypePortraitPrimary,
Angle: 0,
}).Do(ctx)
if err != nil {
return err
}
var clip page.Viewport
x, y := contentSize.X, contentSize.Y
clip.Width, clip.Height = contentSize.Width, contentSize.Height
clip.X, clip.Y = x, y
clip.Scale = 1

// Limit dimensions
if options.MaxHeight > 0 && contentSize.Height > float64(options.MaxHeight) {
Expand All @@ -246,13 +260,8 @@ func screenshotAction(url string, res *[]byte, options ScreenshotOptions) chrome
WithCaptureBeyondViewport(true).
WithQuality(options.Quality).
WithFormat(options.Format).
WithClip(&page.Viewport{
X: contentSize.X,
Y: contentSize.Y,
Width: contentSize.Width,
Height: contentSize.Height,
Scale: 1,
}).Do(ctx)
WithClip(&clip).
Do(ctx)
if err != nil {
return err
}
Expand Down

0 comments on commit c4aef71

Please sign in to comment.