4

I have a use case that needs to use Headless Chrome Network (https://chromedevtools.github.io/devtools-protocol/tot/Network/) to intercept all images requests and find out the image size before saving it (basically discard small images such as icons).

However, I am unable to figure out a way to load the image data in memory before saving it. I need to load it in Img object to get width and height. The Network.getResponseBody is taking requestId which I don't have access in Network.requestIntercepted. Also Network.loadingFinished always gives me "0" in encodedDataLength variable. I have no idea why. So my questions are:

  1. How to intercept all responses from jpg/png request and get the image data? Without saving the file via URL string to the disk and load back.

  2. BEST: how to get image dimension from header response? Then I don't have to read the data into memory at all.

My code is below:

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

(async function() {
  async function launchChrome() {
    return await chromeLauncher.launch({
      chromeFlags: [
        '--disable-gpu',
        '--headless'
      ]
    });
  }
  const chrome = await launchChrome();
  const protocol = await CDP({
    port: chrome.port
  });

  const {
    DOM,
    Network,
    Page,
    Emulation,
    Runtime
  } = protocol;
  await Promise.all([Network.enable(), Page.enable(), Runtime.enable(), DOM.enable()]);

  await Network.setRequestInterceptionEnabled({enabled: true});
  Network.requestIntercepted(({interceptionId, request, resourceType}) => {
    if ((request.url.indexOf('.jpg') >= 0) || (request.url.indexOf('.png') >= 0)) {
      console.log(JSON.stringify(request));
      console.log(resourceType);

      if (request.url.indexOf("/unspecified.jpg") >= 0) {
        console.log("FOUND unspecified.jpg");
        console.log(JSON.stringify(interceptionId));
        // console.log(JSON.stringify(Network.getResponseBody(interceptionId)));
      }

    }
    Network.continueInterceptedRequest({interceptionId});
  });

  Network.loadingFinished(({requestId, timestamp, encodedDataLength}) => {
    console.log(requestId);
    console.log(timestamp);
    console.log(encodedDataLength);
  });

  Page.navigate({
    url: 'https://www.yahoo.com/'
  });

  Page.loadEventFired(async() => {
    protocol.close();
    chrome.kill(); 
  });

})();
HP.
  • 19,226
  • 53
  • 154
  • 253
  • My understanding is that the API doesn't allow to do such thing at the moment. Vote to close then. – HP. Aug 14 '17 at 05:31

1 Answers1

2

This should get you 90% of the way there. It gets the body of each image request. You'd still need to base64decode, check size and save etc...

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

const sizeThreshold = 1024;

async function run() {
  try {
    var client = await CDP();
    const { Network, Page } = client;

    // enable events
    await Promise.all([Network.enable(), Page.enable()]);

    // commands
    const _url = "https://google.co.za";
    let _pics = [];
    Network.responseReceived(async ({requestId, response}) => {
      let url = response ? response.url : null;
      if ((url.indexOf('.jpg') >= 0) || (url.indexOf('.png') >= 0)) {
        const {body, base64Encoded} = await Network.getResponseBody({ requestId }); // throws promise error returning null/undefined so can't destructure. Must be different in inspect shell to app?
        _pics.push({ url, body, base64Encoded });
        console.log(url, body, base64Encoded);
      }
    });
    await Page.navigate({ url: _url });
    await sleep(5000);

    // TODO: process _pics - base64Encoded, check body.length > sizeThreshold, save etc...

  } catch (err) {
    if (err.message && err.message === "No inspectable targets") {
      console.error("Either chrome isn't running or you already have another app connected to chrome - e.g. `chrome-remote-interface inspect`")
    } else {
      console.error(err);
    }
  } finally {
    if (client) {
      await client.close();
    }
  }
}

function sleep(miliseconds = 1000) {
  if (miliseconds == 0)
    return Promise.resolve();
  return new Promise(resolve => setTimeout(() => resolve(), miliseconds))
}

run();
Ilan
  • 1,647
  • 1
  • 15
  • 16
  • Hmm last time I tested `Network.getResponseBody` didn't do what it supposed to do as the name said... Let me double check. – HP. Sep 09 '17 at 07:06
  • Andrea has [fixed up the REPL](https://github.com/cyrus-and/chrome-remote-interface/issues/257) to return values from commands so you should be able to run the above directly in the REPL now. NB I would suggest not using destructing (`const {body, base64Encoded} = ...`) I've occasionally received blank responses and the destructing throws - resulting in an unhandled Promise exception. – Ilan Sep 10 '17 at 11:18