github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/options.go (about) 1 package cdp 2 3 import ( 4 "github.com/MontFerret/ferret/pkg/drivers" 5 ) 6 7 type ( 8 Options struct { 9 *drivers.Options 10 Address string 11 KeepCookies bool 12 Connection *ConnectionOptions 13 } 14 15 ConnectionOptions struct { 16 BufferSize int 17 Compression bool 18 } 19 20 Option func(opts *Options) 21 ) 22 23 const ( 24 DefaultAddress = "http://127.0.0.1:9222" 25 DefaultBufferSize = 1048562 26 ) 27 28 func NewOptions(setters []Option) *Options { 29 opts := new(Options) 30 opts.Options = new(drivers.Options) 31 opts.Name = DriverName 32 opts.Address = DefaultAddress 33 opts.Connection = &ConnectionOptions{ 34 BufferSize: DefaultBufferSize, 35 Compression: true, 36 } 37 38 for _, setter := range setters { 39 setter(opts) 40 } 41 42 return opts 43 } 44 45 func WithAddress(address string) Option { 46 return func(opts *Options) { 47 if address != "" { 48 opts.Address = address 49 } 50 } 51 } 52 53 func WithProxy(address string) Option { 54 return func(opts *Options) { 55 drivers.WithProxy(address)(opts.Options) 56 } 57 } 58 59 func WithUserAgent(value string) Option { 60 return func(opts *Options) { 61 drivers.WithUserAgent(value)(opts.Options) 62 } 63 } 64 65 func WithKeepCookies() Option { 66 return func(opts *Options) { 67 opts.KeepCookies = true 68 } 69 } 70 71 func WithCustomName(name string) Option { 72 return func(opts *Options) { 73 drivers.WithCustomName(name)(opts.Options) 74 } 75 } 76 77 func WithHeader(name string, header []string) Option { 78 return func(opts *Options) { 79 drivers.WithHeader(name, header)(opts.Options) 80 } 81 } 82 83 func WithHeaders(headers *drivers.HTTPHeaders) Option { 84 return func(opts *Options) { 85 drivers.WithHeaders(headers)(opts.Options) 86 } 87 } 88 89 func WithCookie(cookie drivers.HTTPCookie) Option { 90 return func(opts *Options) { 91 drivers.WithCookie(cookie)(opts.Options) 92 } 93 } 94 95 func WithCookies(cookies []drivers.HTTPCookie) Option { 96 return func(opts *Options) { 97 drivers.WithCookies(cookies)(opts.Options) 98 } 99 } 100 101 func WithBufferSize(size int) Option { 102 return func(opts *Options) { 103 opts.Connection.BufferSize = size 104 } 105 } 106 107 func WithCompression() Option { 108 return func(opts *Options) { 109 opts.Connection.Compression = true 110 } 111 } 112 113 func WithNoCompression() Option { 114 return func(opts *Options) { 115 opts.Connection.Compression = false 116 } 117 }