github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/helpers.go (about)

     1  package cdp
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/mafredri/cdp/protocol/dom"
     7  
     8  	"github.com/MontFerret/ferret/pkg/runtime/events"
     9  
    10  	"github.com/mafredri/cdp"
    11  	"github.com/mafredri/cdp/protocol/emulation"
    12  	"github.com/mafredri/cdp/protocol/network"
    13  	"github.com/mafredri/cdp/protocol/page"
    14  	"golang.org/x/sync/errgroup"
    15  
    16  	"github.com/MontFerret/ferret/pkg/drivers"
    17  	"github.com/MontFerret/ferret/pkg/drivers/common"
    18  )
    19  
    20  type (
    21  	batchFunc = func() error
    22  
    23  	closer func(ctx context.Context) error
    24  
    25  	pageNavigationEventStream struct {
    26  		stream events.Stream
    27  		closer
    28  	}
    29  )
    30  
    31  func newPageNavigationEventStream(stream events.Stream, closer closer) events.Stream {
    32  	return &pageNavigationEventStream{stream, closer}
    33  }
    34  
    35  func (p *pageNavigationEventStream) Close(ctx context.Context) error {
    36  	if err := p.stream.Close(ctx); err != nil {
    37  		return err
    38  	}
    39  
    40  	return p.closer(ctx)
    41  }
    42  
    43  func (p *pageNavigationEventStream) Read(ctx context.Context) <-chan events.Message {
    44  	return p.stream.Read(ctx)
    45  }
    46  
    47  func runBatch(funcs ...batchFunc) error {
    48  	eg := errgroup.Group{}
    49  
    50  	for _, f := range funcs {
    51  		eg.Go(f)
    52  	}
    53  
    54  	return eg.Wait()
    55  }
    56  
    57  func enableFeatures(ctx context.Context, client *cdp.Client, params drivers.Params) error {
    58  	if err := client.Page.Enable(ctx); err != nil {
    59  		return err
    60  	}
    61  
    62  	return runBatch(
    63  		func() error {
    64  			return client.Page.SetLifecycleEventsEnabled(
    65  				ctx,
    66  				page.NewSetLifecycleEventsEnabledArgs(true),
    67  			)
    68  		},
    69  
    70  		func() error {
    71  			return client.DOM.Enable(ctx, dom.NewEnableArgs().SetIncludeWhitespace("all"))
    72  		},
    73  
    74  		func() error {
    75  			return client.Runtime.Enable(ctx)
    76  		},
    77  
    78  		func() error {
    79  			ua := common.GetUserAgent(params.UserAgent)
    80  
    81  			// do not use custom user agent
    82  			if ua == "" {
    83  				return nil
    84  			}
    85  
    86  			return client.Emulation.SetUserAgentOverride(
    87  				ctx,
    88  				emulation.NewSetUserAgentOverrideArgs(ua),
    89  			)
    90  		},
    91  
    92  		func() error {
    93  			return client.Network.Enable(ctx, network.NewEnableArgs())
    94  		},
    95  
    96  		func() error {
    97  			return client.Page.SetBypassCSP(ctx, page.NewSetBypassCSPArgs(true))
    98  		},
    99  
   100  		func() error {
   101  			if params.Viewport == nil {
   102  				return nil
   103  			}
   104  
   105  			orientation := emulation.ScreenOrientation{}
   106  
   107  			if !params.Viewport.Landscape {
   108  				orientation.Type = "portraitPrimary"
   109  				orientation.Angle = 0
   110  			} else {
   111  				orientation.Type = "landscapePrimary"
   112  				orientation.Angle = 90
   113  			}
   114  
   115  			scaleFactor := params.Viewport.ScaleFactor
   116  
   117  			if scaleFactor <= 0 {
   118  				scaleFactor = 1
   119  			}
   120  
   121  			deviceArgs := emulation.NewSetDeviceMetricsOverrideArgs(
   122  				params.Viewport.Width,
   123  				params.Viewport.Height,
   124  				scaleFactor,
   125  				params.Viewport.Mobile,
   126  			).SetScreenOrientation(orientation)
   127  
   128  			return client.Emulation.SetDeviceMetricsOverride(
   129  				ctx,
   130  				deviceArgs,
   131  			)
   132  		},
   133  	)
   134  }