-
Notifications
You must be signed in to change notification settings - Fork 12
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
Use of Network.setRequestInterception #31
Comments
For this kind of task, I prefer to do the following script, that works: library(crrri)
chrome <- chr_connect()
url = "https://www.rstudio.com"
intercepted <-
chrome %>%
Page.enable() %>%
Network.enable() %>%
Network.setRequestInterception(patterns = list(list(urlPattern="*"))) %>%
Network.requestIntercepted() %...T>% {
print("intercepted")
}
chrome %>%
Page.navigate(url)
chr_disconnect(chrome) This first creates a promise for intercepted network request then opens the url. |
If you want to execute a callback for each intercepted network request, you can use the library(crrri)
chrome <- chr_connect()
url = "https://www.rstudio.com"
chrome %>%
Page.enable() %>%
Network.enable() %>%
Network.setRequestInterception(patterns = list(list(urlPattern="*"))) %>%
Network.requestIntercepted(.callback = function(msg) {
print(msg$params$request)
})
chrome %>%
Page.navigate(url)
chrome %>%
Page.navigate("https://cdn.rawgit.com/juba/rmdformats/master/resources/examples/material/material.html")
chr_disconnect(chrome) |
Thanks ! One more question about your first example : if I run all the code at once, the request is not intercepted. I have to run the And if I try with
|
You're right, I wrote an "interactive" script. My mistake. library(crrri)
chrome <- chr_connect()
url = "https://www.rstudio.com"
configured <-
chrome %>%
Page.enable() %>%
Network.enable() %>%
Network.setRequestInterception(patterns = list(list(urlPattern="*")))
intercepted <-
configured %>%
Network.requestIntercepted() %...T>% {
print("intercepted")
}
configured %>%
Page.navigate(url) %...!% {
}
intercepted %...>% {
chr_disconnect(chrome)
} Using callback would be a better idea for this use case |
What another great use case for the event emitter API! I'll post a new example soon hoping it will work ok! |
Ok, one more thing I fear... Your last example works fine when using promises only, but it seems I'm in trouble if I try to use callbacks : library(crrri)
chrome <- chr_connect("google-chrome")
url = "https://www.rstudio.com"
configured <- chrome %>%
Page.enable() %>%
Network.enable() %>%
Network.setRequestInterception(patterns = list(list(urlPattern="*"))) %>%
Network.requestIntercepted(.callback = function(msg) {
print(msg$params$request)
})
ended <- configured %>%
Page.navigate(url) %>%
Page.loadEventFired() %>%
DOM.getDocument() %>%
DOM.getOuterHTML(nodeId = ~.$root$nodeId) %...T>%
{ print(.$result)}
ended %...>% { chr_disconnect(chrome) } In this case I get an error : Once again, I'm sorry if this is a misunderstanding from my part. |
There are 2 different topics: one related to Using a callback in an event listenerAs stated in the documentation, an event listener returns:
That means that the Using
|
Ok, thanks for the explanation. I didn't find this in the DevTools documentation, but that's what I think I mostly understood. The way I see it, the only way for it to work would be to issue a Anyway I think I can achieve mostly what I'm trying to do by using |
You can use library(crrri)
chrome <- chr_connect()
url = "https://www.rstudio.com"
configured <-
chrome %>%
Page.enable() %>%
Network.enable() %>%
# intercept all the requests:
Network.setRequestInterception(patterns = list(list(urlPattern="*")))
configured %>%
Network.requestIntercepted() %...>% {
print(.$result$request)
Network.continueInterceptedRequest(.$ws, interceptionId = .$result$interceptionId)
}
configured %>%
Page.navigate(url) %>% # send a request
Page.loadEventFired() %>%
DOM.getDocument() %>%
DOM.getOuterHTML(nodeId = ~ .res$root$nodeId) %...T>%
{ print(.$result)} |
Yes, this works, but can you add a callback in |
I think this script will be fine: library(crrri)
chrome <- chr_connect()
url = "https://www.rstudio.com"
configured <-
chrome %>%
Page.enable() %>%
Network.enable() %>%
Network.setRequestInterception(patterns = list(list(urlPattern="*")))
configured %>%
Network.requestIntercepted(.callback = function(msg) {
print(msg$params$request)
configured %>% Network.continueInterceptedRequest(interceptionId = msg$params$interceptionId)
})
configured %>%
Page.navigate(url) %>% # send a request
Page.loadEventFired() %>%
DOM.getDocument() %>%
DOM.getOuterHTML(nodeId = ~ .res$root$nodeId) %...T>%
{ print(.$result)} |
For the record, I think this works if you add a library(crrri)
chrome <- chr_connect("google-chrome", headless = TRUE)
url <- "https://rstudio.com"
configured <- chrome %>%
Page.enable() %>%
Network.enable() %>%
Network.setRequestInterception(patterns = list(list(urlPattern="*", interceptionStage="HeadersReceived")))
configured %>%
Network.requestIntercepted(.callback = function(msg) {
print(msg$params$request$url)
configured %>%
Network.continueInterceptedRequest(interceptionId = msg$params$interceptionId)
})
ended <- configured %>%
Page.navigate(url) %>%
Page.loadEventFired() %>%
DOM.getDocument() %>%
DOM.getOuterHTML(nodeId = ~ .res$root$nodeId) %...T>%
{ print("---"); print(substring(.$result, 0, 100))}
ended %...>% {print("Done"); chr_disconnect(chrome)} This is great, many many thanks ! |
OK for reference, I tested the new API using this use case. |
Hi,
If I want to check a page and get some informations about network operations, I can do something like the following :
However, I'd like to use
Network.setRequestInterception
to be able to capture only certain requests. I tried to do it this way, but it doesn't seem to work :Would you have any idea of what I'm doing wrong ?
Thanks !
The text was updated successfully, but these errors were encountered: