我正在尝试使用 Puppteer 获取特定网络请求的时间。
有没有办法在 Puppteer 中过滤来自 Chrome DevTools 协议的请求,以便responseReceived
只有在收到这个特定的网络响应时才会触发?
我这个任务的实际代码是:
const client = await page.target().createCDPSession();
await client.send('Network.enable');
// this method is deprecated and I don't know if it was the right thing to use ( https://chromedevtools.github.io/devtools-protocol/tot/Network#method-setRequestInterception )
await client.send('Network.setRequestInterception', {
patterns: [
{
urlPattern: 'https://www.example.com/*task=customaction*',
resourceType: 'XHR',
interceptionStage: 'HeadersReceived'
}
]
});
client.on('responseReceived', (requestId, loaderId, timestamp, type, response, frameId) => {
console.log(`Debug data from: ${response.url} (${requestId})`)
console.log('Timing:')
console.log(response.timing)
detach()
})
该文档建议使用 Fetch 作为Network.setRequestInterception
(https://chromedevtools.github.io/devtools-protocol/tot/Fetch)的替代品,但看起来 Fetch Domain 建议修改网络请求。