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,18 +6,20 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/playwright-community/playwright-go"
|
"github.com/playwright-community/playwright-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
type playWrightBrowser struct {
|
type playWrightBrowser struct {
|
||||||
pw *playwright.Playwright
|
pw *playwright.Playwright
|
||||||
browser playwright.Browser
|
browser playwright.Browser
|
||||||
ctx playwright.BrowserContext
|
ctx playwright.BrowserContext
|
||||||
userAgent string
|
userAgent string
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
cookieJar CookieJar
|
cookieJar CookieJar
|
||||||
|
serverAddr string
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Browser = playWrightBrowser{}
|
var _ Browser = playWrightBrowser{}
|
||||||
@@ -53,6 +55,10 @@ type PlayWrightBrowserOptions struct {
|
|||||||
|
|
||||||
Dimensions Size
|
Dimensions Size
|
||||||
DarkMode bool
|
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 {
|
func cookieToPlaywrightOptionalCookie(cookie Cookie) playwright.OptionalCookie {
|
||||||
@@ -80,10 +86,11 @@ func playwrightCookieToCookie(cookie playwright.Cookie) Cookie {
|
|||||||
func NewPlayWrightBrowser(opts ...PlayWrightBrowserOptions) (Browser, error) {
|
func NewPlayWrightBrowser(opts ...PlayWrightBrowserOptions) (Browser, error) {
|
||||||
var thirtySeconds = 30 * time.Second
|
var thirtySeconds = 30 * time.Second
|
||||||
opt := PlayWrightBrowserOptions{
|
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,
|
Browser: PlayWrightBrowserSelectionFirefox,
|
||||||
Timeout: &thirtySeconds,
|
Timeout: &thirtySeconds,
|
||||||
DarkMode: false,
|
DarkMode: false,
|
||||||
|
PlayWrightServerAddress: os.Getenv("PLAYWRIGHT_SERVER_ADDRESS"),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
@@ -140,12 +147,20 @@ func NewPlayWrightBrowser(opts ...PlayWrightBrowserOptions) (Browser, error) {
|
|||||||
default:
|
default:
|
||||||
return nil, ErrInvalidBrowserSelection
|
return nil, ErrInvalidBrowserSelection
|
||||||
}
|
}
|
||||||
|
var browser playwright.Browser
|
||||||
|
|
||||||
browser, err := bt.Launch(playwright.BrowserTypeLaunchOptions{
|
if opt.PlayWrightServerAddress != "" {
|
||||||
Headless: playwright.Bool(!opt.ShowBrowser),
|
browser, err = bt.Connect(opt.PlayWrightServerAddress, playwright.BrowserTypeConnectOptions{})
|
||||||
})
|
if err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
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
|
var viewport *playwright.Size
|
||||||
@@ -193,12 +208,13 @@ func NewPlayWrightBrowser(opts ...PlayWrightBrowserOptions) (Browser, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return playWrightBrowser{
|
return playWrightBrowser{
|
||||||
pw: pw,
|
pw: pw,
|
||||||
browser: browser,
|
browser: browser,
|
||||||
userAgent: opt.UserAgent,
|
userAgent: opt.UserAgent,
|
||||||
timeout: *opt.Timeout,
|
timeout: *opt.Timeout,
|
||||||
cookieJar: opt.CookieJar,
|
cookieJar: opt.CookieJar,
|
||||||
ctx: c,
|
ctx: c,
|
||||||
|
serverAddr: opt.PlayWrightServerAddress,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user