77 lines
1.7 KiB
Go
Raw Permalink Normal View History

package browser
import (
"context"
"time"
"github.com/urfave/cli/v3"
"gitea.stevedudenhoeffer.com/steve/go-extractor"
)
type BrowserFlags []cli.Flag
var Flags = BrowserFlags{
&cli.StringFlag{
Name: "user-agent",
Aliases: []string{"ua"},
Usage: "User-Agent to use for requests",
DefaultText: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0",
},
&cli.StringFlag{
Name: "timeout",
Aliases: []string{"t"},
Usage: "Timeout for requests",
DefaultText: "30s",
},
&cli.StringFlag{
Name: "browser",
Aliases: []string{"b"},
Usage: "Browser to use, one of: chromium, firefox, webkit",
DefaultText: "firefox",
},
&cli.StringFlag{
Name: "cookies-file",
Aliases: []string{"c"},
Usage: "cookies.txt file to load cookies from",
DefaultText: "",
},
&cli.BoolFlag{
Name: "visible",
Usage: "If set, the browser will be visible, if not set, the browser will be headless",
DefaultText: "false",
},
}
func FromCommand(_ context.Context, cmd *cli.Command) (extractor.Browser, error) {
var opts extractor.PlayWrightBrowserOptions
if ua := cmd.String("user-agent"); ua != "" {
opts.UserAgent = ua
}
if to := cmd.String("timeout"); to != "" {
d, err := time.ParseDuration(to)
if err != nil {
return nil, err
}
opts.Timeout = &d
}
if b := cmd.String("browser"); b != "" {
opts.Browser = extractor.PlayWrightBrowserSelection(b)
}
if cf := cmd.String("cookies-file"); cf != "" {
cookies, err := extractor.LoadCookiesFile(cf)
if err != nil {
return nil, err
}
opts.CookieJar = cookies
}
opts.ShowBrowser = cmd.Bool("visible")
return extractor.NewPlayWrightBrowser(opts)
}