Reason for XHR request and response inside JavaScript workers no being logged.
See original GitHub issueHi,
I am trying to log all the XHR requests and responses from a website. However, I
see some XHR requests and responses not captured by Network.requestWillBeSent
and Network.responseReceived
event handlers. What could be the reason for this?
Note that, I can see this XHR requests and responses, that my script failed to capture, in the network tab in my chrome debug tools. So what could be the reason for the script to miss this request and response?
Following is the code snippet that I am using:
Network.requestWillBeSent((params) => {
fs.appendFile(requests, params.requestId +
',' + params.request.url + '\n', function(err){});
});
Network.responseReceived((params) => {
downloadFile[params.requestId] = params.type;
});
Network.loadingFinished((params) => {
if (downloadFile[params.requestId]) {
Network.getResponseBody({
requestId: params.requestId
}).then(response => {
fs.appendFileSync(path.join(folder, downloadFile[params.requestId] + "-" + params.requestId),
response.body, function(err){});
});
}
});
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
JavaScript console.log causes error: "Synchronous ...
no, the message warning is PRESUMABLY due to an XMLHttpRequest request within the main thread with the async flag set to false. This...
Read more >Using XMLHttpRequest - Web APIs | MDN
In this guide, we'll take a look at how to use XMLHttpRequest to issue HTTP requests in order to exchange data between the...
Read more >Fetch (or the undeniable limitations of XHR) - Microsoft Edge ...
In XHR, the whole response would be buffered, rather than being able to operate on the data in chunks. It's possible to set...
Read more >Browser APIs and Protocols: XMLHttpRequest
XMLHttpRequest (XHR) is a browser-level API that enables the client to script data transfers via JavaScript. XHR made its first debut in Internet...
Read more >AJAX The XMLHttpRequest Object - W3Schools
With the XMLHttpRequest object you can define a callback function to be executed when the request receives an answer. The function is defined...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This happens because those requests are performed inside JavaScript workers and from a Chrome perspective they are separate (sub) targets. Bad news is that handling this case requires to write a much more complex code since you cannot use the usual JavaScript API, but if you’re just interested in events from workers you can get away pretty easily:
The output will be something like:
This issue is closed, yet I’m posting here in case someone else got stuck with this as I did. Apparently just connecting to the service worker doesn’t suffice, as it further uses a dedicated worker to actually make the network requests. So you’ll have to send a message from the main target to the service worker, which in turn should another message to the dedicated worker. Just to extend @cyrus-and 's solution, something like this should work