使用headlesschrome拦截图片请求数据

Use headless chrome to intercept image request data

我有一个用例需要使用Headless Chrome Network (https://chromedevtools.github.io/devtools-protocol/tot/Network/)来拦截所有图片请求并在保存之前找出图片大小(基本上丢弃图标等小图片).

但是,我想不出在保存之前将图像数据加载到内存中的方法。我需要在 Img object 中加载它以获得 widthheightNetwork.getResponseBody 正在获取我在 Network.requestIntercepted 中无权访问的 requestId。另外 Network.loadingFinished 总是在 encodedDataLength 变量中给我“0”。我不知道为什么。所以我的问题是:

  1. 如何拦截jpg/png请求的所有响应并获取图片数据?不通过 URL 字符串将文件保存到磁盘并加载回来。

  2. BEST:如何从 header 响应中获取图像尺寸?那我就完全不用把数据读入内存了

我的代码如下:

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(); 
  });

})();

这应该让你完成了 90%。它获取每个图像请求的主体。您仍然需要进行 base64 解码、检查大小和保存等...

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();