给定 request.continue() 的循环行为,如何将每个网络请求存储到一个数组中?

How to store every network request into an array given the loop behaviour of request.continue()?

我试图在访问页面时获取所有网络请求并将它们存储到一个数组中。

我的代码如下所示:

  await page.setRequestInterceptionEnabled(true);
  page.on('request', request => {
      if(request.url) {
          var networkRequests = request.url;
          var networkArray = [];
          for (var i = 0; i < networkRequests; i++) {
              networkArray.push(networkRequests[i]);
          }
          console.log(networkArray);
          console.log(typeof networkArray);
          request.continue();
      } else {
          request.abort();
      }

  });
  await page.goto('http://www.example.com', {waitUntil: 'networkidle'});

我发现问题出在 request.continue()。它为每个获取的请求创建多个迭代,并且对于每个迭代,它显示该请求并将 returns 作为字符串显示。 这意味着我最终得到了几个字符串。

问题是我无法将所有这些字符串插入到一个数组中,所以我最近可以利用它们。我尝试了几个 for 循环但没有成功。

同时找到了一个快速修复方法:

const puppeteer = require('puppeteer');

function extractRequests(url) {
    return new Promise((resolve, reject) => {
        (async() => {
            const browser = await puppeteer.launch();
            const page = await browser.newPage();
            await page.setRequestInterceptionEnabled(true);
            let result = [];
            page.on('request', request => {
                if (request.url) {
                    var networkRequests = request.url;
                    result.push(networkRequests);
                    request.continue();
                } else {
                    request.abort();
                }

            });
            page.goto(url, {
                     waitUntil: 'networkidle'
                })
                .then( _=> setTimeout( _=> resolve(result), 1000));
        })();
    });
}

extractRequests('http://example.com').then(requests => {
    console.log(requests.filter(x => x.includes('event-name') && x.includes('other-event-name')));
    process.exit(0);
});