无头 Chrome 代理服务器设置

7

我想请问如何在使用Node.js中的Lighthouse Chrome Launcher时为无头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://连接到目标网站。
在使用无头浏览器Chrome的HTTP/HTTPS代理服务器方面,另一个资源是this。但它没有给出如何从Node.js使用的任何示例。
1个回答

6

我尝试使用普通的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,在开发者工具中看到: enter image description here 代理连接错误,这没关系,因为我没有在此端口上使用代理,但这意味着Chrome尝试通过代理连接...
顺便说一下,Chrome版本是59。
检查了源代码https://github.com/GoogleChrome/lighthouse/blob/master/chrome-launcher/chrome-launcher.ts#L38-L44 我在这里看不到additionalFlags,只有chromeFlags,尝试使用它...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接