-
Notifications
You must be signed in to change notification settings - Fork 45
/
example_incognito_test.go
125 lines (107 loc) · 3.2 KB
/
example_incognito_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
package cdp_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/devtool"
"github.com/mafredri/cdp/protocol/page"
"github.com/mafredri/cdp/protocol/runtime"
"github.com/mafredri/cdp/protocol/target"
"github.com/mafredri/cdp/rpcc"
"github.com/mafredri/cdp/session"
)
func Example_incognito() {
err := func() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Fetch the websocket URL for the browser endpoint.
bver, err := devtool.New("http://localhost:9222").Version(ctx)
if err != nil {
return err
}
bconn, err := rpcc.DialContext(ctx, bver.WebSocketDebuggerURL)
if err != nil {
return err
}
defer bconn.Close()
// Initialize the browser CDP client.
bc := cdp.NewClient(bconn)
// Initialize session manager for connecting to targets.
sess, err := session.NewManager(bc)
if err != nil {
return err
}
defer sess.Close()
// Create the new browser context, similar to a new incognito
// window.
createCtx, err := bc.Target.CreateBrowserContext(ctx, target.NewCreateBrowserContextArgs())
if err != nil {
return err
}
// Create a new target belonging to the browser context, similar
// to opening a new tab in an incognito window.
createTargetArgs := target.NewCreateTargetArgs("about:blank").
SetBrowserContextID(createCtx.BrowserContextID)
createTarget, err := bc.Target.CreateTarget(ctx, createTargetArgs)
if err != nil {
return err
}
// Connect to target using the existing websocket connection.
conn, err := sess.Dial(ctx, createTarget.TargetID)
if err != nil {
return err
}
defer conn.Close()
// This cdp client controls the "incognito tab".
c := cdp.NewClient(conn)
err = c.Page.Enable(ctx)
if err != nil {
return err
}
url := "https://github.com/mafredri/cdp"
nav, err := c.Page.Navigate(ctx, page.NewNavigateArgs(url))
if err != nil {
return err
}
if nav.ErrorText != nil {
return errors.New("navigation failed: " + *nav.ErrorText)
}
// Check the window.location for validation.
eval, err := c.Runtime.Evaluate(ctx, runtime.NewEvaluateArgs("window.location.toString();"))
if err != nil {
return err
}
if eval.ExceptionDetails != nil {
return eval.ExceptionDetails
}
var winloc string
err = json.Unmarshal(eval.Result.Value, &winloc)
if err != nil {
return err
}
fmt.Printf("Navigated to %s inside an incognito tab!\n", winloc)
// Close the tab when we are done with it (this is a requirement
// for closing the browser context).
closeReply, err := bc.Target.CloseTarget(ctx, target.NewCloseTargetArgs(createTarget.TargetID))
if err != nil {
return err
}
if !closeReply.Success {
return errors.New("could not close target: " + string(createTarget.TargetID))
}
// Dispose of browser context (a.k.a. incognito window), this
// will fail if not all targets in this context are closed
// beforehand.
err = bc.Target.DisposeBrowserContext(ctx, target.NewDisposeBrowserContextArgs(createCtx.BrowserContextID))
if err != nil {
return err
}
return nil
}()
if err != nil {
fmt.Println(err)
}
// Output: Navigated to https://github.com/mafredri/cdp inside an incognito tab!
}