101 lines
4.1 KiB
JavaScript
101 lines
4.1 KiB
JavaScript
// puppeteer-multiple-profiles.js
|
|
import puppeteer from 'puppeteer';
|
|
|
|
|
|
|
|
const boot_strap_url = 'https://localhost:6789/?log&headless&bootstrap'
|
|
const number_of_peers = 3; // Number of peers to open in separate contexts
|
|
const peer_url = 'https://localhost:6789/'; // URL for the peer page
|
|
// Define an array of colors for console output the lenths of the number of peers or the max number of console colors looped
|
|
const colors =[]
|
|
for (let i = 0; i < number_of_peers; i++) {
|
|
// Generate a color code for each peer
|
|
const colorCode = 31 + (i % 6); // Cycle through ANSI colors 31-36
|
|
colors.push(`\x1b[${colorCode}m`); // ANSI escape code for color
|
|
}
|
|
async function openPagesInSeparateContexts() {
|
|
let browser;
|
|
try {
|
|
console.log('Launching browser...');
|
|
browser = await puppeteer.launch({
|
|
headless: true, // Set to true if you don't want to see the browser UI
|
|
args: [
|
|
'--disable-gpu',
|
|
'--no-sandbox',
|
|
'--disable-setuid-sandbox',
|
|
'--allow-insecure-localhost' // For local HTTPS with self-signed certs
|
|
// Add other args if needed
|
|
],
|
|
// For local HTTPS with self-signed certs
|
|
ignoreHTTPSErrors: true
|
|
});
|
|
|
|
console.log('Browser launched.');
|
|
|
|
const context = await browser.createBrowserContext();
|
|
const page = await context.newPage();
|
|
await page.goto(boot_strap_url, {
|
|
waitUntil: 'networkidle2', // Wait until network is relatively idle
|
|
// timeout: 60000 // Optional: increase timeout if pages load slowly
|
|
});
|
|
console.log(`Successfully navigated to ${boot_strap_url} in initial context`);
|
|
|
|
|
|
for (let i = 0; i < number_of_peers; i++) {
|
|
console.log(`Creating new browser context ${i + 1}...`);
|
|
// --- USE THE NEW METHOD ---
|
|
const context = await browser.createBrowserContext();
|
|
console.log(`Opening new page in context ${i + 1} for URL: ${peer_url}`);
|
|
const page = await context.newPage();
|
|
|
|
// --- Array to store console messages for this page ---
|
|
const consoleMessages = [];
|
|
// --- Listen for console events ---
|
|
page.on('console', msg => {
|
|
const messageText = msg.text();
|
|
|
|
console.log(`${colors[i]}\[Browser Console - Page ${i + 1}]: ${messageText}\x1b[0m`);
|
|
consoleMessages.push(messageText); // Store the message
|
|
});
|
|
|
|
// listen for other events like 'pageerror' for JavaScript errors on the page
|
|
page.on('pageerror', error => {
|
|
console.error(`[Page Error - Page ${i + 1}]: ${error.message}`);
|
|
});
|
|
|
|
// set ignoreHTTPSErrors on a per-page basis if we didn't set it globally
|
|
// await page.setBypassCSP(true); // Might be needed for some sites
|
|
// await page.setExtraHTTPHeaders({ 'Accept-Language': 'en-US' }); // Example header
|
|
|
|
console.log(`Navigating to ${peer_url}...`);
|
|
await page.goto(peer_url, {
|
|
waitUntil: 'networkidle2', // Wait until network is relatively idle
|
|
// timeout: 60000 // Optional: increase timeout if pages load slowly
|
|
});
|
|
console.log(`Successfully navigated to ${peer_url} in context ${i + 1}`);
|
|
|
|
// You can interact with the page here if needed
|
|
// const title = await page.title();
|
|
// console.log(`Page title for ${url}: ${title}`);
|
|
}
|
|
|
|
console.log('All pages opened in separate contexts.');
|
|
console.log('Browser will remain open. Close it manually or the script will exit after a delay if not kept alive.');
|
|
|
|
// Keep the browser open for a while, or remove this to close immediately after navigation
|
|
// await new Promise(resolve => setTimeout(resolve, 300000)); // Keep open for 5 minutes
|
|
|
|
} catch (error) {
|
|
console.error('An error occurred:', error);
|
|
} finally {
|
|
// If you want the script to close the browser automatically after some time or after operations:
|
|
// if (browser) {
|
|
// console.log('Closing browser...');
|
|
// await browser.close();
|
|
// console.log('Browser closed.');
|
|
// }
|
|
// If you want to keep it open indefinitely until manual closure, comment out the finally block or the browser.close() call.
|
|
}
|
|
}
|
|
|
|
openPagesInSeparateContexts(); |