forked from Davincible/chromedp-undetected
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
141 lines (114 loc) · 3.57 KB
/
config.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
package chromedpundetected
import (
"context"
"time"
"github.com/chromedp/chromedp"
)
const (
// DefaultNoSandbox enables the 'no-sandbox' flag by default.
DefaultNoSandbox = true
)
// Option is a functional option.
type Option func(*Config)
// Config is a undetected Chrome config.
type Config struct {
// Ctx is the base context to use. By default a background context will be used.
Ctx context.Context `json:"-" yaml:"-"`
// ContextOptions are chromedp context option.
ContextOptions []chromedp.ContextOption `json:"-" yaml:"-"`
// ChromeFlags are additional Chrome flags to pass to the browser.
//
// NOTE: adding additional flags can make the detection unstable, so test,
// and be careful of what flags you add. Mostly intended to configure things
// like a proxy. Also check if the flags you want to set are not already set
// by this library.
ChromeFlags []chromedp.ExecAllocatorOption
// UserDataDir is the path to the directory where Chrome user data is stored.
//
// By default a temporary directory will be used.
UserDataDir string `json:"userDataDir" yaml:"userDataDir"`
// LogLevel is the Chrome log level, 0 by default.
LogLevel int `json:"logLevel" yaml:"logLevel"`
// NoSandbox dictates whether the no-sanbox flag is added. Defaults to true.
NoSandbox bool `json:"noSandbox" yaml:"noSandbox"`
// ChromePath is a specific binary path for Chrome.
//
// By default the chrome or chromium on your PATH will be used.
ChromePath string `json:"chromePath" yaml:"chromePath"`
// Port is the Chrome debugger port. By default a random port will be used.
Port int `json:"port" yaml:"port"`
// Timeout is the context timeout.
Timeout time.Duration `json:"timeout" yaml:"timeout"`
// Headless dicates whether Chrome will start headless (without a visible window)
//
// It will NOT use the '--headless' option, rather it will use a virtual display.
// Requires Xvfb to be installed, only available on Linux.
Headless bool `json:"headless" yaml:"headless"`
// language to be used otherwise system/OS defaults are used
// https://developer.chrome.com/docs/webstore/i18n/#localeTable
Language string
}
// NewConfig creates a new config object with defaults.
func NewConfig(opts ...Option) Config {
c := Config{
NoSandbox: DefaultNoSandbox,
}
for _, o := range opts {
o(&c)
}
return c
}
// WithContext adds a base context.
func WithContext(ctx context.Context) Option {
return func(c *Config) {
c.Ctx = ctx
}
}
// WithUserDataDir sets the user data directory to a custom path.
func WithUserDataDir(dir string) Option {
return func(c *Config) {
c.UserDataDir = dir
}
}
// WithChromeBinary sets the chrome binary path.
func WithChromeBinary(path string) Option {
return func(c *Config) {
c.ChromePath = path
}
}
// WithTimeout sets the context timeout.
func WithTimeout(timeout time.Duration) Option {
return func(c *Config) {
c.Timeout = timeout
}
}
// WithHeadless creates a headless chrome instance.
func WithHeadless() Option {
return func(c *Config) {
c.Headless = true
}
}
// WithNoSandbox enable/disable sandbox. Disabled by default.
func WithNoSandbox(b bool) Option {
return func(c *Config) {
c.NoSandbox = b
}
}
// WithPort sets the chrome debugger port.
func WithPort(port int) Option {
return func(c *Config) {
c.Port = port
}
}
// WithLogLevel sets the chrome log level.
func WithLogLevel(level int) Option {
return func(c *Config) {
c.LogLevel = level
}
}
// WithChromeFlags add chrome flags.
func WithChromeFlags(opts ...chromedp.ExecAllocatorOption) Option {
return func(c *Config) {
c.ChromeFlags = append(c.ChromeFlags, opts...)
}
}