Improve refreshSession

Waiting for cloudflare cookie to be received which fixes the issue of non-authenticated false-postive due to the refreshSession() function returning while the CF anti-bot is still on-going
pull/200/head
Skippy 2022-12-25 10:38:31 +02:00
rodzic de3b60f1f8
commit 8b24a9e80a
1 zmienionych plików z 21 dodań i 4 usunięć

Wyświetl plik

@ -332,10 +332,27 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
if (!this._minimize) {
await maximizePage(this._page)
}
await this._page.reload({
waitUntil: 'networkidle2',
timeout: 2 * 60 * 1000 // 2 minutes
})
await this._page.reload()
// Wait for a response that includes the 'cf_clearance' cookie
const timeout = 30000 // 30 seconds in milliseconds
const response = await this._page.waitForResponse(
(response) => {
// Check if the `set-cookie` header exists in the response headers
const setCookie = response.headers()['set-cookie']
if (setCookie) {
// Check if the `set-cookie` value contains the `cf_clearance=` string
return setCookie.includes('cf_clearance=')
}
return false
},
{ timeout }
)
if (response) {
console.log('Found cf_clearance in set-cookie header')
} else {
throw new types.ChatGPTError('Failed to refresh session')
}
if (this._minimize && this.isChatPage) {
await minimizePage(this._page)
}