背景
在使用 puppeteer 开启 {headless: true}
的情况下多一个页面多次生成pdf;为了减少网络请求,在打开页面的时候,做了网络请求拦截,来转发存储的数据。但存在程序突然卡着不动了。此时又因为部署在服务器,没办法使用浏览器调试。
期望:本地机器可用连接远程服务器进行调试
调试步骤
-
启动 puppeteer 添加参数:
remote-debugging-port
,remote-debugging-address
remote-debugging-port
: 指定远程链接服务chrome调试端口remote-debugging-address
: 一般情况下,chrome只允许使用127.0.0.1
对启动的浏览器进行调试,设置该参数,可以允许其他电脑通过ip进行调试
启动 puppeteer 代码如下:
const puppeteerInstance = await puppeteer.launch({ ignoreHTTPSErrors: true, args: ['--no-sandbox', '--disable-setuid-sandbox', "--remote-debugging-port=18523", "--remote-debugging-address=0.0.0.0"], // executablePath: Config.browser.path, headless: true });
-
打开 http://127.0.0.1:18523/json 可以看到 puppeteer 的情况
- 看上图,通过 devtoolsFrontendUrl 可以查看打开页面如: http://127.0.0.1:18523/devtools/inspector.html?ws=127.0.0.1:18523/devtools/page/7E9F348DB6CC9A6876AA7824DE4A805B
4.由于配置了 remote-debugging-address
所以在其他机器通过服务器ip访问调试
- http://[远程服务器ip]:18523/json
- http://[远程服务器ip]:18523/devtools/page/7E9F348DB6CC9A6876AA7824DE4A805B