-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchrome.go
203 lines (170 loc) · 5.41 KB
/
chrome.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
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
202
203
package chrome
import (
"context"
"fmt"
"io"
"log"
"strconv"
"strings"
"sync"
"time"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/cdproto/target"
"github.com/chromedp/chromedp"
)
type Chrome struct {
url string
useragent string
width int
height int
proxy string
debugger *log.Logger
flags []chromedp.ExecAllocatorOption
ctxOpts []chromedp.ContextOption
actions []chromedp.Action
mu sync.Mutex
ctx context.Context
cancel chan struct{}
done chan struct{}
}
func New(url string) *Chrome {
c := &Chrome{url: url, debugger: log.New(io.Discard, "", log.LstdFlags), cancel: make(chan struct{})}
return c.AddContextOptions(chromedp.WithDebugf(c.debugger.Printf))
}
func (c *Chrome) headless() *Chrome {
return c.AddFlags(
chromedp.Flag("headless", "new"),
chromedp.Flag("hide-scrollbars", true),
chromedp.Flag("mute-audio", true),
)
}
func UserAgent() (userAgent string) {
c := New("").headless().NoSandbox()
defer c.Close()
ctx, cancel := context.WithTimeout(c, time.Minute)
defer cancel()
if err := chromedp.Run(ctx, chromedp.Evaluate("navigator.userAgent", &userAgent)); err != nil {
if err == context.Canceled {
err = context.Cause(ctx)
}
panic("failed to get chrome useragent: " + err.Error())
}
userAgent = strings.ReplaceAll(userAgent, "Headless", "")
return
}
func Headless() *Chrome {
return New("").UserAgent(UserAgent()).headless()
}
func Headful() *Chrome {
return New("")
}
func Remote(url string) *Chrome {
if url == "" {
panic("empty url")
}
return New(url)
}
func Local(port int) *Chrome {
if port <= 0 || port > 65535 {
panic("invalid port number: " + strconv.Itoa(port))
}
return Remote(fmt.Sprintf("ws://localhost:%d", port))
}
func (c *Chrome) SetDebuggerOutput(w io.Writer) *Chrome {
c.debugger.SetOutput(w)
return c
}
func (c *Chrome) SetDebuggerPrefix(prefix string) *Chrome {
c.debugger.SetPrefix(prefix)
return c
}
func (c *Chrome) SetDebuggerFlags(flag int) *Chrome {
c.debugger.SetFlags(flag)
return c
}
func (c *Chrome) UserAgent(useragent string) *Chrome {
c.useragent = useragent
return c
}
func (c *Chrome) WindowSize(width int, height int) *Chrome {
c.width, c.height = width, height
return c
}
func (c *Chrome) Proxy(proxy string) *Chrome {
c.proxy = proxy
return c
}
func (c *Chrome) AddFlags(flags ...chromedp.ExecAllocatorOption) *Chrome {
c.flags = append(c.flags, flags...)
return c
}
func (c *Chrome) AutoOpenDevtools() *Chrome {
return c.AddFlags(chromedp.Flag("auto-open-devtools-for-tabs", true))
}
func (c *Chrome) Incognito() *Chrome {
return c.AddFlags(chromedp.Flag("incognito", true))
}
func (c *Chrome) Guest() *Chrome {
return c.AddFlags(chromedp.Flag("guest", true))
}
func (c *Chrome) NoSandbox() *Chrome {
return c.AddFlags(chromedp.Flag("no-sandbox", true))
}
func (c *Chrome) DisableUserAgentClientHint() *Chrome {
return c.AddFlags(chromedp.Flag("disable-features", "UserAgentClientHint"))
}
func (c *Chrome) DisableAutomationControlled() *Chrome {
return c.AddFlags(chromedp.Flag("disable-blink-features", "AutomationControlled"))
}
func (c *Chrome) AddContextOptions(opts ...chromedp.ContextOption) *Chrome {
c.ctxOpts = append(c.ctxOpts, opts...)
return c
}
func (c *Chrome) AddActions(actions ...chromedp.Action) *Chrome {
c.actions = append(c.actions, actions...)
return c
}
var DefaultExecAllocatorOptions = [...]chromedp.ExecAllocatorOption{
// https://github.com/puppeteer/puppeteer/blob/main/packages/puppeteer-core/src/node/ChromeLauncher.ts
chromedp.Flag("allow-pre-commit-input", true),
chromedp.Flag("disable-background-networking", true),
chromedp.Flag("disable-background-timer-throttling", true),
chromedp.Flag("disable-backgrounding-occluded-windows", true),
chromedp.Flag("disable-breakpad", true),
chromedp.Flag("disable-client-side-phishing-detection", true),
chromedp.Flag("disable-component-extensions-with-background-pages", true),
chromedp.Flag("disable-component-update", true),
chromedp.Flag("disable-default-apps", true),
chromedp.Flag("disable-dev-shm-usage", true),
chromedp.Flag("disable-extensions", true),
chromedp.Flag("disable-hang-monitor", true),
chromedp.Flag("disable-infobars", true),
chromedp.Flag("disable-ipc-flooding-protection", true),
chromedp.Flag("disable-popup-blocking", true),
chromedp.Flag("disable-prompt-on-repost", true),
chromedp.Flag("disable-renderer-backgrounding", true),
chromedp.Flag("disable-search-engine-choice-screen", true),
chromedp.Flag("disable-sync", true),
chromedp.Flag("enable-automation", true),
chromedp.Flag("export-tagged-pdf", true),
chromedp.Flag("generate-pdf-document-outline", true),
// chromedp.Flag("force-color-profile", "srgb"),
chromedp.Flag("metrics-recording-only", true),
chromedp.Flag("no-first-run", true),
chromedp.Flag("password-store", "basic"),
chromedp.Flag("use-mock-keychain", true),
chromedp.Flag("disable-features", "Translate,AcceptCHFrame,MediaRouter,OptimizationHints,ProcessPerSiteUpToMainFrameThreshold,IsolateSandboxedIframes"),
chromedp.Flag("start-maximized", true),
}
func (c *Chrome) Run(actions ...chromedp.Action) error {
return chromedp.Run(c, actions...)
}
func (c *Chrome) WaitNewTarget(fn func(*target.Info) bool) <-chan target.ID {
return chromedp.WaitNewTarget(c, fn)
}
func AddScriptToEvaluateOnNewDocument(script string) chromedp.Action {
return chromedp.ActionFunc(func(ctx context.Context) (err error) {
_, err = page.AddScriptToEvaluateOnNewDocument(script).Do(ctx)
return
})
}