mattsse / chromiumoxide

Chrome Devtools Protocol rust API
Apache License 2.0
712 stars 69 forks source link

Implement function to fetch existing targets when connecting to existing chromium instance #187

Closed hackermondev closed 8 months ago

hackermondev commented 8 months ago

When connecting to an existing Chromium instance with Browser::connect, pages created before the connection cannot be used because chromiumoxide doesn't know they exist.

I implemented a function Browser::fetch_targets that uses Target.getTargets to fetch all existing pages. If the user wants to use pages created before the connection, all they need to do is call that function.

hackermondev commented 8 months ago

@mattsse mind reviewing this?

steve-the-crab commented 2 months ago

Hi @hackermondev,

I had a few problems in that frequently the pages of fetched targets might hang or not be discoverable.

For the sake of testing, trying to connect to any target in a loop to get any that might be available has the effect of either getting a usable target and it hanging on navigation or no target being available.

It works very well the first time, but seems to run into issues on subsequent tries.

Is this something you have seen yourself? I'm just wondering if I have missed something.

Thanks

pub async fn get_first_connectable_page(mut browser: Browser) -> anyhow::Result<(Browser, Page)> {
    for t in browser.fetch_targets().await? {
        let target = t.target_id.clone();
        match browser.get_page(target).await {
            Ok(page) => return Ok((browser, page)),
            Err(_) => {}
        };
    }
    Err(anyhow!("..."))
}