-
Notifications
You must be signed in to change notification settings - Fork 45
/
example_test.go
139 lines (115 loc) · 3.07 KB
/
example_test.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
package cdp_test
import (
"bufio"
"context"
"fmt"
"io/ioutil"
"log"
"os"
"time"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/devtool"
"github.com/mafredri/cdp/protocol/dom"
"github.com/mafredri/cdp/protocol/page"
"github.com/mafredri/cdp/rpcc"
)
func Example() {
err := run(5 * time.Second)
if err != nil {
log.Fatal(err)
}
}
func run(timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
// Use the DevTools HTTP/JSON API to manage targets (e.g. pages, webworkers).
devt := devtool.New("http://127.0.0.1:9222")
pt, err := devt.Get(ctx, devtool.Page)
if err != nil {
pt, err = devt.Create(ctx)
if err != nil {
return err
}
}
// Initiate a new RPC connection to the Chrome DevTools Protocol target.
conn, err := rpcc.DialContext(ctx, pt.WebSocketDebuggerURL)
if err != nil {
return err
}
defer conn.Close() // Leaving connections open will leak memory.
c := cdp.NewClient(conn)
// Open a DOMContentEventFired client to buffer this event.
domContent, err := c.Page.DOMContentEventFired(ctx)
if err != nil {
return err
}
defer domContent.Close()
// Enable events on the Page domain, it's often preferrable to create
// event clients before enabling events so that we don't miss any.
if err = c.Page.Enable(ctx); err != nil {
return err
}
// Create the Navigate arguments with the optional Referrer field set.
navArgs := page.NewNavigateArgs("https://www.google.com").
SetReferrer("https://duckduckgo.com")
nav, err := c.Page.Navigate(ctx, navArgs)
if err != nil {
return err
}
// Wait until we have a DOMContentEventFired event.
if _, err = domContent.Recv(); err != nil {
return err
}
fmt.Printf("Page loaded with frame ID: %s\n", nav.FrameID)
// Fetch the document root node. We can pass nil here
// since this method only takes optional arguments.
doc, err := c.DOM.GetDocument(ctx, nil)
if err != nil {
return err
}
// Get the outer HTML for the page.
result, err := c.DOM.GetOuterHTML(ctx, &dom.GetOuterHTMLArgs{
NodeID: &doc.Root.NodeID,
})
if err != nil {
return err
}
fmt.Printf("HTML: %s\n", result.OuterHTML)
// Capture a screenshot of the current page.
screenshotName := "screenshot.jpg"
screenshotArgs := page.NewCaptureScreenshotArgs().
SetFormat("jpeg").
SetQuality(80)
screenshot, err := c.Page.CaptureScreenshot(ctx, screenshotArgs)
if err != nil {
return err
}
if err = ioutil.WriteFile(screenshotName, screenshot.Data, 0o644); err != nil {
return err
}
fmt.Printf("Saved screenshot: %s\n", screenshotName)
pdfName := "page.pdf"
f, err := os.Create(pdfName)
if err != nil {
return err
}
pdfArgs := page.NewPrintToPDFArgs().
SetTransferMode("ReturnAsStream") // Request stream.
pdfData, err := c.Page.PrintToPDF(ctx, pdfArgs)
if err != nil {
return err
}
sr := c.NewIOStreamReader(ctx, *pdfData.Stream)
r := bufio.NewReader(sr)
// Write to file in ~r.Size() chunks.
_, err = r.WriteTo(f)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
fmt.Printf("Saved PDF: %s\n", pdfName)
return nil
}