如前所述here,谁能帮我在 Node.js 中使用 Lighthouse Chrome 启动器时为 headless Chrome 设置代理服务器?
const launcher = new ChromeLauncher({
port: 9222,
autoSelectChrome: true, // False to manually select which Chrome install.
additionalFlags: [
'--window-size=412,732',
'--disable-gpu',
'--proxy-server="IP:PORT"',
headless ? '--headless' : ''
]
});
但是,上面的脚本根本没有命中我的代理服务器。 Chrome 似乎回退到 DIRECT://连接到目标网站。
关于在 headless chrome 上下文中使用 HTTP/HTTPS 代理服务器的另一个资源是 this .但它没有给出任何如何在 Node.js 中使用它的示例。
最佳答案
我用常规的 exec
试了一下,效果很好,这是我的代码片段:
const exec = require('child_process').exec;
function launchHeadlessChrome(url, callback) {
// Assuming MacOSx.
const CHROME = '/Users/h0x91b/Desktop/Google\\ Chrome\\ Beta.app/Contents/MacOS/Google\\ Chrome';
exec(`${CHROME} --headless --disable-gpu --remote-debugging-port=9222 --proxy-server=127.0.0.1:8888 ${url}`, callback);
}
launchHeadlessChrome('https://www.chromestatus.com', (err, stdout, stderr) => {
console.log('callback', err, stderr, stdout)
});
然后我导航到 http://localhost:9222在开发人员工具中,我看到:
代理连接错误,没关系,因为我在这个端口上没有代理,但这意味着 Chrome 试图通过代理连接...
顺便说一句,Chrome 版本是 59。
查看了源码https://github.com/GoogleChrome/lighthouse/blob/master/chrome-launcher/chrome-launcher.ts#L38-L44
我在这里没有看到additionalFlags
,只有chromeFlags
尝试使用它...
关于node.js - headless chrome 代理服务器设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43868871/