package lorca import ( "context" "forge.cadoles.com/arcad/arcast/pkg/browser" "github.com/pkg/errors" "github.com/zserge/lorca" "gitlab.com/wpetit/goweb/logger" ) type Browser struct { ui lorca.UI status browser.Status opts *Options } func (b *Browser) Start() error { logger.Debug(context.Background(), "starting browser", logger.F("opts", b.opts)) ui, err := lorca.New( lorca.WithWindowSize(b.opts.Width, b.opts.Height), lorca.WithAdditionalCustomArgs(b.opts.ChromeArgs...), ) if err != nil { return errors.WithStack(err) } b.ui = ui return nil } func (b *Browser) Stop() error { if err := b.ui.Close(); err != nil { return errors.WithStack(err) } b.ui = nil return nil } func (b *Browser) Wait() { <-b.ui.Done() } // Load implements browser.Browser. func (b *Browser) Load(url string) error { if err := b.ui.Load(url); err != nil { return errors.WithStack(err) } b.status = browser.StatusCasting return nil } // Unload implements browser.Browser. func (b *Browser) Reset(url string) error { if err := b.ui.Load(url); err != nil { return errors.WithStack(err) } b.status = browser.StatusIdle return nil } // Status implements browser.Browser. func (b *Browser) Status() (browser.Status, error) { return b.status, nil } // Title implements browser.Browser. func (b *Browser) Title() (string, error) { result := b.ui.Eval("document.title.toString()") if err := result.Err(); err != nil { return "", errors.WithStack(err) } return result.String(), nil } // URL implements browser.Browser. func (b *Browser) URL() (string, error) { result := b.ui.Eval("window.location.toString()") if err := result.Err(); err != nil { return "", errors.WithStack(err) } return result.String(), nil } func NewBrowser(funcs ...OptionsFunc) *Browser { opts := NewOptions(funcs...) return &Browser{ status: browser.StatusIdle, opts: opts, } } var _ browser.Browser = &Browser{}