cdp
cdp copied to clipboard
New browser tab event / notification
Hi, I'm intercepting the bodies of XHRs sent from my application to the user. I'm using this awesome library for this purpose.
Scenario:
- Users open the main application site in a tab.
- Users can open additional tabs (1-5) by right-clicking links to my application within that first tab. Technical Details:
Environment: Chromium browser on Windows 11 My current solution:
// Initialization of global instances:
globalCtx = context.Background()
globalDevt = devtool.New("http://127.0.0.1:9222")
// Periodically check for new tabs:
ticker := time.NewTicker(100 * time.Millisecond)
targets, err := globalDevt.List(globalCtx)
for _, tab := range targets {
if tab.Type == "page" {
tabConnection, err := rpcc.DialContext(globalCtx, target.WebSocketDebuggerURL)
if err != nil {
// Handle error
}
tabClient := cdp.NewClient(tabConnection)
// ... (Logic for XHR interception on this tabClient)
}
}
Is there any better way to do this? I tried using gists avaialable on the web, but no luck. I'm asking, because I have also second program connected to same chrome instance (yes, i have to have 2 programs connected to same browser using cdp :( ) and I think that this frequent querying for tabs may cause interferences I have between these 2 programs
The Target domain likely has the functionality you are looking for (https://chromedevtools.github.io/devtools-protocol/tot/Target/#method-attachToTarget).
With Target.attachToTarget and / or Target.setAutoAttach you should be getting callbacks whenever new targets are created by the context, have you tried this?
I just discovered this package so I am not sure how well supported it is.