...

Source file src/github.com/playwright-community/playwright-go/browser_type.go

Documentation: github.com/playwright-community/playwright-go

     1  package playwright
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  type browserTypeImpl struct {
     8  	channelOwner
     9  }
    10  
    11  func (b *browserTypeImpl) Name() string {
    12  	return b.initializer["name"].(string)
    13  }
    14  
    15  func (b *browserTypeImpl) ExecutablePath() string {
    16  	return b.initializer["executablePath"].(string)
    17  }
    18  
    19  func (b *browserTypeImpl) Launch(options ...BrowserTypeLaunchOptions) (Browser, error) {
    20  	overrides := map[string]interface{}{}
    21  	if len(options) == 1 && options[0].Env != nil {
    22  		overrides["env"] = serializeMapToNameAndValue(options[0].Env)
    23  		options[0].Env = nil
    24  	}
    25  	channel, err := b.channel.Send("launch", overrides, options)
    26  	if err != nil {
    27  		return nil, fmt.Errorf("could not send message: %w", err)
    28  	}
    29  	return fromChannel(channel).(*browserImpl), nil
    30  }
    31  
    32  func (b *browserTypeImpl) LaunchPersistentContext(userDataDir string, options ...BrowserTypeLaunchPersistentContextOptions) (BrowserContext, error) {
    33  	overrides := map[string]interface{}{
    34  		"userDataDir": userDataDir,
    35  	}
    36  	if len(options) == 1 {
    37  		if options[0].ExtraHttpHeaders != nil {
    38  			overrides["extraHTTPHeaders"] = serializeMapToNameAndValue(options[0].ExtraHttpHeaders)
    39  		}
    40  		if options[0].Env != nil {
    41  			overrides["env"] = serializeMapToNameAndValue(options[0].Env)
    42  			options[0].Env = nil
    43  		}
    44  		if options[0].NoViewport != nil && *options[0].NoViewport {
    45  			overrides["noDefaultViewport"] = true
    46  			options[0].NoViewport = nil
    47  		}
    48  	}
    49  	channel, err := b.channel.Send("launchPersistentContext", overrides, options)
    50  	if err != nil {
    51  		return nil, fmt.Errorf("could not send message: %w", err)
    52  	}
    53  	return fromChannel(channel).(*browserContextImpl), nil
    54  }
    55  func (b *browserTypeImpl) Connect(url string, options ...BrowserTypeConnectOptions) (Browser, error) {
    56  	overrides := map[string]interface{}{
    57  		"wsEndpoint": url,
    58  	}
    59  	pipe, err := b.channel.Send("connect", overrides, options)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	jsonPipe := fromChannel(pipe).(*jsonPipe)
    64  	connection := newConnection(jsonPipe.Close)
    65  	connection.isRemote = true
    66  	var browser *browserImpl
    67  	pipeClosed := func() {
    68  		for _, context := range browser.contexts {
    69  			pages := context.(*browserContextImpl).pages
    70  			for _, page := range pages {
    71  				page.(*pageImpl).onClose()
    72  			}
    73  			context.(*browserContextImpl).onClose()
    74  		}
    75  		browser.onClose()
    76  	}
    77  	jsonPipe.On("closed", pipeClosed)
    78  	connection.onmessage = func(message map[string]interface{}) error {
    79  		if err := jsonPipe.Send(message); err != nil {
    80  			pipeClosed()
    81  			return err
    82  		}
    83  		return nil
    84  	}
    85  	jsonPipe.On("message", connection.Dispatch)
    86  	playwright := connection.Start()
    87  	browser = fromChannel(playwright.initializer["preLaunchedBrowser"]).(*browserImpl)
    88  	browser.isConnectedOverWebSocket = true
    89  	return browser, nil
    90  }
    91  func (b *browserTypeImpl) ConnectOverCDP(endpointURL string, options ...BrowserTypeConnectOverCDPOptions) (Browser, error) {
    92  	overrides := map[string]interface{}{
    93  		"endpointURL": endpointURL,
    94  	}
    95  	response, err := b.channel.SendReturnAsDict("connectOverCDP", overrides, options)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  	browser := fromChannel(response.(map[string]interface{})["browser"]).(*browserImpl)
   100  	if defaultContext, ok := response.(map[string]interface{})["defaultContext"]; ok {
   101  		context := fromChannel(defaultContext).(*browserContextImpl)
   102  		browser.contexts = append(browser.contexts, context)
   103  		context.browser = browser
   104  	}
   105  	return browser, nil
   106  }
   107  
   108  func newBrowserType(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *browserTypeImpl {
   109  	bt := &browserTypeImpl{}
   110  	bt.createChannelOwner(bt, parent, objectType, guid, initializer)
   111  	return bt
   112  }
   113  

View as plain text