feature: Add browserPath option

pull/152/head
Alex Shand 2022-12-17 00:02:33 +10:00
rodzic abd6dd1c2f
commit e5cad7a55e
2 zmienionych plików z 22 dodań i 5 usunięć

Wyświetl plik

@ -3,7 +3,11 @@ import type { Browser, HTTPRequest, HTTPResponse, Page } from 'puppeteer'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import * as types from './types' import * as types from './types'
import { getBrowser, getOpenAIAuth } from './openai-auth' import {
defaultChromeExecutablePath,
getBrowser,
getOpenAIAuth
} from './openai-auth'
import { import {
browserPostEventStream, browserPostEventStream,
isRelevantRequest, isRelevantRequest,
@ -22,6 +26,7 @@ export class ChatGPTAPIBrowser {
protected _email: string protected _email: string
protected _password: string protected _password: string
protected _browserPath: string
protected _browser: Browser protected _browser: Browser
protected _page: Page protected _page: Page
@ -46,6 +51,9 @@ export class ChatGPTAPIBrowser {
/** @defaultValue `undefined` **/ /** @defaultValue `undefined` **/
captchaToken?: string captchaToken?: string
/** @defaultValue `undefined` **/
browserPath?: string
}) { }) {
const { const {
email, email,
@ -54,7 +62,8 @@ export class ChatGPTAPIBrowser {
debug = false, debug = false,
isGoogleLogin = false, isGoogleLogin = false,
minimize = true, minimize = true,
captchaToken captchaToken,
browserPath = defaultChromeExecutablePath()
} = opts } = opts
this._email = email this._email = email
@ -65,6 +74,7 @@ export class ChatGPTAPIBrowser {
this._isGoogleLogin = !!isGoogleLogin this._isGoogleLogin = !!isGoogleLogin
this._minimize = !!minimize this._minimize = !!minimize
this._captchaToken = captchaToken this._captchaToken = captchaToken
this._browserPath = browserPath
} }
async init() { async init() {
@ -75,7 +85,10 @@ export class ChatGPTAPIBrowser {
} }
try { try {
this._browser = await getBrowser({ captchaToken: this._captchaToken }) this._browser = await getBrowser({
captchaToken: this._captchaToken,
executablePath: this._browserPath
})
this._page = this._page =
(await this._browser.pages())[0] || (await this._browser.newPage()) (await this._browser.pages())[0] || (await this._browser.newPage())

Wyświetl plik

@ -186,7 +186,11 @@ export async function getBrowser(
captchaToken?: string captchaToken?: string
} = {} } = {}
) { ) {
const { captchaToken = process.env.CAPTCHA_TOKEN, ...launchOptions } = opts const {
captchaToken = process.env.CAPTCHA_TOKEN,
executablePath = defaultChromeExecutablePath(),
...launchOptions
} = opts
if (captchaToken && !hasRecaptchaPlugin) { if (captchaToken && !hasRecaptchaPlugin) {
hasRecaptchaPlugin = true hasRecaptchaPlugin = true
@ -207,7 +211,7 @@ export async function getBrowser(
headless: false, headless: false,
args: ['--no-sandbox', '--exclude-switches', 'enable-automation'], args: ['--no-sandbox', '--exclude-switches', 'enable-automation'],
ignoreHTTPSErrors: true, ignoreHTTPSErrors: true,
executablePath: defaultChromeExecutablePath(), executablePath,
...launchOptions ...launchOptions
}) })
} }