-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
137 lines (123 loc) · 3 KB
/
main.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
// Command simple is a chromedp example demonstrating how to do a simple google
// search.
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/dom"
"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp/runner"
"github.com/pkg/errors"
"github.com/chromedp/chromedp"
)
func main() {
var err error
url := "https://www.google.com/"
// create context
ctxt, cancel := context.WithCancel(context.Background())
defer cancel()
// create chrome instance
c, err := chromedp.New(ctxt,
chromedp.WithLog(log.Printf),
chromedp.WithRunnerOptions(runner.WindowSize(412, 800),
runner.UserAgent(""),
runner.Flag("headless", true)),
)
if err != nil {
log.Fatal(err)
}
rInfo := ChromeRunnerInfo{
Url: url,
SaveTo: "111.jpeg",
}
err = c.Run(ctxt, chromedp.Tasks{
navigate(&rInfo),
chromedp.Sleep(3 * time.Second),
getBaseUrl(&rInfo),
setLayoutMetrics(),
captureScreenshot(&rInfo),
})
fmt.Printf("%+v", rInfo)
// 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("contact-form.png", buf, 0644)
if err != nil {
log.Fatal(err)
}
}
type ChromeRunnerInfo struct {
Url string
SaveTo string
Redirect string
ErrTxt string
}
func navigate(rInfo *ChromeRunnerInfo) chromedp.ActionFunc {
return func(ctxt context.Context, h cdp.Executor) error {
th, ok := h.(*chromedp.TargetHandler)
if !ok {
return chromedp.ErrInvalidHandler
}
frameID, _, errTxt, err := page.Navigate(rInfo.Url).Do(ctxt, th)
if errTxt != "" { // ERR_CONNECTION_TIMED_OUT ERR_NAME_NOT_RESOLVED
rInfo.ErrTxt = errTxt
return errors.New(errTxt)
}
if err != nil {
return err
}
return th.SetActive(ctxt, frameID)
}
}
func getBaseUrl(rInfo *ChromeRunnerInfo) chromedp.ActionFunc {
return func(ctxt context.Context, h cdp.Executor) error {
_, ok := h.(*chromedp.TargetHandler)
if !ok {
return chromedp.ErrInvalidHandler
}
root, err := dom.GetDocument().Do(ctxt, h)
if err != nil {
return err
}
rInfo.Redirect = root.BaseURL
return nil
}
}
func setLayoutMetrics() chromedp.ActionFunc {
return func(i context.Context, executor cdp.Executor) error {
_, _, contentSize, err := page.GetLayoutMetrics().Do(i, executor)
if err != nil {
return err
}
w := int64(contentSize.Width)
h := int64(contentSize.Height)
scale := 1.0
err = emulation.SetDeviceMetricsOverride(w, h, scale, false).WithScale(scale).Do(i, executor)
if err != nil {
return err
}
return nil
}
}
func captureScreenshot(rInfo *ChromeRunnerInfo) chromedp.ActionFunc {
return func(i context.Context, executor cdp.Executor) error {
data, err := page.CaptureScreenshot().WithFormat(page.CaptureScreenshotFormatJpeg).WithQuality(90).Do(i, executor)
if err != nil {
return err
}
ioutil.WriteFile(rInfo.SaveTo, data, 0644)
return nil
}
}