Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
Puppeteer interceptedRequest has not alway the same output

I'm trying to get the size of all media on a specific URL with Puppeteer. I made this script with a simple Node.JS server. The URL is a command line parameter.

const puppeteer = require('puppeteer');
const axios = require('axios');

let totalPageSize = {
  'image/jpeg' : 0,
  'image/png' : 0,
  'video/mp4' : 0,
  'image/webp' : 0
};

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.setRequestInterception(true);
  await page.setDefaultNavigationTimeout(0); 
  await page.setCacheEnabled(false);
  page.on('request', (interceptedRequest) => {
    countFiles(interceptedRequest);
  });
  await page.goto(process.argv[2]);

  // FINAL OUTPUT
  console.log(totalPageSize);
  await browser.close();
})();

async function countFiles(requested) {
  var request = requested;
  if (
  requested.url().indexOf('.png?') != -1 
  || requested.url().indexOf('.jpg?') != -1 
  || requested.url().indexOf('.svg?') != -1 
  || requested.url().indexOf('.webp?') != -1 
  || requested.url().indexOf('.mp4?') != -1) {
    request = request.url().substring(0, request.url().indexOf('?'));
  }
  if (
  request.url().endsWith('.png') 
  || request.url().endsWith('.jpg') 
  || request.url().endsWith('.svg') 
  || request.url().endsWith('.webp') 
  || request.url().endsWith('.mp4')) {
    await request.abort();
    url = request.url();
    axios.get(url)
    .then((res) => {
      totalPageSize[res.headers['content-type']] += parseInt(res.headers['content-length']);
    })
  } else {
    await request.continue();
  }
}

Actually it seems to work, i have an amount of bytes as output but it's not always the same (may be 2x bigger or smaller on a same non-dynamic page).

Have you got an idea of a possible fix ?

Thanks

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!