-
Notifications
You must be signed in to change notification settings - Fork 5
/
cdp-screenshot.go
73 lines (62 loc) · 1.37 KB
/
cdp-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
package main
import (
"context"
"io/ioutil"
"log"
"time"
cdp "github.com/knq/chromedp"
"github.com/knq/chromedp/client"
)
func cdpScreenshot(headless bool, url, css, imgf string) {
var err error
// create context
ctxt, cancel := context.WithCancel(context.Background())
defer cancel()
var c *cdp.CDP
if headless {
// create chrome-headless instance
c, err = cdp.New(ctxt, cdp.WithTargets(client.New().WatchPageTargets(ctxt)))
} else {
// create chrome instance
c, err = cdp.New(ctxt)
}
abortOn("Create chrome instance", err)
if rootOpts.Verbose >= 1 {
err := cdp.WithErrorf(log.Printf)(c)
abortOn("Turn on logging", err)
}
// run task list
var buf []byte
err = c.Run(ctxt, screenshot(url, css, &buf))
if err != nil {
log.Fatal(err)
}
// shutdown chrome
err = c.Shutdown(ctxt)
if err != nil {
log.Fatal(err)
}
// wait for chrome to finish
err = c.Wait()
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(imgf, buf, 0644)
if err != nil {
log.Fatal(err)
}
}
func screenshot(urlstr, sel string, res *[]byte) cdp.Tasks {
s, err := time.ParseDuration(rootOpts.Sleep)
abortOn("Parsing sleep time", err)
t := cdp.Tasks{
cdp.Navigate(urlstr),
cdp.Sleep(s),
cdp.WaitVisible(sel, cdp.ByQuery),
cdp.Screenshot(sel, res, cdp.ByQuery),
}
// if len(Opts.WaitFor) != 0 {
// cdp.WaitVisible(Opts.WaitFor, cdp.ByQuery),
// }
return t
}