Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I listen CDP events in thirtyfour? #55

Open
DCjanus opened this issue May 26, 2021 · 15 comments
Open

Can I listen CDP events in thirtyfour? #55

DCjanus opened this issue May 26, 2021 · 15 comments
Labels
help wanted Extra attention is needed

Comments

@DCjanus
Copy link

DCjanus commented May 26, 2021

We've able to execute cdp command in thirtyfour, but I've not found a way to listen CDP event.

Sometimes, I hope to use fetch.enable to catch all network request in a page. When page send a network request, a requestPaused event is issued, and I should capture this event and send fulfillRequest or failRequest.

Like this: https://stackoverflow.com/questions/59420493/how-to-use-chrome-devtools-protocol-in-selenium-using-python-for-capturing-htt

@stevepryde
Copy link
Owner

I don't think this is possible in thirtyfour currently. Would you be willing to contribute this feature?

@DCjanus
Copy link
Author

DCjanus commented May 27, 2021

Sounds good to me.

@DCjanus
Copy link
Author

DCjanus commented May 27, 2021

I am not very familiar with chrome driver special protocol, like /session/{session_id}/goog/cdp/execute, is there any place that I can find it,

@stevepryde
Copy link
Owner

stevepryde commented May 27, 2021

Have a look here: https://chromedevtools.github.io/devtools-protocol/

Edit: use the navigation on the side to view docs for each command

@stevepryde
Copy link
Owner

You can also check out other selenium libraries such as the python one to see how they do it

@DCjanus
Copy link
Author

DCjanus commented May 27, 2021

It looks like that selenium open a websocket connection to accept CDP event, which means we need to introduce a websocket library to thirtyfour.

Which websocket library would you like to use?

I'm not familiar with this domain, my friends told me tokio-tungstenite is the best chose, but it doesn't support async-std, and there is a fork to support different async runtime.

@stevepryde
Copy link
Owner

stevepryde commented May 27, 2021

Yeah have a look at https://github.com/sdroege/async-tungstenite which supports both runtimes via feature flags

Edit: just realised that's the one you linked to 😂

@audioXD
Copy link
Contributor

audioXD commented Jul 3, 2021

I thought that most libraries just use a proxy to intercept all those requests :p

@stevepryde
Copy link
Owner

You can use a proxy, however you can use CDP to do it, which I would prefer over a proxy since it's coming from the browser itself. It's more like listening to the browser's network tab

@notdanilo
Copy link

Any success on listening the events from a websocket connection? I am trying this with no success yet:

    let response = reqwest::get("http://localhost:9222/json/version")
        .await?
        .json::<HashMap<String, String>>()
        .await?;
    let ws_debugger_url = response.get("webSocketDebuggerUrl").expect("Couldn't get ws debugger url.");

    let (mut socket, _) = connect_async(Url::parse(ws_debugger_url)?).await?;
    
        let dev_tools = ChromeDevTools::new(driver.session());
    let version_info = dev_tools.execute_cdp("Browser.getVersion").await?;
    println!("{:#?}", version_info);

    std::thread::sleep(Duration::from_secs_f64(3.0));
    println!("{:#?}", dev_tools.execute_cdp("HeapProfiler.enable").await?);
    println!("{:#?}", dev_tools.execute_cdp_with_params("HeapProfiler.takeHeapSnapshot", json!({"reportProgress":true,"treatGlobalObjectsAsRoots":true,"captureNumericValue":false})).await?);

    while let Some(Ok(message)) = socket.next().await {
        println!("{}", message);
    }

@notdanilo
Copy link

Maybe I am missing the session ID or something that binds the websocket to the client sending the requests?

@notdanilo
Copy link

I just found this link, which describes the protocol process pretty well: https://github.com/aslushnikov/getting-started-with-cdp

@audioXD
Copy link
Contributor

audioXD commented Sep 9, 2021

@notdanilo I saw that the OP made a project seperate from thirtyfour, chromiumoxide made for CDP. To be honest it looks quite similar (but works only for chrome)

EDIT: Yust realized it was a fork from @mattsse

@notdanilo
Copy link

notdanilo commented Sep 9, 2021

@audioXD I urgently created a CDP client myself because I couldn't get chromiumoxide working. The reason it didnt work was because I was simply misunderstanding how the protocol works and chromiumoxide didn't provide a high level API for what I wanted (getting a memory heap snapshot).

I am still using thirtyfour for collecting information from html although.

@stevepryde stevepryde added the help wanted Extra attention is needed label Nov 4, 2021
@stevepryde
Copy link
Owner

For anyone else looking for CDP support, start here: https://docs.rs/thirtyfour/latest/thirtyfour/extensions/cdp/struct.ChromeDevTools.html

CDP itself is quite large, so we may not have high-level wrappers around it in the short term, but you can implement it yourself without needing to modify thirtyfour.
CDP docs are here: https://chromedevtools.github.io/devtools-protocol/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants