最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Puppeteer: How to Open a New Chrome Window with the Same Profile? - Stack Overflow

programmeradmin2浏览0评论

n Puppeteer with Node.js, I have the following code. If I trigger openWebsite again, it opens a new tab. Is it possible to open a new Chrome window with the same profile instead?

Currently, my code checks if an existing window is still open before opening a new tab with the same profile. However, this approach causes a singleton lock issue.

Instead of opening a new tab, I want to open a separate Chrome window while maintaining the same profile.

const axios = require('axios');
const puppeteer = require('puppeteer-extra');

const openWebsite = async ({ url }) => {
  const browser = await connectToExistingBrowser();
  const page = await browser.newPage();

  await page.goto(url, { waitUntil: "networkidle2" });
}

async function isBrowserRunning() {
  try {
    const response = await axios.get("http://127.0.0.1:9222/json/version");
    return response.data.webSocketDebuggerUrl;
  } catch (error) {
    return null;
  }
}

async function connectToExistingBrowser() {
  const wsUrl = await isBrowserRunning();
  if (!wsUrl) {
    return await puppeteer.launch({
      headless: false,
      executablePath: /usr/bin/google-chrome,
      args: [
        '--remote-debugging-port=9222',
        '--user-data-dir=/home/fe/.config/google-chrome',
        '--profile-directory=Default',
        '--new-window'
      ],
    });
  }

  return await puppeteer.connect({ browserWSEndpoint: wsUrl });
}
发布评论

评论列表(0)

  1. 暂无评论