Skip to content

Instantly share code, notes, and snippets.

@Alex1990
Created December 19, 2018 06:17
Show Gist options
  • Save Alex1990/66c97f2d61b30a66fff24dcb3a53d175 to your computer and use it in GitHub Desktop.
Save Alex1990/66c97f2d61b30a66fff24dcb3a53d175 to your computer and use it in GitHub Desktop.
Get the encoded data size of all requests in a page
const puppeteer = require('puppeteer');
const prettyBytes = require('pretty-bytes');
const chalk = require('chalk');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const devToolsResponses = new Map();
const devTools = await page.target().createCDPSession();
await devTools.send('Network.enable');
devTools.on('Network.responseReceived', event => {
devToolsResponses.set(event.requestId, event.response);
});
devTools.on('Network.loadingFinished', event => {
const response = devToolsResponses.get(event.requestId);
const encodedBodyLength = event.encodedDataLength;
console.log(`${chalk.green(prettyBytes(encodedBodyLength))} - ${response.url}`);
});
const url = 'https://www.baidu.com';
await page.goto(url, { waitUtil: 'networkIdle2' });
await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment