refactor: move browser device options to pkg/uixt/option pkg

This commit is contained in:
lilong.129
2025-03-03 21:40:50 +08:00
parent 69d06a7215
commit b687abb3c8
4 changed files with 30 additions and 20 deletions

View File

@@ -11,38 +11,34 @@ import (
)
type BrowserDevice struct {
BrowserId string `json:"browser_id,omitempty" yaml:"browser_id,omitempty"`
LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"`
Options *option.BrowserDeviceOptions
}
type BrowserDeviceOption func(*BrowserDevice)
func WithBrowserId(serial string) BrowserDeviceOption {
return func(device *BrowserDevice) {
device.BrowserId = serial
}
}
func NewBrowserDevice(options ...BrowserDeviceOption) (device *BrowserDevice, err error) {
device = &BrowserDevice{}
for _, option := range options {
option(device)
func NewBrowserDevice(opts ...option.BrowserDeviceOption) (device *BrowserDevice, err error) {
options := &option.BrowserDeviceOptions{}
for _, option := range opts {
option(options)
}
if device.BrowserId == "" {
if options.BrowserID == "" {
browserInfo, err := CreateBrowser(3600)
if err != nil {
log.Error().Err(err).Msg("failed to create browser")
return nil, err
}
device.BrowserId = browserInfo.ContextId
options.BrowserID = browserInfo.ContextId
}
device = &BrowserDevice{
Options: options,
}
log.Info().Str("browserID", device.Options.BrowserID).Msg("init browser device")
return device, nil
}
func (dev *BrowserDevice) UUID() string {
return dev.BrowserId
return dev.Options.BrowserID
}
func (dev *BrowserDevice) Setup() error {
@@ -50,7 +46,7 @@ func (dev *BrowserDevice) Setup() error {
}
func (dev *BrowserDevice) LogEnabled() bool {
return dev.LogOn
return dev.Options.LogOn
}
func (dev *BrowserDevice) Teardown() error {

View File

@@ -0,0 +1,14 @@
package option
type BrowserDeviceOptions struct {
BrowserID string `json:"browser_id,omitempty" yaml:"browser_id,omitempty"`
LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"`
}
type BrowserDeviceOption func(*BrowserDeviceOptions)
func WithBrowserID(serial string) BrowserDeviceOption {
return func(device *BrowserDeviceOptions) {
device.BrowserID = serial
}
}