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"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"log/slog"
 | 
						"log/slog"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/playwright-community/playwright-go"
 | 
						"github.com/playwright-community/playwright-go"
 | 
				
			||||||
@@ -18,6 +19,7 @@ type playWrightBrowser struct {
 | 
				
			|||||||
	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,13 +147,21 @@ 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 != "" {
 | 
				
			||||||
 | 
							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),
 | 
								Headless: playwright.Bool(!opt.ShowBrowser),
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var viewport *playwright.Size
 | 
						var viewport *playwright.Size
 | 
				
			||||||
	if opt.Dimensions.Width > 0 && opt.Dimensions.Height > 0 {
 | 
						if opt.Dimensions.Width > 0 && opt.Dimensions.Height > 0 {
 | 
				
			||||||
@@ -199,6 +214,7 @@ func NewPlayWrightBrowser(opts ...PlayWrightBrowserOptions) (Browser, error) {
 | 
				
			|||||||
		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