简体   繁体   English

我怎样才能捕获Devtools扩展上的XHR请求?

[英]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.

相关问题 使用Chrome Extension捕获和修改来自Facebook的XHR请求 - Catch and Modify XHR requests from Facebook with Chrome Extension 如何使用 chrome-extension 收听 xhr 响应? - How can I listen to xhr response using chrome-extension? 如何捕获JavaScript中的所有Ajax请求? - How can I catch all ajax requests in javascript? 如何以编程方式“捕获”对UIWebView的后退和转发请求? - How can I “catch” the back and forward requests made to UIWebView programmatically? 我可以在没有任何服务器的情况下发送 XHR 请求吗? - Can I send XHR requests without being on any server? 将XHR从Chrome扩展程序发送到PHP页面时出错:仅HTTP支持跨源请求 - Error when sending XHR from Chrome Extension to a PHP page: Cross origin requests are only supported for HTTP 在使用 manifest v3 的浏览器扩展中,如何在内容脚本和开发工具面板之间进行通信? - In a browser extension using manifest v3, how can I communicate between a content script and a devtools panel? 如何捕获任何XHR请求? - How to catch any XHR request? 我可以通过 Google Chrome 扩展程序以编程方式打开开发工具吗? - Can I programmatically open the devtools from a Google Chrome extension? 如何捕获被阻止的网络请求 - How do I catch the blocked network requests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM