Created
December 19, 2018 06:17
-
-
Save Alex1990/66c97f2d61b30a66fff24dcb3a53d175 to your computer and use it in GitHub Desktop.
Get the encoded data size of all requests in a page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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