feat: initial commit
This commit is contained in:
100
pkg/browser/lorca/browser.go
Normal file
100
pkg/browser/lorca/browser.go
Normal file
@ -0,0 +1,100 @@
|
||||
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("", "", b.opts.Width, b.opts.Height, 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{}
|
40
pkg/browser/lorca/options.go
Normal file
40
pkg/browser/lorca/options.go
Normal file
@ -0,0 +1,40 @@
|
||||
package lorca
|
||||
|
||||
type Options struct {
|
||||
Width int
|
||||
Height int
|
||||
ChromeArgs []string
|
||||
}
|
||||
|
||||
type OptionsFunc func(opts *Options)
|
||||
|
||||
var DefaultChromeArgs = []string{
|
||||
"--remote-allow-origins=*",
|
||||
}
|
||||
|
||||
func NewOptions(funcs ...OptionsFunc) *Options {
|
||||
opts := &Options{
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
ChromeArgs: DefaultChromeArgs,
|
||||
}
|
||||
|
||||
for _, fn := range funcs {
|
||||
fn(opts)
|
||||
}
|
||||
|
||||
return opts
|
||||
}
|
||||
|
||||
func WithWindowSize(width, height int) OptionsFunc {
|
||||
return func(opts *Options) {
|
||||
opts.Width = width
|
||||
opts.Height = height
|
||||
}
|
||||
}
|
||||
|
||||
func WithAdditionalChromeArgs(args ...string) OptionsFunc {
|
||||
return func(opts *Options) {
|
||||
opts.ChromeArgs = append(args, DefaultChromeArgs...)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user