Along with this answer here and here, here is how you can do this. Basically the idea is to run chrome on different docker and connect to it from another, then use that whenever we need. It will need some maintenance, error handling, timeouts and concurrency, but that is not the issue here.
Master
You save puppeteer on master account, you do not install chrome when installing puppeteer there with PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = true
, use this one to connect to your worker puppeteers running on another docker.
const browser = await puppeteer.connect({
browserWSEndpoint: "ws://123.123.123.123:8080",
ignoreHTTPSErrors: true
});
Worker
You setup a fully running chrome here, expose the websocket. There are different ways to do this. Here is the simplest one.
const http = require('http');
const httpProxy = require('http-proxy');
const proxy = new httpProxy.createProxyServer();
http
.createServer()
.on('upgrade', async(req, socket, head) => {
const browser = await puppeteer.launch();
const target = browser.wsEndpoint();
proxyy.ws(req, socket, head, { target })
})
.listen(8080);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…