diff --git a/lib/cjs/index.js b/lib/cjs/index.js index ed29002..ca63854 100644 --- a/lib/cjs/index.js +++ b/lib/cjs/index.js @@ -1,78 +1,113 @@ -var puppeteer = require('rebrowser-puppeteer-core') -const { pageController } = require('./module/pageController.js') -const Xvfb = require('xvfb'); +var puppeteer = require("rebrowser-puppeteer-core"); +const { pageController } = require("./module/pageController.js"); +const Xvfb = require("xvfb"); -async function connect({ args = [], headless = false, customConfig = {}, proxy = {}, turnstile = false, connectOption = {}, disableXvfb = false, plugins = [], ignoreAllFlags = false }) { - const { launch, Launcher } = await import('chrome-launcher'); +async function connect({ + args = [], + headless = false, + customConfig = {}, + proxy = {}, + turnstile = false, + connectOption = {}, + disableXvfb = false, + plugins = [], + ignoreAllFlags = false, +}) { + const { launch, Launcher } = await import("chrome-launcher"); - let xvfbsession = null - if (headless == 'auto') headless = false + let xvfbsession = null; + if (headless == "auto") headless = false; - if (process.platform === 'linux' && disableXvfb === false) { - try { - xvfbsession = new Xvfb({ - silent: true, - xvfb_args: ['-screen', '0', '1920x1080x24', '-ac'] - }); - xvfbsession.startSync(); - } catch (err) { - console.log('You are running on a Linux platform but do not have xvfb installed. The browser can be captured. Please install it with the following command\n\nsudo apt-get install xvfb\n\n' + err.message); - } + if (process.platform === "linux" && disableXvfb === false) { + try { + xvfbsession = new Xvfb({ + silent: true, + xvfb_args: ["-screen", "0", "1920x1080x24", "-ac"], + }); + xvfbsession.startSync(); + } catch (err) { + console.log( + "You are running on a Linux platform but do not have xvfb installed. The browser can be captured. Please install it with the following command\n\nsudo apt-get install xvfb\n\n" + + err.message + ); } + } - const chrome = await launch({ - ignoreDefaultFlags: true, - chromeFlags: [ - ...( - (ignoreAllFlags === true) - ? [ - ...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []), - ...args, ...((headless !== false) ? [`--headless=${headless}`] : []), - ] : [ - ...Launcher.defaultFlags().filter(item => !item.includes("--disable-features") && !item.includes("component-update")), - ...args, ...((headless !== false) ? [`--headless=${headless}`] : []), - ...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []), - '--disable-features=Translate,OptimizationHints,MediaRouter,DialMediaRouteProvider,CalculateNativeWinOcclusion,InterestFeedContentSuggestions,CertificateTransparencyComponentUpdater,AutofillServerCommunication,PrivacySandboxSettings4,AutomationControlled', - "--no-sandbox" - ] + const chrome = await launch({ + ignoreDefaultFlags: true, + chromeFlags: [ + ...(ignoreAllFlags === true + ? [ + ...(proxy && proxy.host && proxy.port + ? [`--proxy-server=${proxy.host}:${proxy.port}`] + : []), + ...args, + ...(headless !== false ? [`--headless=${headless}`] : []), + ] + : [ + ...Launcher.defaultFlags().filter( + (item) => + !item.includes("--disable-features") && + !item.includes("component-update") ), - ], - ...customConfig - }); + ...args, + ...(headless !== false ? [`--headless=${headless}`] : []), + ...(proxy && proxy.host && proxy.port + ? [`--proxy-server=${proxy.host}:${proxy.port}`] + : []), + "--disable-features=Translate,OptimizationHints,MediaRouter,DialMediaRouteProvider,CalculateNativeWinOcclusion,InterestFeedContentSuggestions,CertificateTransparencyComponentUpdater,AutofillServerCommunication,PrivacySandboxSettings4,AutomationControlled", + "--no-sandbox", + "--disable-dev-shm-usage", + ]), + ], + ...customConfig, + }); - if (plugins.length > 0) { - const { addExtra } = await import('puppeteer-extra'); + if (plugins.length > 0) { + const { addExtra } = await import("puppeteer-extra"); - puppeteer = addExtra(puppeteer); + puppeteer = addExtra(puppeteer); - for (const item of plugins) { - puppeteer.use(item); - } + for (const item of plugins) { + puppeteer.use(item); } + } - const browser = await puppeteer.connect({ - browserURL: `http://127.0.0.1:${chrome.port}`, - ...connectOption - }); + const browser = await puppeteer.connect({ + browserURL: `http://127.0.0.1:${chrome.port}`, + ...connectOption, + }); - let [page] = await browser.pages(); + let [page] = await browser.pages(); - let pageControllerConfig = { browser, page, proxy, turnstile, xvfbsession, pid: chrome.pid, plugins } + let pageControllerConfig = { + browser, + page, + proxy, + turnstile, + xvfbsession, + pid: chrome.pid, + plugins, + }; - page = await pageController({ ...pageControllerConfig, killProcess: true, chrome }); + page = await pageController({ + ...pageControllerConfig, + killProcess: true, + chrome, + }); - browser.on('targetcreated', async target => { - if (target.type() === 'page') { - let newPage = await target.page(); - pageControllerConfig.page = newPage - newPage = await pageController(pageControllerConfig); - } - }); - - return { - browser, - page + browser.on("targetcreated", async (target) => { + if (target.type() === "page") { + let newPage = await target.page(); + pageControllerConfig.page = newPage; + newPage = await pageController(pageControllerConfig); } + }); + + return { + browser, + page, + }; } -module.exports = { connect } \ No newline at end of file +module.exports = { connect }; diff --git a/lib/esm/index.mjs b/lib/esm/index.mjs index d08cd42..3d41620 100644 --- a/lib/esm/index.mjs +++ b/lib/esm/index.mjs @@ -1,76 +1,111 @@ -import { launch, Launcher } from 'chrome-launcher'; -import puppeteer from 'rebrowser-puppeteer-core'; -import { pageController } from './module/pageController.mjs'; -import Xvfb from 'xvfb'; +import { launch, Launcher } from "chrome-launcher"; +import puppeteer from "rebrowser-puppeteer-core"; +import { pageController } from "./module/pageController.mjs"; +import Xvfb from "xvfb"; // process.env.REBROWSER_PATCHES_DEBUG=1 -export async function connect({ args = [], headless = false, customConfig = {}, proxy = {}, turnstile = false, connectOption = {}, disableXvfb = false, plugins = [], ignoreAllFlags = false }) { - let xvfbsession = null - if (headless == 'auto') headless = false +export async function connect({ + args = [], + headless = false, + customConfig = {}, + proxy = {}, + turnstile = false, + connectOption = {}, + disableXvfb = false, + plugins = [], + ignoreAllFlags = false, +}) { + let xvfbsession = null; + if (headless == "auto") headless = false; - if (process.platform === 'linux' && disableXvfb === false) { - try { - xvfbsession = new Xvfb({ - silent: true, - xvfb_args: ['-screen', '0', '1920x1080x24', '-ac'] - }); - xvfbsession.startSync(); - } catch (err) { - console.log('You are running on a Linux platform but do not have xvfb installed. The browser can be captured. Please install it with the following command\n\nsudo apt-get install xvfb\n\n' + err.message); - } + if (process.platform === "linux" && disableXvfb === false) { + try { + xvfbsession = new Xvfb({ + silent: true, + xvfb_args: ["-screen", "0", "1920x1080x24", "-ac"], + }); + xvfbsession.startSync(); + } catch (err) { + console.log( + "You are running on a Linux platform but do not have xvfb installed. The browser can be captured. Please install it with the following command\n\nsudo apt-get install xvfb\n\n" + + err.message + ); } + } - const chrome = await launch({ - ignoreDefaultFlags: true, - chromeFlags: [ - ...( - (ignoreAllFlags === true) - ? [ - ...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []), - ...args, ...((headless !== false) ? [`--headless=${headless}`] : []), - ] : [ - ...Launcher.defaultFlags().filter(item => !item.includes("--disable-features") && !item.includes("component-update")), - ...args, ...((headless !== false) ? [`--headless=${headless}`] : []), - ...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []), - '--disable-features=Translate,OptimizationHints,MediaRouter,DialMediaRouteProvider,CalculateNativeWinOcclusion,InterestFeedContentSuggestions,CertificateTransparencyComponentUpdater,AutofillServerCommunication,PrivacySandboxSettings4,AutomationControlled', - "--no-sandbox" - ] + const chrome = await launch({ + ignoreDefaultFlags: true, + chromeFlags: [ + ...(ignoreAllFlags === true + ? [ + ...(proxy && proxy.host && proxy.port + ? [`--proxy-server=${proxy.host}:${proxy.port}`] + : []), + ...args, + ...(headless !== false ? [`--headless=${headless}`] : []), + ] + : [ + ...Launcher.defaultFlags().filter( + (item) => + !item.includes("--disable-features") && + !item.includes("component-update") ), - ], - ...customConfig - }); - let pextra = null - if (plugins.length > 0) { - const { addExtra } = await import('puppeteer-extra'); + ...args, + ...(headless !== false ? [`--headless=${headless}`] : []), + ...(proxy && proxy.host && proxy.port + ? [`--proxy-server=${proxy.host}:${proxy.port}`] + : []), + "--disable-features=Translate,OptimizationHints,MediaRouter,DialMediaRouteProvider,CalculateNativeWinOcclusion,InterestFeedContentSuggestions,CertificateTransparencyComponentUpdater,AutofillServerCommunication,PrivacySandboxSettings4,AutomationControlled", + "--no-sandbox", + "--disable-dev-shm-usage", + ]), + ], + ...customConfig, + }); + let pextra = null; + if (plugins.length > 0) { + const { addExtra } = await import("puppeteer-extra"); - pextra = addExtra(puppeteer); + pextra = addExtra(puppeteer); - for (const item of plugins) { - pextra.use(item); - } + for (const item of plugins) { + pextra.use(item); } + } - const browser = await (pextra ? pextra : puppeteer).connect({ - browserURL: `http://127.0.0.1:${chrome.port}`, - ...connectOption - }); + const browser = await (pextra ? pextra : puppeteer).connect({ + browserURL: `http://127.0.0.1:${chrome.port}`, + ...connectOption, + }); - let [page] = await browser.pages(); + let [page] = await browser.pages(); - let pageControllerConfig = { browser, page, proxy, turnstile, xvfbsession, pid: chrome.pid, plugins } + let pageControllerConfig = { + browser, + page, + proxy, + turnstile, + xvfbsession, + pid: chrome.pid, + plugins, + }; - page = await pageController({ ...pageControllerConfig, chrome, killProcess: true }); + page = await pageController({ + ...pageControllerConfig, + chrome, + killProcess: true, + }); - browser.on('targetcreated', async target => { - if (target.type() === 'page') { - let newPage = await target.page(); - pageControllerConfig.page = newPage - newPage = await pageController(pageControllerConfig); - } - }); - - return { - browser, - page + browser.on("targetcreated", async (target) => { + if (target.type() === "page") { + let newPage = await target.page(); + pageControllerConfig.page = newPage; + newPage = await pageController(pageControllerConfig); } -} \ No newline at end of file + }); + + return { + browser, + page, + }; +} diff --git a/package.json b/package.json index 57c3adf..f0a6598 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "puppeteer-real-browser", - "version": "1.3.18", + "version": "1.3.20", "description": "This package is designed to bypass puppeteer's bot-detecting captchas such as Cloudflare. It acts like a real browser and can be managed with puppeteer.", "main": "lib/cjs/index.js", "module": "lib/esm/index.mjs",