Rez*_*ari 2 chromium node.js telegram puppeteer
我想用 puppeteer 打开 Telegram 网站
但存在一个问题: Telegram 会话只能在 Chrome 上打开
每次都必须使用puppeteer登录
有一种方法可以让 puppeteer 仅在正在运行的 chrome 上运行来检测会话
const browser = await puppeteer.launch({
headless : false,
executablePath: "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
args: ["--lang=en-US,en", '--no-sandbox', '--disable-setuid-sandbox', '--disable-extensions']
Run Code Online (Sandbox Code Playgroud)
})
这段代码可以正常工作,但是在 chromium 上
是的,可以在预先存在的 Chrome 进程之上运行 puppeteer 实例。
为了实现这一点,首先,您需要使用选项启动 Chrome 进程remote-debugging-port,通常定义为:--remote-debugging-port=9222
这篇Medium 文章非常详细地介绍了如何实现这一目标,但总结一下:
跑步:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222 --no-first-run --no-default-browser-check --user-data-dir=$(mktemp -d -t 'chrome-remote_data_dir')
Run Code Online (Sandbox Code Playgroud)
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222
Run Code Online (Sandbox Code Playgroud)
然后,您将能够导航到 http://localhost:9222/json/version (端口与您上面定义的端口相同),并看到如下输出:
{
"Browser": "HeadlessChrome/87.0.4280.66",
"Protocol-Version": "1.3",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/87.0.4280.66 Safari/537.36",
"V8-Version": "8.7.220.25",
"WebKit-Version": "537.36 (@fd98a29dd59b36f71e4741332c9ad5bda42094bf)",
"webSocketDebuggerUrl": "ws://localhost:9222/devtools/browser/000aaaa-bb08-55af-a8e3-760dd9998fc7"
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用 puppeteer connect() 方法(而不是launch()方法),如下所示:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222 --no-first-run --no-default-browser-check --user-data-dir=$(mktemp -d -t 'chrome-remote_data_dir')
Run Code Online (Sandbox Code Playgroud)