feat: improve chat page detection for browser version

pull/155/head
Travis Fischer 2022-12-16 23:39:44 -06:00
rodzic 1af5db2471
commit 619ddd9148
1 zmienionych plików z 19 dodań i 6 usunięć

Wyświetl plik

@ -8,10 +8,13 @@ import { getBrowser, getOpenAIAuth } from './openai-auth'
import {
browserPostEventStream,
isRelevantRequest,
markdownToText,
maximizePage,
minimizePage
} from './utils'
const CHAT_PAGE_URL = 'https://chat.openai.com/chat'
export class ChatGPTAPIBrowser extends AChatGPTAPI {
protected _markdown: boolean
protected _debug: boolean
@ -134,11 +137,8 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
throw err
}
const chatUrl = 'https://chat.openai.com/chat'
const url = this._page.url().replace(/\/$/, '')
if (url !== chatUrl) {
await this._page.goto(chatUrl, {
if (!this.isChatPage) {
await this._page.goto(CHAT_PAGE_URL, {
waitUntil: 'networkidle2'
})
}
@ -287,7 +287,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
waitUntil: 'networkidle2',
timeout: 2 * 60 * 1000 // 2 minutes
})
if (this._minimize) {
if (this._minimize && this.isChatPage) {
await minimizePage(this._page)
}
console.log(`ChatGPT "${this._email}" refreshed session successfully`)
@ -451,6 +451,10 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
throw error
} else {
if (!this._markdown) {
result.response = markdownToText(result.response)
}
return result
}
@ -516,4 +520,13 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
// [data-id="root"]
return this._page.$('textarea')
}
get isChatPage(): boolean {
try {
const url = this._page?.url().replace(/\/$/, '')
return url === CHAT_PAGE_URL
} catch (err) {
return false
}
}
}