Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent Chrome headless from loading images

I am running a test in headless chrome and part of it I want to prevent the browser from loading the images on the page, the page must be a data url and not a normal page.
I am using headless chrome with the next start command:
chrome --headless --remote-debugging-port=9222

I had created the next test to demonstrate what I am trying to achieve. but nothing works...

const CDP = require('chrome-remote-interface');
const fs = require('fs');

CDP(async(client) => {
  const {
    Page,
    Network
  } = client;
  try {
    await Page.enable();
    await Network.enable();
    await Network.emulateNetworkConditions({
      offline: true,
      latency: 0,
      downloadThroughput: 0,
      uploadThroughput: 0
    });
    await Page.navigate({
      url: "data:text/html,<h1>The next image should not be loaded</h1><img src='http://via.placeholder.com/350x150'>"
    });
    await Page.loadEventFired();
    const {
      data
    } = await Page.captureScreenshot();
    fs.writeFileSync((+new Date()) + '.png', Buffer.from(data, 'base64'));
  } catch (err) {
    console.error(err);
  } finally {
    await client.close();
  }
}).on('error', (err) => {
  console.error(err);
});
like image 924
Wazime Avatar asked Feb 13 '18 18:02

Wazime


2 Answers

With puppeteer you can use the args option for passing the blink-settings argument

const browser = await puppeteer.launch({
    args: [
      '--blink-settings=imagesEnabled=false'
    ]
});
like image 193
Joyce Babu Avatar answered Dec 09 '22 21:12

Joyce Babu


You can block images using this flag.
It works on canary and stable.

chrome --headless --remote-debugging-port=9222 --blink-settings=imagesEnabled=false

like image 34
kappatech Avatar answered Dec 09 '22 20:12

kappatech