-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot.go
667 lines (601 loc) · 17.3 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
package screenshot
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"sync"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/dom"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/cdproto/runtime"
"github.com/chromedp/chromedp"
"github.com/pkg/errors"
"github.com/wabarc/helper"
"github.com/wabarc/logger"
"gopkg.in/yaml.v2"
)
func init() {
debug := os.Getenv("DEBUG")
if debug == "true" || debug == "1" || debug == "on" {
logger.EnableDebug()
}
}
type Screenshots struct {
URL string
Title string
Image []byte
HTML []byte
PDF []byte
HAR []byte
}
// Screenshoter is a webpage screenshot interface.
type Screenshoter interface {
Screenshot(ctx context.Context, input *url.URL, options ...ScreenshotOption) (Screenshots, error)
}
type chromeRemoteScreenshoter struct {
url string
}
// NewChromeRemoteScreenshoter creates a Screenshoter backed by Chrome DevTools Protocol.
// The addr is the headless chrome websocket debugger endpoint, such as 127.0.0.1:9222.
func NewChromeRemoteScreenshoter(addr string) (Screenshoter, 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 &chromeRemoteScreenshoter{
url: strings.Replace(result["webSocketDebuggerUrl"].(string), "localhost", addr, 1),
}, nil
}
func (s *chromeRemoteScreenshoter) Screenshot(ctx context.Context, input *url.URL, options ...ScreenshotOption) (shot Screenshots, err error) {
if s.url == "" {
return shot, fmt.Errorf("can't connect to headless browser")
}
ctx, cancel := chromedp.NewRemoteAllocator(ctx, s.url)
defer cancel()
return screenshotStart(ctx, input, options...)
}
func Screenshot(ctx context.Context, input *url.URL, options ...ScreenshotOption) (shot Screenshots, err error) {
if _, err := exec.LookPath(helper.FindChromeExecPath()); err != nil {
return shot, err
}
// https://github.com/chromedp/chromedp/blob/b56cd66f9cebd6a1fa1283847bbf507409d48225/allocate.go#L53
var allocOpts = append(
chromedp.DefaultExecAllocatorOptions[:],
// chromedp.CombinedOutput(log.Writer()),
chromedp.NoDefaultBrowserCheck,
chromedp.Flag("ignore-certificate-errors", true),
chromedp.Flag("allow-running-insecure-content", true),
chromedp.Flag("no-default-browser-check", true),
chromedp.Flag("disable-notifications", true),
chromedp.Flag("disable-web-security", true),
chromedp.Flag("disable-webgl", true),
chromedp.Flag("disable-gpu", true),
chromedp.Flag("no-first-run", true),
)
f := "false"
if noHeadless := os.Getenv("CHROMEDP_NO_HEADLESS"); noHeadless != "" && noHeadless != f {
allocOpts = append(allocOpts, chromedp.Flag("headless", false))
}
if noSandbox := os.Getenv("CHROMEDP_NO_SANDBOX"); noSandbox != "" && noSandbox != f {
allocOpts = append(allocOpts, chromedp.NoSandbox)
}
if disableGPU := os.Getenv("CHROMEDP_DISABLE_GPU"); disableGPU != "" && disableGPU != f {
allocOpts = append(allocOpts, chromedp.DisableGPU)
}
if userAgent := os.Getenv("CHROMEDP_USER_AGENT"); userAgent != "" {
allocOpts = append(allocOpts, chromedp.UserAgent(userAgent))
}
dir, err := ioutil.TempDir(os.TempDir(), "chromedp-runner-*")
if err == nil && dir != "" {
defer os.RemoveAll(dir)
allocOpts = append(allocOpts, chromedp.UserDataDir(dir))
}
ctx, cancel := chromedp.NewExecAllocator(ctx, allocOpts...)
defer cancel()
return screenshotStart(ctx, input, options...)
}
func screenshotStart(ctx context.Context, input *url.URL, options ...ScreenshotOption) (shot Screenshots, err error) {
var browserOpts []chromedp.ContextOption
if debug := os.Getenv("CHROMEDP_DEBUG"); debug != "" && debug != "false" {
browserOpts = append(browserOpts, chromedp.WithDebugf(log.Printf))
}
ctx, cancel := chromedp.NewContext(ctx, browserOpts...)
defer cancel()
var opts ScreenshotOptions
for _, o := range options {
o(&opts)
}
if opts.Quality != 100 {
opts.Format = page.CaptureScreenshotFormatJpeg
}
// run a no-op action to allocate the browser
// if err := chromedp.Run(ctx, chromedp.ActionFunc(func(_ context.Context) error {
// return nil
// })); err != nil {
// return nil, err
// }
url := convertURI(input)
var buf []byte
var pdf []byte
var har []byte
var raw string
var title string
nRequests := &sync.Map{}
nResponses := &sync.Map{}
requestsID := []network.RequestID{}
wg := sync.WaitGroup{}
mu := sync.Mutex{}
chromedp.ListenTarget(ctx, func(v interface{}) {
switch v := v.(type) {
case *page.EventJavascriptDialogOpening:
go func() {
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
_ = chromedp.Run(ctx, page.HandleJavaScriptDialog(true))
}()
case *network.EventRequestWillBeSent:
wg.Add(1)
go func(r *network.EventRequestWillBeSent) {
defer wg.Done()
var cookies []*network.Cookie
req := processRequest(r, cookies, opts)
nRequests.Store(r.RequestID, req)
mu.Lock()
requestsID = append(requestsID, r.RequestID)
mu.Unlock()
}(v)
case *network.EventResponseReceived:
wg.Add(1)
go func(r *network.EventResponseReceived) {
defer wg.Done()
var body []byte
var ids []cdp.NodeID
var cookies []*network.Cookie
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
mu.Lock()
_ = chromedp.Run(ctx,
chromedp.NodeIDs(`document`, &ids, chromedp.ByJSPath),
chromedp.ActionFunc(func(ctx context.Context) error {
body, err = network.GetResponseBody(r.RequestID).Do(ctx)
return err
}),
chromedp.ActionFunc(func(ctx context.Context) error {
cookies, err = network.GetAllCookies().Do(ctx)
return err
}),
)
mu.Unlock()
res := processResponse(r, cookies, body, opts)
nResponses.Store(r.RequestID, res)
}(v)
case *network.EventDataReceived:
// Fired when data chunk was received over the network.
// go func() {
// edr := v.(*network.EventDataReceived)
// fmt.Printf("%#v\n", edr)
// }()
// case *network.EventLoadingFinished:
// go func() {
// lf := v.(*network.EventLoadingFinished)
// }()
// case *network.EventLoadingFailed:
// // Fired when HTTP request has failed to load.
// go func() {
// lf := v.(*network.EventLoadingFailed)
// }()
}
})
captureAction := screenshotAction(&buf, opts)
exportHTML := exportHTML(&raw, opts)
saveAsPDF := printPDF(&pdf, opts)
if err := chromedp.Run(ctx, chromedp.Tasks{
page.Enable(),
network.Enable(),
// enableLifeCycleEvents(),
stealth(),
setCookies(opts),
setLocalStorage(input, opts),
page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorDeny),
navigateAndWaitFor(url, "DOMContentLoaded"),
// chromedp.Navigate(url),
chromedp.WaitReady("body"),
chromedp.Sleep(time.Second),
scrollToBottom(),
chromedp.Title(&title),
captureAction,
exportHTML,
saveAsPDF,
chromedp.ResetViewport(),
chromedp.Sleep(time.Second),
closePageAction(),
}); err != nil {
log.Print(err)
buf = nil
}
// Wait for all the go routines to complete
wg.Wait()
var html []byte
if raw != "" {
html = helper.String2Byte(raw)
}
har, _ = compose(requestsID, nRequests, nResponses, opts, url)
shot = Screenshots{
URL: revertURI(url),
PDF: pdf,
HAR: har,
HTML: html,
Image: buf,
Title: title,
}
return shot, nil
}
// https://github.com/chromedp/chromedp/blob/875d6f4a3453149639d7fa83a2fa473b743fc33f/poll.go#L88-L127
func scrollToBottom() chromedp.Action {
const script = `()=>{
let distance = 150;
let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
let currentHeight = window.innerHeight + window.pageYOffset;
window.scrollBy(0, distance);
if (currentHeight >= scrollHeight) {
return true;
}
return false;
}`
// Scroll down to the bottom line by line
return chromedp.Tasks{
chromedp.PollFunction(script, nil, chromedp.WithPollingInterval(150*time.Millisecond)),
}
}
func setCookies(options ScreenshotOptions) chromedp.Action {
if len(options.Cookies) == 0 {
return chromedp.Tasks{}
}
return chromedp.Tasks{
chromedp.ActionFunc(func(ctx context.Context) (err error) {
for key := range options.Cookies {
cookie := options.Cookies[key]
expire := cdp.TimeSinceEpoch(cookie.Expires)
er := network.SetCookie(cookie.Name, cookie.Value).
WithDomain(cookie.Domain).
WithPath(cookie.Path).
WithExpires(&expire).
WithHTTPOnly(cookie.HTTPOnly).
WithSecure(cookie.Secure).
WithSameParty(cookie.SameParty).
WithSameSite(network.CookieSameSite(cookie.SameSite)).
WithPriority(network.CookiePriority(cookie.Priority)).
Do(ctx)
if er != nil {
err = errors.Wrap(err, er.Error())
}
}
return err
}),
}
}
func setLocalStorage(u *url.URL, options ScreenshotOptions) chromedp.Action {
if len(options.Storage) == 0 {
return chromedp.Tasks{}
}
return chromedp.Tasks{
chromedp.ActionFunc(func(ctx context.Context) (err error) {
for key := range options.Storage {
item := options.Storage[key]
if u.Host != item.Host {
continue
}
_, exp, er := runtime.Evaluate(fmt.Sprintf(`window.localStorage.setItem('%s', '%s')`, item.Key, item.Value)).Do(ctx)
if er != nil {
err = errors.Wrap(err, er.Error())
}
if exp != nil {
err = errors.Wrap(err, exp.Error())
}
}
return err
}),
}
}
// Note: this will override the viewport emulation settings.
func screenshotAction(res *[]byte, options ScreenshotOptions) chromedp.Action {
return chromedp.Tasks{
chromedp.ActionFunc(func(ctx context.Context) (err error) {
// get layout metrics
_, _, contentSize, _, _, cssContentSize, err := page.GetLayoutMetrics().Do(ctx)
if err != nil {
return err
}
if cssContentSize != nil {
contentSize = cssContentSize
}
// Limit dimensions
if options.MaxHeight > 0 && contentSize.Height > float64(options.MaxHeight) {
contentSize.Height = float64(options.MaxHeight)
}
if options.MaxWidth > 0 && contentSize.Width > float64(options.MaxWidth) {
contentSize.Width = float64(options.MaxWidth)
}
*res, err = page.CaptureScreenshot().
WithCaptureBeyondViewport(true).
WithQuality(options.Quality).
WithFormat(options.Format).
WithClip(&page.Viewport{
X: 0,
Y: 0,
Width: contentSize.Width,
Height: contentSize.Height,
Scale: 1,
}).
Do(ctx)
if err != nil {
return err
}
return nil
}),
}
}
func printPDF(res *[]byte, options ScreenshotOptions) chromedp.Action {
if !options.PrintPDF {
return chromedp.Tasks{}
}
return chromedp.Tasks{
chromedp.ActionFunc(func(ctx context.Context) error {
var err error
*res, _, err = page.PrintToPDF().WithLandscape(true).WithPrintBackground(true).Do(ctx)
return err
}),
}
}
func exportHTML(res *string, options ScreenshotOptions) chromedp.Action {
if !options.RawHTML {
return chromedp.Tasks{}
}
return chromedp.Tasks{
// chromedp.OuterHTML("html", res, chromedp.ByQuery),
chromedp.ActionFunc(func(ctx context.Context) error {
node, err := dom.GetDocument().Do(ctx)
if err != nil {
return err
}
*res, err = dom.GetOuterHTML().WithNodeID(node.NodeID).Do(ctx)
return err
}),
}
}
func closePageAction() chromedp.Action {
return chromedp.ActionFunc(func(ctx context.Context) (err error) {
return page.Close().Do(ctx)
})
}
// page.SetLifecycleEventsEnabled is called by chromedp from v0.5.4
// https://github.com/chromedp/chromedp/issues/431#issuecomment-840433914
// nolint:deadcode,unused
func enableLifeCycleEvents() chromedp.ActionFunc {
return func(ctx context.Context) error {
err := page.Enable().Do(ctx)
if err != nil {
return err
}
err = page.SetLifecycleEventsEnabled(true).Do(ctx)
if err != nil {
return err
}
return nil
}
}
func navigateAndWaitFor(url string, eventName string) chromedp.ActionFunc {
return func(ctx context.Context) error {
_, _, _, err := page.Navigate(url).Do(ctx)
if err != nil {
return err
}
return waitFor(ctx, eventName)
}
}
// waitFor blocks until eventName is received.
// Examples of events you can wait for:
// init, DOMContentLoaded, firstPaint,
// firstContentfulPaint, firstImagePaint,
// firstMeaningfulPaintCandidate,
// load, networkAlmostIdle, firstMeaningfulPaint, networkIdle
//
// This is not super reliable, I've already found incidental cases where
// networkIdle was sent before load. It's probably smart to see how
// puppeteer implements this exactly.
func waitFor(ctx context.Context, eventName string) error {
ch := make(chan struct{})
cctx, cancel := context.WithCancel(ctx)
chromedp.ListenTarget(cctx, func(ev interface{}) {
switch e := ev.(type) {
case *page.EventLifecycleEvent:
if e.Name == eventName {
cancel()
close(ch)
}
default:
}
})
select {
case <-ch:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
// ScreenshotOptions is the options used by Screenshot.
type ScreenshotOptions struct {
Width int64
Height int64
Mobile bool
Format page.CaptureScreenshotFormat // jpg, png, default: png.
Quality int64
MaxWidth int64
MaxHeight int64
ScaleFactor float64
PrintPDF bool
RawHTML bool
DumpHAR bool
Cookies []Cookie
Storage []LocalStorage
}
type ScreenshotOption func(*ScreenshotOptions)
func Width(width int64) ScreenshotOption {
return func(opts *ScreenshotOptions) {
opts.Width = width
}
}
func Height(height int64) ScreenshotOption {
return func(opts *ScreenshotOptions) {
opts.Height = height
}
}
func Quality(quality int64) ScreenshotOption {
return func(opts *ScreenshotOptions) {
opts.Quality = quality
}
}
func MaxWidth(width int64) ScreenshotOption {
return func(opts *ScreenshotOptions) {
opts.MaxWidth = width
}
}
func MaxHeight(height int64) ScreenshotOption {
return func(opts *ScreenshotOptions) {
opts.MaxHeight = height
}
}
func ScaleFactor(factor float64) ScreenshotOption {
return func(opts *ScreenshotOptions) {
opts.ScaleFactor = factor
}
}
func Mobile(b bool) ScreenshotOption {
return func(opts *ScreenshotOptions) {
opts.Mobile = b
}
}
func Format(format string) ScreenshotOption {
return func(opts *ScreenshotOptions) {
switch format {
default:
case "png":
opts.Format = page.CaptureScreenshotFormatPng
case "jpg", "jpeg":
opts.Format = page.CaptureScreenshotFormatJpeg
}
}
}
func PrintPDF(b bool) ScreenshotOption {
return func(opts *ScreenshotOptions) {
opts.PrintPDF = b
}
}
func RawHTML(b bool) ScreenshotOption {
return func(opts *ScreenshotOptions) {
opts.RawHTML = b
}
}
func DumpHAR(b bool) ScreenshotOption {
return func(opts *ScreenshotOptions) {
opts.DumpHAR = b
}
}
// Cookie represents a cookie item.
type Cookie struct {
Name string `yaml:"name"` // Cookie name.
Value string `yaml:"value"` // Cookie value.
Domain string `yaml:"domain"` // Cookie domain.
Path string `yaml:"path,omitempty"` // Cookie path.
Expires time.Time `yaml:"expires,omitempty"` // Cookie expiration date, session cookie if not set
Size int `yaml:"size,omitempty"` // Cookie size.
HTTPOnly bool `yaml:"httpOnly,omitempty"` // True if cookie is http-only.
Secure bool `yaml:"secure,omitempty"` // True if cookie is secure.
SameSite string `yaml:"sameSite,omitempty"` // Cookie SameSite type.
SameParty bool `yaml:"sameParty,omitempty"` // True if cookie is SameParty.
Priority string `yaml:"priority,omitempty"` // Cookie Priority type.
}
// ImportCookies imports cookies by given byte with yaml configuration.
// Format:
// cookies:
// example.com:
// - name: 'foo'
// value: 'bar'
// - name: 'zoo'
// value: 'zoo'
// example.org:
// - name: 'foo'
// value: 'bar'
func ImportCookies(r []byte) (cookies []Cookie, err error) {
type configs struct {
Cookies map[string][]Cookie `yaml:"cookies"`
}
var cfg configs
if err := yaml.Unmarshal(r, &cfg); err != nil {
return nil, err
}
for domain, items := range cfg.Cookies {
for i := range items {
if items[i].Domain == "" {
items[i].Domain = domain
}
cookies = append(cookies, items[i])
}
}
return cookies, nil
}
func Cookies(cookies []Cookie) ScreenshotOption {
return func(opts *ScreenshotOptions) {
opts.Cookies = cookies
}
}
// LocalStorage represents a local storage item.
type LocalStorage struct {
Key string
Value string
Host string
}
func ImportStorage(r []byte) (storage []LocalStorage, err error) {
type configs struct {
LocalStorage map[string][]LocalStorage `yaml:"local-storage"`
}
var cfg configs
if err := yaml.Unmarshal(r, &cfg); err != nil {
return nil, err
}
for domain, items := range cfg.LocalStorage {
for i := range items {
if items[i].Host == "" {
items[i].Host = domain
}
storage = append(storage, items[i])
}
}
return storage, nil
}
func Storage(storage []LocalStorage) ScreenshotOption {
return func(opts *ScreenshotOptions) {
opts.Storage = storage
}
}