chromiumoxide icon indicating copy to clipboard operation
chromiumoxide copied to clipboard

Add support for browser.on events

Open bobajeff opened this issue 4 years ago • 7 comments

I'm needing to use browser.on events similar to what puppeteer documents here: https://pptr.dev/#?product=Puppeteer&version=v5.5.0&show=api-class-browser

The main ones I'm interested in are browser.on('targetchanged') and browser.on('targetcreated'). I think puppeteer sends Target.setDiscoverTargets [1] on start up and and emits those events when it receives Target.targetInfoChanged and Target.targetCreated respectivly.

I'm willing to take this on however I'm unsure on the correct way to send messages to the connection.

bobajeff avatar Jan 07 '21 01:01 bobajeff

Can you check whether the eventlistener api I introduced in #4 fulfills your needs.

This is currently only exposed on the Page, but could potentially also added to the browser itself.

I double checked and I see a couple of problems with emitting events from the Browser, right now the Targets emit the events where the message contains the sessionId of the Target. Emitting events from the browser that don't contain a sessionId should be rather easy to implement though.

mattsse avatar Jan 07 '21 10:01 mattsse

That looks like something like that would work. I'm assuming it would look something like this:

let target = browser.event_listener::<EventTargetCreated>().await?;

~~We must send setDiscoverTargets before targets are created though:~~ ~~Which I guess would look something like this:~~

~~browser.execute::<SetDiscoverTargetsParams>();~~

Edit: It appears that setDiscoverTargets is already being sent to CDP. Though I don't know by which function.

bobajeff avatar Jan 07 '21 18:01 bobajeff

It appears that setDiscoverTargets is already being sent to CDP. Though I don't know by which function.

Yes this is the very first command sent when creating the Handler

The drawback from exposing the event_listner on the browser directly is that there aren't many events that aren't related to a session, so it would not be very easy to distinguish what event should be sent via the browser's event pipeline or via the target's event pipeline. One thing that may be possible is to emit all events of a certain type on the Browser disregarding the event's sessionId and only emitting the event via the Target with the matching sessionId (as it is right now). However this would introduce a certain overhead (wrapping all events in Arc or cloning a lot), this might do more harm than it is useful

mattsse avatar Jan 08 '21 16:01 mattsse

The drawback from exposing the event_listner on the browser directly is that there aren't many events that aren't related to a session, so it would not be very easy to distinguish what event should be sent via the browser's event pipeline or via the target's event pipeline. One thing that may be possible is to emit all events of a certain type on the Browser disregarding the event's sessionId and only emitting the event via the Target with the matching sessionId (as it is right now). However this would introduce a certain overhead (wrapping all events in Arc or cloning a lot), this might do more harm than it is useful

I agree I was just about to suggest something like a custom_on_target_created ('target_changed, 'destroyed etc.) in the handler would make more sense.

See my use case is running operations and each page. So maybe I just need those functions to return a Page for me to call execute, evaluate etc. on.

Edit: Actually, I'll investigate using Browser.pages() for this.

bobajeff avatar Jan 08 '21 17:01 bobajeff

I see, does Browser::pages fulfil your needs?

mattsse avatar Jan 08 '21 17:01 mattsse

It might. I'm not sure yet. I'll check it out though.

bobajeff avatar Jan 08 '21 17:01 bobajeff

Browser::pages seems to only return a vector of pages. I need to run operations on every page that exists (when they exist). If I run it early and a page gets added later I won't know until the next time I run pages(). Also, I only want to run these operations once per added page or navigation.

bobajeff avatar Jan 08 '21 20:01 bobajeff