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 });
}