Minor Cleanups to improve efficiency

pull/200/head
Skippy 2022-12-25 19:35:56 +02:00
rodzic bfa0a90a12
commit 8d4abc36a3
1 zmienionych plików z 30 dodań i 28 usunięć

Wyświetl plik

@ -336,40 +336,42 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
await maximizePage(this._page) await maximizePage(this._page)
} }
await this._page.reload() await this._page.reload()
let response
const timeout = 120000 // 2 minutes in milliseconds
// Wait for a response that includes the 'cf_clearance' cookie // Wait for a response that includes the 'cf_clearance' cookie
const timeout = 30000 // 30 seconds in milliseconds try {
const response = await this._page.waitForResponse( response = await this._page.waitForResponse(
(response) => { (response) => {
// Check if the `set-cookie` header exists in the response headers // Check if the `set-cookie` header exists in the response headers
const setCookie = response.headers()['set-cookie'] const setCookie = response.headers()['set-cookie']
if (setCookie) { if (setCookie) {
// Check if the `set-cookie` value contains the `cf_clearance=` string // Check if the `set-cookie` value contains the `cf_clearance=` string
return setCookie.includes('cf_clearance=') let check = setCookie.includes('cf_clearance=')
} if (check) {
return false console.log('Found cf_clearance in set-cookie header')
}, // split setCookie at cf-clearance= and get the second part, then remove the semicolon at the end
{ timeout } let cf_clearance = setCookie
) .split('cf_clearance=')[1]
.split(';')[0]
if (response) { console.log('Cloudflare Cookie:', cf_clearance)
console.log('Found cf_clearance in set-cookie header') }
} else { return check
throw new types.ChatGPTError('Failed to refresh session') }
return false
},
{ timeout }
)
} catch (err) {
// useful for when cloudflare cookie is still valid, to catch TimeoutError
response = !!(await this._getInputBox())
}
if (!response) {
throw new types.ChatGPTError('Could not fetch cf_clearance cookie')
} }
if (this._minimize && this.isChatPage) { if (this._minimize && this.isChatPage) {
await minimizePage(this._page) await minimizePage(this._page)
} }
console.log(`ChatGPT "${this._email}" refreshed session successfully`) console.log(`ChatGPT "${this._email}" refreshed session successfully`)
const pageCookies = await this._page.cookies()
const cookies = pageCookies.reduce(
(map, cookie) => ({ ...map, [cookie.name]: cookie }),
{}
)
try {
console.log('Cloudflare Cookie: ', cookies['cf_clearance'].value)
} catch (error) {
console.log('Cloudflare Cookie: ', 'Not Found')
}
} catch (err) { } catch (err) {
console.error( console.error(
`ChatGPT "${this._email}" error refreshing session`, `ChatGPT "${this._email}" error refreshing session`,