Reza Salari
Reza Salari

Reputation: 131

Run puppeteer on Chrome No chromium

I want to open Telegram site with puppeteer

But there is a problem Telegram session only opens on Chrome

You must login with puppeteer each time

There is a way for the puppeteer to run only on the running chrome to detect the session

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']

})

This code works properly But on chromium

Upvotes: 2

Views: 4946

Answers (1)

tmilar
tmilar

Reputation: 1921

Yes, it's possible to run a puppeteer instance on top of an pre-existing Chrome process.

In order to achieve this, first, you need to start the Chrome process with the remote-debugging-port option, usually defined as: --remote-debugging-port=9222

This Medium articule is well detailed on how to achieve so, but to summarize:

  • MAC OS:

Run:

/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')
  • Windows:
  1. Right click on your Google Chrome shortcut icon => Properties
  2. In Target field, add to the very end --remote-debugging-port=9222 Should look something like:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222

Then, you'll be able to navigate to http://localhost:9222/json/version (the port is the same you've defined above), and see an output like this:

{
   "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"
}

Then, you can use the puppeteer connect() method (instead of the launch() method) like this:

  const browser = await puppeteer.connect({
    browserWSEndpoint: "ws://localhost:9222/devtools/browser/000aaaa-bb08-55af-a8e3-760dd9998fc7",
  });
  // now, 'browser' is connected to your chrome window.
  
  // get the opened pages
  const openedPages = await browser.pages();

  // filter out the one you want (telegram). not sure the best way to do it, please test it yourself
  const telegramPage = openedPages.filter(page => page.url().includes("telegram"));

Upvotes: 7

Related Questions