我有一个用例需要使用无头 Chrome 网络(https://chromedevtools.github.io/devtools-protocol/tot/Network/)来拦截所有图像请求并在保存之前找出图像大小(基本上丢弃小图标等图像)。
但是,我无法找到一种在保存之前将图像数据加载到内存中的方法。我需要将它加载到 Img 对象中以获取width
和height
. Network.getResponseBody
正在接受我无权访问的requestId Network.requestIntercepted
。也Network.loadingFinished
总是给我encodedDataLength
变量中的“0”。我不知道为什么。所以我的问题是:
如何拦截来自 jpg/png 请求的所有响应并获取图像数据?无需通过 URL 字符串将文件保存到磁盘并重新加载。
最佳:如何从标题响应中获取图像尺寸?然后我根本不必将数据读入内存。
我的代码如下:
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();
});
})();