generated from wabarc/project-template
/
snapshot.go
164 lines (138 loc) · 3.84 KB
/
snapshot.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package snapshot
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
)
// Snapshoter is a webpage snapshot interface.
type Snapshoter interface {
Snapshot(ctx context.Context, url string, options ...SnapshotOption) (io.Reader, error)
}
type chromeRemoteSnapshoter struct {
url string
}
// NewChromeRemoteSnapshoter creates a Snapshoter backed by Chrome DevTools Protocol.
// The addr is the headless chrome websocket debugger endpoint, such as 127.0.0.1:9222.
func NewChromeRemoteSnapshoter(addr string) (Snapshoter, error) {
// Due to issue#505 (https://github.com/chromedp/chromedp/issues/505),
// chrome restricts the host must be IP or localhost, we should rewrite the url.
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s/json/version", addr), nil)
if err != nil {
return nil, err
}
req.Host = "localhost"
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result map[string]interface{}
if err = json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &chromeRemoteSnapshoter{
url: strings.Replace(result["webSocketDebuggerUrl"].(string), "localhost", addr, 1),
}, nil
}
func (s *chromeRemoteSnapshoter) Snapshot(ctx context.Context, url string, options ...SnapshotOption) (io.Reader, error) {
allocatorCtx, cancel := chromedp.NewRemoteAllocator(ctx, s.url)
defer cancel()
ctxt, cancel := chromedp.NewContext(allocatorCtx)
defer cancel()
var opts SnapshotOptions
for _, o := range options {
o(&opts)
}
var buf []byte
captureAction := s.snapshotAction(&buf, opts.Format)
if opts.Format == "pdf" {
captureAction = s.printToPDFAction(&buf, page.PrintToPDF())
}
err := chromedp.Run(ctxt,
emulation.SetDeviceMetricsOverride(opts.Width, opts.Height, opts.ScaleFactor, opts.Mobile),
chromedp.Navigate(url),
captureAction,
s.closePageAction(),
)
if err != nil {
return nil, err
}
return bytes.NewReader(buf), nil
}
func (s *chromeRemoteSnapshoter) printToPDFAction(res *[]byte, params *page.PrintToPDFParams) chromedp.Action {
return chromedp.ActionFunc(func(ctx context.Context) (err error) {
if res == nil {
return
}
if params == nil {
params = page.PrintToPDF()
}
*res, _, err = params.Do(ctx)
return
})
}
func (s *chromeRemoteSnapshoter) snapshotAction(res *[]byte, format string) chromedp.Action {
return chromedp.ActionFunc(func(ctx context.Context) (err error) {
if res == nil {
return
}
params := page.CaptureSnapshot()
// switch format {
// case "pdf":
// // TODO
// // params.Format = page.CaptureSnapshotFormatJpeg
// case "mhtml", "mhtm":
// default:
// }
if data, err := params.Do(ctx); err == nil {
*res = []byte(data)
}
return
})
}
func (s *chromeRemoteSnapshoter) closePageAction() chromedp.Action {
return chromedp.ActionFunc(func(ctx context.Context) (err error) {
return page.Close().Do(ctx)
})
}
// SnapshotOptions is the options used by Snapshot.
type SnapshotOptions struct {
Width int64
Height int64
Mobile bool
Format string // mhtml, pdf, default mhtml.
ScaleFactor float64
}
type SnapshotOption func(*SnapshotOptions)
func WidthSnapshotOption(width int64) SnapshotOption {
return func(opts *SnapshotOptions) {
opts.Width = width
}
}
func HeightSnapshotOption(height int64) SnapshotOption {
return func(opts *SnapshotOptions) {
opts.Height = height
}
}
func ScaleFactorSnapshotOption(factor float64) SnapshotOption {
return func(opts *SnapshotOptions) {
opts.ScaleFactor = factor
}
}
func MobileSnapshotOption(b bool) SnapshotOption {
return func(opts *SnapshotOptions) {
opts.Mobile = b
}
}
func FormatSnapshotOption(format string) SnapshotOption {
return func(opts *SnapshotOptions) {
opts.Format = format
}
}