-
Notifications
You must be signed in to change notification settings - Fork 1
/
screenshot.go
130 lines (107 loc) · 2.72 KB
/
screenshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"bytes"
"context"
"fmt"
"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/cdproto/runtime"
"github.com/chromedp/chromedp"
"image/png"
"strings"
"time"
)
func TakeScreenshot(opts ScreenshotOption) (ImageBuffer, error) {
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
ctx, cancel = context.WithTimeout(ctx, 10*time.Second)
defer cancel()
var byteArray ImageBuffer
if err := chromedp.Run(ctx, ScreenshotTasks(opts, &byteArray)); err != nil {
return nil, err
}
if !IsNoBackgroundColor(opts.background.color) {
img, err := ServeFrames(byteArray)
if err != nil {
return nil, err
}
draw := DrawBackground(opts, img)
buffer := new(bytes.Buffer)
if err := png.Encode(buffer, draw); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
return byteArray, nil
}
func ScreenshotTasks(opts ScreenshotOption, buffer *ImageBuffer) chromedp.Tasks {
actions := chromedp.Tasks{
chromedp.Navigate(opts.url),
chromedp.WaitVisible("html", chromedp.ByQuery),
}
var scripts []string
if len(opts.hide) != 0 {
scripts = append(scripts, BuildInvisibleScript(opts.hide))
}
if opts.scrollY != 0 {
scripts = append(scripts, BuildWindowScrollScript(opts.scrollY))
}
for _, script := range scripts {
actions = append(actions, chromedp.ActionFunc(func(ctx context.Context) error {
_, exp, err := runtime.Evaluate(script).Do(ctx)
if err != nil {
return err
}
if exp != nil {
return exp
}
return nil
}))
}
if opts.selector != "" {
actions = append(actions, chromedp.ScrollIntoView(opts.selector))
}
actions = append(actions, chromedp.ActionFunc(func(ctx context.Context) (err error) {
_, _, size, err := page.GetLayoutMetrics().Do(ctx)
if err != nil {
return err
}
err = emulation.
SetDeviceMetricsOverride(opts.width, opts.height, 1, false).
WithScreenOrientation(&emulation.ScreenOrientation{
Type: emulation.OrientationTypePortraitPrimary,
Angle: 0,
}).
Do(ctx)
if err != nil {
return err
}
*buffer, err = page.
CaptureScreenshot().
WithQuality(opts.quality).
WithClip(&page.Viewport{
X: size.X,
Y: size.Y + float64(opts.scrollY),
Width: float64(opts.width),
Height: float64(opts.height),
Scale: 1,
}).
Do(ctx)
return err
}))
return actions
}
/* SCRIPTS */
func BuildInvisibleScript(hide []string) string {
return fmt.Sprintf(`[%s].forEach(item => {
const element = document.querySelector(item)
if(element) element.remove()
})`, fmt.Sprintf(
"'%s'",
strings.Join(hide, `', '`),
),
)
}
func BuildWindowScrollScript(scrollY int64) string {
return fmt.Sprintf(`window.scrollTo(0, %d)`, scrollY)
}