-
Notifications
You must be signed in to change notification settings - Fork 4
/
cdp.go
46 lines (41 loc) · 1.27 KB
/
cdp.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
package main
import (
"context"
"fmt"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/devtool"
"github.com/mafredri/cdp/rpcc"
"log"
"time"
)
func tryConnectToChromeUntilSuccess(ctx context.Context, url string) *cdp.Client {
var err error
var client *cdp.Client
for {
client, err = connectToChromeDebugger(ctx, url)
if err == nil {
return client
}
log.Print(fmt.Errorf("can't connect to %s Chrome must be started in debug mode. Retrying every 5 seconds. %w", url, err))
time.Sleep(5 * time.Second)
}
}
// connectToChromeDebugger establishes a debugging session on a remote chrome instance. Chrome must be already started in debug-mode.
// See https://blog.chromium.org/2011/05/remote-debugging-with-chrome-developer.html for more details
func connectToChromeDebugger(ctx context.Context, url string) (*cdp.Client, error) {
// Use the DevTools HTTP/JSON API to manage targets (e.g. pages, webworkers).
devt := devtool.New(url)
pt, err := devt.Get(ctx, devtool.Page)
if err != nil {
pt, err = devt.Create(ctx)
if err != nil {
return nil, err
}
}
// Initiate a new RPC connection to the Chrome DevTools Protocol target.
conn, err := rpcc.DialContext(ctx, pt.WebSocketDebuggerURL)
if err != nil {
return nil, err
}
return cdp.NewClient(conn), nil
}