[英]How can I catch only the XHR requests on a Devtools Extension?
For the past days, we've been trying to develop a Devtools extension that could intercept only XHR requests. 在过去的几天里,我们一直在尝试开发一个只能拦截XHR请求的Devtools扩展。 We can use the chrome.webRequest API on a normal extension, but that is not possible on a Devtools Extension Panel. 我们可以在普通扩展上使用chrome.webRequest API,但这在Devtools扩展面板上是不可能的。 We tried to used the devtools.network, but it catches all requests. 我们尝试使用devtools.network,但它捕获了所有请求。
Is there a way to catch only the XHR requests? 有没有办法只捕获XHR请求?
Thanks in advance. 提前致谢。
You can use the chrome.devtools.network
API to get the HAR, and then you can determine whether a request is XHR or not, filtering the output. 您可以使用chrome.devtools.network
API获取HAR,然后您可以确定请求是否为XHR,过滤输出。
I'm not totally sure how DevTools determines this, but the X-Requested-With
header is (typically) sent when AJAX requests are made. 我不完全确定DevTools如何确定这一点,但是在发出AJAX请求时(通常)发送X-Requested-With
标头。 It is a non-standard, but is used widely. 它是非标准的,但被广泛使用。 You can check for the XMLHttpRequest
value in the HAR. 您可以在HAR中检查XMLHttpRequest
值。
It's possible this doesn't catch all the requests, and there might be some other data DevTools uses, but here's a little snippet I created that will filter the HAR based on this header. 这可能不会捕获所有请求,DevTools可能会使用其他一些数据,但这里有一个我创建的小片段,它将根据此标头过滤HAR。
chrome.devtools.network.getHAR(function(result) {
var entries = result.entries;
var xhrEntries = entries.filter(function(entry) {
var headers = entry.request.headers;
var xhrHeader = headers.filter(function(header) {
return header.name.toLowerCase() === 'x-requested-with'
&& header.value === 'XMLHttpRequest';
});
return xhrHeader.length > 0;
});
console.log(xhrEntries);
});
Note. 注意。 You can access the HAR data in the same way, per request, as it finishes, using the chrome.devtools.network.onRequestFinished
event. 您可以使用chrome.devtools.network.onRequestFinished
事件按照完成后的请求以相同的方式访问HAR数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.