Node.js Puppeteer:如何接收 iframe 的 XHR 响应(使用 JS Web Worker)
我有一个页面,每 10 秒通过 iframe 内的 XHR 与服务器进行通信。我想监视响应(文本/纯 UTF-8)。
与 DevTools Network 列表相反,Puppeteer 似乎没有“检测” 来自 iframe 内部的 XHR 响应,具有正常的页面过程:
page.on('response', async (response) => {}
当 iframe 的 ID 已知时,有什么方法可以接收 iframe 的 XHR 响应吗? (由 JS Web Worker 执行 XHR 请求)
I have a page which communicates to a server every 10 seconds via XHR inside an iframe. I would like to monitor the responses (text/plain UTF-8).
Opposite to the DevTools Network list, it seems Puppeteer does not "detect"
XHR responses from inside iframes, with it's normal page procedure:
page.on('response', async (response) => {}
When the ID of the iframe is known, is there any way to receive the XHR responses of the iframe? (with a JS web worker doing the XHR requests)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我编写了这个示例,它展示了请求确实是从
页面
的内部框架中捕获
的。您必须小心,失败的请求实际上不会触发page.on('response')
处理程序,但会触发page.on('requestfailed')
。另外,请确保在向页面添加任何请求相关处理程序之前调用
await page.setRequestInterception(true)
!这是一个工作示例:(由于跨源限制,它只会触发
requestFailed
。)I have written this example which showcases that requests are indeed
captured
from inner frames of yourpage
. You have to be careful, requests that fail won't actually triggerpage.on('response')
handler but will triggerpage.on('requestfailed')
.Also make sure to call
await page.setRequestInterception(true)
before adding any request related handlers to your page!Here is a working example: (it will only trigger
requestFailed
due to cross origin restrictions.)