-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.go
More file actions
193 lines (164 loc) · 4.36 KB
/
browser.go
File metadata and controls
193 lines (164 loc) · 4.36 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
package browser
import (
"errors"
"net/http"
"os"
"strings"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"github.com/sohaha/zlsgo/zfile"
"github.com/sohaha/zlsgo/zhttp"
"github.com/sohaha/zlsgo/zlog"
"github.com/sohaha/zlsgo/zutil"
)
type Browser struct {
err error
userAgent *proto.NetworkSetUserAgentOverride
log *zlog.Logger
launcher *launcher.Launcher
Browser *rod.Browser
client *zhttp.Engine
id string
after []func()
before []func()
cookies []*http.Cookie
options Options
isCustomWSEndpoint bool
canUserDir bool
}
func New(opts ...func(o *Options)) (browser *Browser, err error) {
browser = &Browser{
client: zhttp.New(),
log: zlog.New(),
}
browser.options = zutil.Optional(Options{
autoKill: true,
Headless: true,
// Stealth: true,
browser: browser,
Flags: map[string]string{
"no-sandbox": "",
"disable-blink-features": "AutomationControlled",
"no-default-browser-check": "",
"no-first-run": "",
// "disable-gpu": "",
// "no-startup-window": "",
"disable-component-update": "",
"window-position": "0,0",
},
IgnoreCertError: true,
}, opts...)
browser.Client().EnableCookie(true)
browser.canUserDir = browser.options.UserMode || browser.options.UserDataDir != ""
if err := browser.init(); err != nil {
return nil, err
}
return browser, nil
}
func (b *Browser) Headless(enable ...bool) (bool, error) {
headless := b.options.Headless
if len(enable) > 0 {
headless = enable[0]
}
if b.options.Headless == headless {
return headless, nil
}
if !b.isCustomWSEndpoint {
b.options.WSEndpoint = ""
}
b.options.Headless = headless
if b.launcher.PID() != 0 {
p, err := os.FindProcess(b.launcher.PID())
if err == nil {
_ = p.Kill()
}
}
return headless, b.init()
}
func (b *Browser) Kill() {
b.launcher.Kill()
}
func (b *Browser) NewIncognito() *Browser {
incognito, _ := b.Browser.Incognito()
browser := *b
browser.Browser = incognito
return &browser
}
func (b *Browser) Close() error {
if b.Browser == nil {
return nil
}
return b.Browser.Close()
}
func (b *Browser) Cleanup() {
if !b.canUserDir && b.options.UserDataDir != "" {
_ = zfile.Rmdir(b.options.UserDataDir)
}
}
func (b *Browser) Release() {
b.Cleanup()
}
// SetCookie set global cookies
func (b *Browser) SetCookies(cookies []*http.Cookie) error {
if cookies == nil {
b.cookies = make([]*http.Cookie, 0, 0)
_ = b.Browser.SetCookies(nil)
return nil
}
b.cookies = b.uniqueCookies(cookies)
c, err := b.cookiesToProto(cookies)
if err != nil {
return errors.New("failed to set cookie: " + err.Error())
}
b.Browser.SetCookies(c)
return nil
}
// GetCookie get global cookies
func (b *Browser) GetCookies() ([]*http.Cookie, error) {
protoCookies, err := b.Browser.GetCookies()
if err != nil {
return []*http.Cookie{}, err
}
cookies := make([]*http.Cookie, 0, len(protoCookies))
for i := range protoCookies {
value := protoCookies[i].Value
if strings.HasPrefix(value, "\"") && strings.HasSuffix(value, "\"") {
value = value[1 : len(value)-1]
}
cookie := http.Cookie{
Name: protoCookies[i].Name,
Value: value,
Path: protoCookies[i].Path,
Domain: protoCookies[i].Domain,
Secure: protoCookies[i].Secure,
HttpOnly: protoCookies[i].HTTPOnly,
}
if protoCookies[i].Expires > 0 {
cookie.Expires = protoCookies[i].Expires.Time()
}
cookies = append(cookies, &cookie)
}
return cookies, nil
}
func (b *Browser) cookiesToProto(cookies []*http.Cookie) ([]*proto.NetworkCookieParam, error) {
protoCookies := make([]*proto.NetworkCookieParam, 0, len(cookies))
for i := range cookies {
if cookies[i].Domain == "" {
return nil, errors.New("domain is required for cookie configuration")
}
if cookies[i].Name == "" {
return nil, errors.New("name is required for cookie configuration")
}
protoCookies = append(protoCookies, &proto.NetworkCookieParam{
Name: cookies[i].Name,
Value: cookies[i].Value,
Expires: proto.TimeSinceEpoch(cookies[i].Expires.Unix()),
Path: cookies[i].Path,
Domain: cookies[i].Domain,
Secure: cookies[i].Secure,
HTTPOnly: cookies[i].HttpOnly,
})
}
return protoCookies, nil
}