From 8b24a9e80a3f699a9b901246e811bf03f042d7dd Mon Sep 17 00:00:00 2001 From: Skippy <82155626+skippyyy@users.noreply.github.com> Date: Sun, 25 Dec 2022 10:38:31 +0200 Subject: [PATCH] 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 --- src/chatgpt-api-browser.ts | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/chatgpt-api-browser.ts b/src/chatgpt-api-browser.ts index 0fcc221..4ba37c2 100644 --- a/src/chatgpt-api-browser.ts +++ b/src/chatgpt-api-browser.ts @@ -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) }