【问题标题】:How can i monitor network activity with nodejs (puppeteer)我如何使用 nodejs (puppeteer) 监控网络活动
【发布时间】:2019-04-02 14:33:41
【问题描述】:

我正在使用 Nodejs 和 firefox-puppeteer 构建一个应用程序,并且我正在尝试监控网络流量,以便我可以读取标头,如果标头与我的正则表达式匹配,则会在我的终端上打印一条消息。

我试过木偶师:page.setRequestInterception(value) 但这只会在应用程序找到标题之一时停止应用程序并且程序将中断

欢迎任何建议 提前致谢

【问题讨论】:

    标签: node.js google-chrome firefox networking puppeteer


    【解决方案1】:

    您无需通过page.setRequestInterception 启用请求拦截来侦听网络活动(您可能需要查看request.continue 以了解有关该主题的更多信息)。

    要简单地监听网络响应,您可以监听 response 事件。下面是一些示例代码:

    const puppeteer = require('puppeteer');
    
    const textRegex = /(javascript|html)/; // example regex
    
    (async () => {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
    
        page.on('response', (response) => {
            const headers = response.headers();
    
            // example test: check if content-type contains javascript or html
            const contentType = headers['content-type'];
            if (textRegex.test(contentType)) {
                console.log(response.url());
            }
        });
        await page.goto(`...`);
    
        await page.close();
        await browser.close();
    })();
    

    此示例侦听响应并打印所有在其 Content-Type 标头中包含字符串 javascripthtml 的 URL。

    【讨论】:

      猜你喜欢
      • 2013-11-27
      • 2011-09-02
      • 1970-01-01
      • 2021-03-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多