-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.go
139 lines (107 loc) · 3.54 KB
/
browser.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 wee
import (
"fmt"
"log"
"strings"
"time"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/launcher/flags"
"github.com/gookit/goutil/fsutil"
"github.com/k0kubun/pp/v3"
"github.com/samber/lo"
)
const (
// SlowMotionMillis is the default slow motion duration in milliseconds
SlowMotionMillis = 500
)
// PaintRects is a flag to show paint rectangles in the browser
const PaintRects = "--show-paint-rects"
func NewUserMode(opts ...BrowserOptionFunc) (*launcher.Launcher, *rod.Browser) {
opt := BrowserOptions{}
bindBrowserOptions(&opt, opts...)
lch, wsURL := newUserModeLauncher(opts...)
browser := rod.New().ControlURL(wsURL).MustConnect().NoDefaultDevice()
log.Printf("running in user-mode with user-data-dir:(%s)", opt.userDataDir)
return lch, browser
}
func NewBrowser(opts ...BrowserOptionFunc) (*launcher.Launcher, *rod.Browser) {
opt := BrowserOptions{slowMotionDelay: SlowMotionMillis, noDefaultDevice: true}
bindBrowserOptions(&opt, opts...)
lnchr := NewLauncher(opts...)
brw := rod.New().ControlURL(lnchr.MustLaunch()).MustConnect()
if opt.noDefaultDevice {
brw.NoDefaultDevice()
}
if opt.incognito {
brw.MustIncognito()
}
// just ignore cert errors
if opt.ignoreCertErrors {
_ = brw.IgnoreCertErrors(opt.ignoreCertErrors)
}
brw.SlowMotion(time.Millisecond * time.Duration(opt.slowMotionDelay))
return lnchr, brw
}
func NewLauncher(opts ...BrowserOptionFunc) *launcher.Launcher {
opt := BrowserOptions{}
bindBrowserOptions(&opt, opts...)
lnchr := launcher.New()
setLauncher(lnchr, opt.headless)
// for _, extFolder := range opt.extensions {
lnchr.Set("load-extension", strings.Join(opt.extensions, ","))
// }
if dir := opt.userDataDir; dir != "" {
lnchr.UserDataDir(fsutil.Expand(dir))
}
if opt.paintRects {
lnchr.Set(PaintRects)
}
if len(opt.flags) != 0 {
lo.ForEach(opt.flags, func(item string, i int) {
lnchr.Set(flags.Flag(item))
})
}
if proxy := opt.proxy; proxy != "" {
lnchr.Proxy(proxy)
}
return lnchr
}
func newUserModeLauncher(opts ...BrowserOptionFunc) (*launcher.Launcher, string) {
opt := BrowserOptions{}
bindBrowserOptions(&opt, opts...)
launch := launcher.NewUserMode()
if dir := opt.userDataDir; dir != "" {
launch = launch.UserDataDir(fsutil.Expand(dir))
pp.Println("set user-data-dir to:", dir)
}
launch.Leakless(opt.leakless)
wsURL, err := launch.Launch()
if err != nil {
s := fmt.Sprintf("%s", err)
if strings.Contains(s, "[launcher] Failed to get the debug url: Opening in existing browser session") {
fmt.Printf("%[1]s\nlaunch chrome browser failed, please make sure chrome is closed, and then run again\n%[1]s\n", strings.Repeat("=", 32)) //nolint
}
log.Fatalf("cannot launch browser: %v", err)
}
return launch, wsURL
}
func setLauncher(client *launcher.Launcher, headless bool) {
// Delete("use-mock-keychain"). if add this, a popup with message: "chromium wants to use your confidential information" will shown, and you have to manually confirm it.
client.
Set("no-sandbox").
Set("no-first-run").
Set("no-startup-window").
Set("disable-blink-features", "AutomationControlled").
// Set("disable-gpu").
Set("disable-dev-shm-usage").
// Set("disable-web-security").
Set("disable-infobars").
Set("enable-automation").
Headless(headless)
}
// func loadProxyExtension(l *launcher.Launcher, proxyLine string) {
// extensionFolder, _ := NewChromeExtension(proxyLine, "/tmp")
// l.Set("load-extension", extensionFolder)
// log.Info().Str("extension_folder", extensionFolder).Msg("load proxy extension")
// }