-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathbrowser_type.go
More file actions
201 lines (188 loc) · 6.16 KB
/
browser_type.go
File metadata and controls
201 lines (188 loc) · 6.16 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package playwright
import (
"fmt"
)
type browserTypeImpl struct {
channelOwner
playwright *Playwright
}
func (b *browserTypeImpl) Name() string {
return b.initializer["name"].(string)
}
func (b *browserTypeImpl) ExecutablePath() string {
return b.initializer["executablePath"].(string)
}
func (b *browserTypeImpl) Launch(options ...BrowserTypeLaunchOptions) (Browser, error) {
overrides := map[string]any{}
// timeout is required in Playwright v1.57+ protocol
if len(options) == 0 || options[0].Timeout == nil {
overrides["timeout"] = float64(30000) // default 30s
}
if len(options) == 1 && options[0].Env != nil {
overrides["env"] = serializeMapToNameAndValue(options[0].Env)
options[0].Env = nil
}
channel, err := b.channel.Send("launch", options, overrides)
if err != nil {
return nil, err
}
browser := fromChannel(channel).(*browserImpl)
b.didLaunchBrowser(browser)
return browser, nil
}
func (b *browserTypeImpl) LaunchPersistentContext(userDataDir string, options ...BrowserTypeLaunchPersistentContextOptions) (BrowserContext, error) {
overrides := map[string]any{
"userDataDir": userDataDir,
}
// timeout is required in Playwright v1.57+ protocol
if len(options) == 0 || options[0].Timeout == nil {
overrides["timeout"] = float64(30000) // default 30s
}
option := &BrowserNewContextOptions{}
var tracesDir *string = nil
if len(options) == 1 {
tracesDir = options[0].TracesDir
err := assignStructFields(option, options[0], true)
if err != nil {
return nil, fmt.Errorf("can not convert options: %w", err)
}
if options[0].AcceptDownloads != nil {
if *options[0].AcceptDownloads {
overrides["acceptDownloads"] = "accept"
} else {
overrides["acceptDownloads"] = "deny"
}
options[0].AcceptDownloads = nil
}
if options[0].ClientCertificates != nil {
certs, err := transformClientCertificate(options[0].ClientCertificates)
if err != nil {
return nil, err
}
overrides["clientCertificates"] = certs
options[0].ClientCertificates = nil
}
if options[0].ExtraHttpHeaders != nil {
overrides["extraHTTPHeaders"] = serializeMapToNameAndValue(options[0].ExtraHttpHeaders)
options[0].ExtraHttpHeaders = nil
}
if options[0].Env != nil {
overrides["env"] = serializeMapToNameAndValue(options[0].Env)
options[0].Env = nil
}
if options[0].NoViewport != nil && *options[0].NoViewport {
overrides["noDefaultViewport"] = true
options[0].NoViewport = nil
}
if options[0].RecordHarPath != nil {
overrides["recordHar"] = prepareRecordHarOptions(recordHarInputOptions{
Path: *options[0].RecordHarPath,
URL: options[0].RecordHarURLFilter,
Mode: options[0].RecordHarMode,
Content: options[0].RecordHarContent,
OmitContent: options[0].RecordHarOmitContent,
})
options[0].RecordHarPath = nil
options[0].RecordHarURLFilter = nil
options[0].RecordHarMode = nil
options[0].RecordHarContent = nil
options[0].RecordHarOmitContent = nil
}
}
response, err := b.channel.SendReturnAsDict("launchPersistentContext", options, overrides)
if err != nil {
return nil, err
}
context := fromChannel(response["context"]).(*browserContextImpl)
b.didCreateContext(context, option, tracesDir)
if err := context.initializeHarFromOptions(); err != nil {
return nil, err
}
return context, nil
}
func (b *browserTypeImpl) Connect(wsEndpoint string, options ...BrowserTypeConnectOptions) (Browser, error) {
overrides := map[string]any{
"wsEndpoint": wsEndpoint,
"headers": map[string]string{
"x-playwright-browser": b.Name(),
"x-playwright-launch-options": "{}",
},
}
// timeout is required in Playwright v1.57+ protocol
if len(options) == 0 || options[0].Timeout == nil {
overrides["timeout"] = float64(0) // default no timeout
}
if len(options) == 1 {
if options[0].Headers != nil {
for k, v := range options[0].Headers {
overrides["headers"].(map[string]string)[k] = v
}
options[0].Headers = nil
}
}
localUtils := b.connection.LocalUtils()
pipe, err := localUtils.channel.SendReturnAsDict("connect", options, overrides)
if err != nil {
return nil, err
}
jsonPipe := fromChannel(pipe["pipe"]).(*jsonPipe)
connection := newConnection(jsonPipe, localUtils)
playwright, err := connection.Start()
if err != nil {
return nil, err
}
playwright.setSelectors(b.playwright.Selectors)
browser := fromChannel(playwright.initializer["preLaunchedBrowser"]).(*browserImpl)
browser.shouldCloseConnectionOnClose = true
pipeClosed := func() {
for _, context := range browser.Contexts() {
pages := context.Pages()
for _, page := range pages {
page.(*pageImpl).onClose()
}
context.(*browserContextImpl).onClose()
}
browser.onClose()
connection.cleanup()
}
jsonPipe.On("closed", pipeClosed)
b.didLaunchBrowser(browser)
return browser, nil
}
func (b *browserTypeImpl) ConnectOverCDP(endpointURL string, options ...BrowserTypeConnectOverCDPOptions) (Browser, error) {
overrides := map[string]any{
"endpointURL": endpointURL,
}
// timeout is required in Playwright v1.57+ protocol
if len(options) == 0 || options[0].Timeout == nil {
overrides["timeout"] = float64(30000) // default 30s
}
if len(options) == 1 {
if options[0].Headers != nil {
overrides["headers"] = serializeMapToNameAndValue(options[0].Headers)
options[0].Headers = nil
}
}
response, err := b.channel.SendReturnAsDict("connectOverCDP", options, overrides)
if err != nil {
return nil, err
}
browser := fromChannel(response["browser"]).(*browserImpl)
b.didLaunchBrowser(browser)
if defaultContext, ok := response["defaultContext"]; ok {
context := fromChannel(defaultContext).(*browserContextImpl)
b.didCreateContext(context, nil, nil)
}
return browser, nil
}
func (b *browserTypeImpl) didCreateContext(context *browserContextImpl, contextOptions *BrowserNewContextOptions, tracesDir *string) {
context.setOptions(contextOptions, tracesDir)
}
func (b *browserTypeImpl) didLaunchBrowser(browser *browserImpl) {
browser.browserType = b
}
func newBrowserType(parent *channelOwner, objectType string, guid string, initializer map[string]any) *browserTypeImpl {
bt := &browserTypeImpl{}
bt.createChannelOwner(bt, parent, objectType, guid, initializer)
return bt
}