Add support for connecting to a remote Playwright server
Introduced `PlayWrightServerAddress` to `PlayWrightBrowserOptions`, allowing the browser to connect to a remote Playwright server if specified. Defaults to the `PLAYWRIGHT_SERVER_ADDRESS` environment variable. Updated initialization logic to handle both local launches and remote connections seamlessly.
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/playwright-community/playwright-go"
|
||||
@@ -18,6 +19,7 @@ type playWrightBrowser struct {
|
||||
userAgent string
|
||||
timeout time.Duration
|
||||
cookieJar CookieJar
|
||||
serverAddr string
|
||||
}
|
||||
|
||||
var _ Browser = playWrightBrowser{}
|
||||
@@ -53,6 +55,10 @@ type PlayWrightBrowserOptions struct {
|
||||
|
||||
Dimensions Size
|
||||
DarkMode bool
|
||||
|
||||
// PlayWrightServerAddress is the address of a PlayWright server to connect to.
|
||||
// Defaults to the value of the environment variable PLAYWRIGHT_SERVER_ADDRESS.
|
||||
PlayWrightServerAddress string
|
||||
}
|
||||
|
||||
func cookieToPlaywrightOptionalCookie(cookie Cookie) playwright.OptionalCookie {
|
||||
@@ -80,10 +86,11 @@ func playwrightCookieToCookie(cookie playwright.Cookie) Cookie {
|
||||
func NewPlayWrightBrowser(opts ...PlayWrightBrowserOptions) (Browser, error) {
|
||||
var thirtySeconds = 30 * time.Second
|
||||
opt := PlayWrightBrowserOptions{
|
||||
UserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:137.0) Gecko/20100101 Firefox/137.0",
|
||||
UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0",
|
||||
Browser: PlayWrightBrowserSelectionFirefox,
|
||||
Timeout: &thirtySeconds,
|
||||
DarkMode: false,
|
||||
PlayWrightServerAddress: os.Getenv("PLAYWRIGHT_SERVER_ADDRESS"),
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
@@ -140,13 +147,21 @@ func NewPlayWrightBrowser(opts ...PlayWrightBrowserOptions) (Browser, error) {
|
||||
default:
|
||||
return nil, ErrInvalidBrowserSelection
|
||||
}
|
||||
var browser playwright.Browser
|
||||
|
||||
browser, err := bt.Launch(playwright.BrowserTypeLaunchOptions{
|
||||
if opt.PlayWrightServerAddress != "" {
|
||||
browser, err = bt.Connect(opt.PlayWrightServerAddress, playwright.BrowserTypeConnectOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
browser, err = bt.Launch(playwright.BrowserTypeLaunchOptions{
|
||||
Headless: playwright.Bool(!opt.ShowBrowser),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var viewport *playwright.Size
|
||||
if opt.Dimensions.Width > 0 && opt.Dimensions.Height > 0 {
|
||||
@@ -199,6 +214,7 @@ func NewPlayWrightBrowser(opts ...PlayWrightBrowserOptions) (Browser, error) {
|
||||
timeout: *opt.Timeout,
|
||||
cookieJar: opt.CookieJar,
|
||||
ctx: c,
|
||||
serverAddr: opt.PlayWrightServerAddress,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user