How to intercept Playwright's Firefox image requests

988 Views Asked by At

I recently tried to intercept all image requests following the example in their documentation:

await page.route('**/*.{png,jpg,jpeg}', route => route.abort());

// Abort based on the request type
await page.route('**/*', route => {
  return route.request().resourceType() === 'image' ?
      route.abort() : route.continue();
});

But it was not working at all.

How can I make it work?

1

There are 1 best solutions below

0
Soldeplata Saketos On

There is a missing string in the types (or they are outdated in v1.15.1):enter image description here

When logging route.request().resourceType() into console, I noticed there is a "images" resource type. So you can easily intercept any images including "images" to your check.

For instance you could do like this:

await page.route('**/*', route => {
  return route.request().resourceType().match(/(image)s?/) ?
      route.abort() : route.continue();
});