@@ -299,14 +299,11 @@ type ModCDPClient struct {
299299 Types * CDPTypes
300300 CDPURL string
301301 Launcher browserLauncherClient
302- Injector extensionInjector
302+ Injector * ExtensionInjector
303303 Upstream upstreamTransportClient
304304 handlers map [string ][]handlerEntry
305305 handlersMu sync.Mutex
306306 Router * AutoSessionRouter
307- ExtensionID string
308- ExtTargetID string
309- ExtSessionID string
310307 Latency map [string ]any
311308 ConnectTiming map [string ]any
312309 LastCommandTiming map [string ]any
@@ -419,9 +416,9 @@ func New(config Config) *ModCDPClient {
419416 client .Mod = ModDomain {client : client }
420417 client .Router = NewAutoSessionRouter (& upstream .UpstreamTransport , client .Types , config .Router )
421418 client .Launcher = client .browserLauncher ()
422- injectors := client .extensionInjectorsForConfig ()
419+ injectors , selectedInjector := client .extensionInjectorsForConfig ()
423420 if len (injectors ) > 0 {
424- client .Injector = injectors [ 0 ]
421+ client .Injector = selectedInjector
425422 client .extensionInjectors = injectors
426423 }
427424 if * client .Config .ClientConfig .ClientHydrateAliases {
@@ -438,8 +435,8 @@ func (c *ModCDPClient) ToJSON() map[string]any {
438435 if child , ok := c .Upstream .(types.ModCDPJSONChild ); ok {
439436 children ["upstream" ] = child
440437 }
441- if child , ok := c .Injector .(types. ModCDPJSONChild ); ok {
442- children ["injector" ] = child
438+ if c .Injector != nil {
439+ children ["injector" ] = c . Injector
443440 }
444441 if child , ok := any (c .Router ).(types.ModCDPJSONChild ); ok {
445442 children ["router" ] = child
@@ -588,24 +585,21 @@ func (c *ModCDPClient) Connect() error {
588585 return err
589586 }
590587 extensionStartedAt := time .Now ().UnixMilli ()
591- ext , err := c .injectExtension (c .extensionInjectors )
588+ ext , injectorState , err := c .injectExtension (c .extensionInjectors )
592589 if err != nil {
593590 c .Close ()
594591 return err
595592 }
596593 extensionCompletedAt := time .Now ().UnixMilli ()
597- c .ExtensionID = ext .ExtensionID
598- c .ExtTargetID = ext .TargetID
599- c .ExtSessionID = ext .SessionID
600- if ext .TargetID == "" || ext .SessionID == "" {
594+ if injectorState .TargetID == "" || injectorState .SessionID == "" {
601595 c .Close ()
602596 return fmt .Errorf ("%T did not record a ModCDP extension target" , c .Injector )
603597 }
604- if _ , err := c .Router .Send ("Runtime.enable" , map [string ]any {}, c . ExtSessionID ); err != nil {
598+ if _ , err := c .Router .Send ("Runtime.enable" , map [string ]any {}, injectorState . SessionID ); err != nil {
605599 c .Close ()
606600 return err
607601 }
608- if _ , err := c .Router .Send ("Runtime.addBinding" , map [string ]any {"name" : translate .CustomEventBindingName }, c . ExtSessionID ); err != nil {
602+ if _ , err := c .Router .Send ("Runtime.addBinding" , map [string ]any {"name" : translate .CustomEventBindingName }, injectorState . SessionID ); err != nil {
609603 c .Close ()
610604 return err
611605 }
@@ -614,7 +608,7 @@ func (c *ModCDPClient) Connect() error {
614608 mirrorUpstreamEvents = * c .Config .ClientConfig .ClientMirrorUpstreamEvents
615609 }
616610 if mirrorUpstreamEvents {
617- if _ , err := c .Router .Send ("Runtime.addBinding" , map [string ]any {"name" : translate .UpstreamEventBindingName }, c . ExtSessionID ); err != nil {
611+ if _ , err := c .Router .Send ("Runtime.addBinding" , map [string ]any {"name" : translate .UpstreamEventBindingName }, injectorState . SessionID ); err != nil {
618612 c .Close ()
619613 return err
620614 }
@@ -674,10 +668,10 @@ func (c *ModCDPClient) connectUpstreamTransport() error {
674668 }
675669 injectors := c .extensionInjectors
676670 if len (injectors ) == 0 && c .Config .Injector .InjectorMode != "none" {
677- injectors = c .extensionInjectorsForConfig ()
671+ injectors , selectedInjector : = c .extensionInjectorsForConfig ()
678672 c .extensionInjectors = injectors
679673 if len (injectors ) > 0 {
680- c .Injector = injectors [ 0 ]
674+ c .Injector = selectedInjector
681675 }
682676 }
683677 initialTransportConfig := c .upstreamTransportConfig ()
@@ -989,7 +983,7 @@ func (c *ModCDPClient) sendCommand(method string, params map[string]any, cdpSess
989983 preparation , err := c .Types .PrepareCommand (
990984 method ,
991985 params ,
992- method == "Mod.addCustomCommand" || ((method == "Mod.addCustomEvent" || method == "Mod.addMiddleware" ) && c . ExtSessionID == "" ),
986+ method == "Mod.addCustomCommand" || ((method == "Mod.addCustomEvent" || method == "Mod.addMiddleware" ) && ( c . Injector == nil || c . Injector . SessionID == "" ) ),
993987 )
994988 if err != nil {
995989 return nil , err
@@ -1075,14 +1069,14 @@ func (c *ModCDPClient) sendCommand(method string, params map[string]any, cdpSess
10751069 step := command .Steps [0 ]
10761070 result , err = c .Upstream .Send (step .Method , step .Params , step .SessionID )
10771071 } else if command .Target == "service_worker" {
1078- if c .ExtSessionID == "" {
1072+ if c .Injector == nil || c . Injector . SessionID == "" {
10791073 return nil , fmt .Errorf ("service_worker commands require an injected ModCDP extension target" )
10801074 }
10811075 step , stepErr := c .Types .ServiceWorkerCommandStep (method , params , cdpSessionID , 0 )
10821076 if stepErr != nil {
10831077 return nil , stepErr
10841078 }
1085- rawResult , routeErr := c .Router .Send (step .Method , step .Params , c .ExtSessionID )
1079+ rawResult , routeErr := c .Router .Send (step .Method , step .Params , c .Injector . SessionID )
10861080 if routeErr != nil {
10871081 return nil , routeErr
10881082 }
@@ -1198,31 +1192,31 @@ func (c *ModCDPClient) upstreamTransport() upstreamTransportClient {
11981192 }
11991193}
12001194
1201- func (c * ModCDPClient ) extensionInjectorsForConfig () []extensionInjector {
1195+ func (c * ModCDPClient ) extensionInjectorsForConfig () ( []extensionInjector , * ExtensionInjector ) {
12021196 if c .Config .Injector .InjectorMode == "none" {
1203- return nil
1197+ return nil , nil
12041198 }
12051199 if c .Config .Injector .InjectorMode == "cli" {
12061200 injector := NewCLIExtensionInjector (InjectorConfig {})
1207- return []extensionInjector {& injector }
1201+ return []extensionInjector {& injector }, & injector . ExtensionInjector
12081202 }
12091203 if c .Config .Injector .InjectorMode == "cdp" {
12101204 injector := NewCDPExtensionInjector (InjectorConfig {})
1211- return []extensionInjector {& injector }
1205+ return []extensionInjector {& injector }, & injector . ExtensionInjector
12121206 }
12131207 if c .Config .Injector .InjectorMode == "bb" {
12141208 injector := NewBBExtensionInjector (InjectorConfig {})
1215- return []extensionInjector {& injector }
1209+ return []extensionInjector {& injector }, & injector . ExtensionInjector
12161210 }
12171211 if c .Config .Injector .InjectorMode == "discover" {
12181212 injector := NewDiscoverExtensionInjector (InjectorConfig {})
1219- return []extensionInjector {& injector }
1213+ return []extensionInjector {& injector }, & injector . ExtensionInjector
12201214 }
12211215 if c .Config .Injector .InjectorMode == "borrow" {
12221216 injector := NewBorrowExtensionInjector (InjectorConfig {})
1223- return []extensionInjector {& injector }
1217+ return []extensionInjector {& injector }, & injector . ExtensionInjector
12241218 }
1225- return nil
1219+ return nil , nil
12261220}
12271221
12281222func isKnownLaunchMode (mode string ) bool {
@@ -1266,9 +1260,9 @@ func (c *ModCDPClient) baseInjectorConfig(send SendCDP) InjectorConfig {
12661260 }
12671261}
12681262
1269- func (c * ModCDPClient ) injectExtension (injectors []extensionInjector ) (* ExtensionInjectionResult , error ) {
1263+ func (c * ModCDPClient ) injectExtension (injectors []extensionInjector ) (* ExtensionInjectionResult , * ExtensionInjector , error ) {
12701264 if len (injectors ) == 0 {
1271- return nil , fmt .Errorf ("injector.injector_mode=none cannot be used with an extension-routed browser upstream" )
1265+ return nil , nil , fmt .Errorf ("injector.injector_mode=none cannot be used with an extension-routed browser upstream" )
12721266 }
12731267 send := func (method string , params map [string ]any , sessionID string ) (map [string ]any , error ) {
12741268 if c .Upstream == nil {
@@ -1289,11 +1283,12 @@ func (c *ModCDPClient) injectExtension(injectors []extensionInjector) (*Extensio
12891283 continue
12901284 }
12911285 if result != nil {
1292- injector .RecordInjectionResult (result )
1293- return result , nil
1286+ state := injector .RecordInjectionResult (result )
1287+ c .Injector = state
1288+ return result , state , nil
12941289 }
12951290 }
1296- return nil , fmt .Errorf ("cannot install, discover, or borrow the ModCDP extension in the running browser.%s" , formatInjectorErrors (errors ))
1291+ return nil , nil , fmt .Errorf ("cannot install, discover, or borrow the ModCDP extension in the running browser.%s" , formatInjectorErrors (errors ))
12971292}
12981293
12991294func formatInjectorErrors (errors []string ) string {
@@ -1410,8 +1405,8 @@ func (c *ModCDPClient) handleEventMessage(msg map[string]any) {
14101405 method , _ := msg ["method" ].(string )
14111406 sessionID , _ := msg ["sessionId" ].(string )
14121407 params , _ := msg ["params" ].(map [string ]any )
1413- if c .ExtSessionID != "" && sessionID == c .ExtSessionID {
1414- if unwrapped , ok := translate .UnwrapEventIfNeeded (method , params , sessionID , c .ExtSessionID ); ok {
1408+ if c .Injector != nil && c . Injector . SessionID != "" && sessionID == c .Injector . SessionID {
1409+ if unwrapped , ok := translate .UnwrapEventIfNeeded (method , params , sessionID , c .Injector . SessionID ); ok {
14151410 validatedData , valid := c .Types .ParseEventPayload (unwrapped .Event , unwrapped .Data )
14161411 if ! valid {
14171412 return
0 commit comments