mattsse / chromiumoxide

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

Request interception #83

Open ghost opened 2 years ago

ghost commented 2 years ago

Is request interception supported? I see NetworkManager functions that seem like they may enable request interception (they appear to be related to Handler and not Page), but I don't see how to implement them in my own code.

ghost commented 2 years ago

I overlooked page.event_listener::<RequestWillBeSent however it is not catching all requests. The code in https://github.com/mattsse/chromiumoxide/issues/28 does not enable fetch/XHR request interception. I also get some errors while the page is navigating:

[2022-01-13T03:46:18Z ERROR chromiumoxide::handler] WS Connection error: Serde(Error("data did not match any variant of untagged enum Message", line: 0, column: 0))
[2022-01-13T03:46:18Z ERROR chromiumoxide::conn] Failed to deserialize WS response data did not match any variant of untagged enum Message
[2022-01-13T03:46:18Z ERROR chromiumoxide::handler] WS Connection error: Serde(Error("data did not match any variant of untagged enum Message", line: 0, column: 0))
[2022-01-13T03:46:19Z ERROR chromiumoxide::conn] Failed to deserialize WS response data did not match any variant of untagged enum Message
[2022-01-13T03:46:19Z ERROR chromiumoxide::handler] WS Connection error: Serde(Error("data did not match any variant of untagged enum Message", line: 0, column: 0))
[2022-01-13T03:46:19Z ERROR chromiumoxide::conn] Failed to deserialize WS response data did not match any variant of untagged enum Message
[2022-01-13T03:46:19Z ERROR chromiumoxide::handler] WS Connection error: Serde(Error("data did not match any variant of untagged enum Message", line: 0, column: 0))

The trace of one of the error messages looks like this:

[2022-01-13T03:47:24Z TRACE tungstenite::protocol] Received message {"method":"Network.requestWillBeSent","params":{"requestId":"475742D522E65043717C83E682E60A29","loaderId":"","documentURL":"https://gql.twitch.tv/gql","request":{"url":"https://gql.twitch.tv/gql","method":"OPTIONS","headers":{"Accept":"*/*","Access-Control-Request-Method":"POST","Access-Control-Request-Headers":"authorization,client-id,device-id","Origin":"https://www.twitch.tv","User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36","Sec-Fetch-Mode":"cors"},"initialPriority":"High","referrerPolicy":"strict-origin-when-cross-origin"},"timestamp":88031.531252,"wallTime":1642045644.800056,"initiator":{"type":"preflight","url":"https://gql.twitch.tv/gql","requestId":"84218.9"},"type":"Other","hasUserGesture":false},"sessionId":"2080018A284A5608EEEC8F7A84708972"}
[2022-01-13T03:47:24Z ERROR chromiumoxide::conn] Failed to deserialize WS response data did not match any variant of untagged enum Message
[2022-01-13T03:47:24Z ERROR chromiumoxide::handler] WS Connection error: Serde(Error("data did not match any variant of untagged enum Message", line: 0, column: 0))

It looks like there are 2 requests that cause errors, and another error is triggered on ResponseReceived for each of the requests. These errors don't seem related to request interception, but I noticed that these requests hang indefinitely and the page never finishes loading.

Sytten commented 1 year ago

Did you manage to do request interception?

Sytten commented 1 year ago

You can look at my PR #118, I added an example

theanh098 commented 1 year ago

@Sytten Can you give a example to get the reponse body after requesting network with a given url ?

Sytten commented 11 months ago

It's been a while since I touched this code, there is probably an event you can listen to to get that data. I don't have that need, but feel free to share if you manage to do it.

escritorio-gustavo commented 11 months ago

Would it be possible to do this on the browser level, so that setting it up once affects all pages? I tried following the example, but it didn't work because Browser doesn't implement Clone.

Is there another way? And if not, Would it be worth it (or even possible) implementing the Clone trait for Browser?