Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
Chatgpt Puppeteer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
Chatgpt Puppeteer
Commits
d57db8e7
Commit
d57db8e7
authored
Dec 26, 2022
by
Travis Fischer
Committed by
GitHub
Dec 26, 2022
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #200 from skippyyy/main
parents
bff9d0e1
8d4abc36
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
6 deletions
+43
-6
src/chatgpt-api-browser.ts
src/chatgpt-api-browser.ts
+43
-6
No files found.
src/chatgpt-api-browser.ts
View file @
d57db8e7
...
@@ -32,6 +32,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -32,6 +32,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
protected
_browser
:
Browser
protected
_browser
:
Browser
protected
_page
:
Page
protected
_page
:
Page
protected
_proxyServer
:
string
protected
_proxyServer
:
string
protected
_isRefreshing
:
boolean
/**
/**
* Creates a new client for automating the ChatGPT webapp.
* Creates a new client for automating the ChatGPT webapp.
...
@@ -95,6 +96,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -95,6 +96,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
this
.
_nopechaKey
=
nopechaKey
this
.
_nopechaKey
=
nopechaKey
this
.
_executablePath
=
executablePath
this
.
_executablePath
=
executablePath
this
.
_proxyServer
=
proxyServer
this
.
_proxyServer
=
proxyServer
this
.
_isRefreshing
=
false
if
(
!
this
.
_email
)
{
if
(
!
this
.
_email
)
{
const
error
=
new
types
.
ChatGPTError
(
'
ChatGPT invalid email
'
)
const
error
=
new
types
.
ChatGPTError
(
'
ChatGPT invalid email
'
)
...
@@ -157,7 +159,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -157,7 +159,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
this
.
_page
.
on
(
'
response
'
,
this
.
_onResponse
.
bind
(
this
))
this
.
_page
.
on
(
'
response
'
,
this
.
_onResponse
.
bind
(
this
))
// bypass cloudflare and login
// bypass cloudflare and login
await
getOpenAIAuth
({
var
authInfo
=
await
getOpenAIAuth
({
email
:
this
.
_email
,
email
:
this
.
_email
,
password
:
this
.
_password
,
password
:
this
.
_password
,
browser
:
this
.
_browser
,
browser
:
this
.
_browser
,
...
@@ -165,6 +167,8 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -165,6 +167,8 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
isGoogleLogin
:
this
.
_isGoogleLogin
,
isGoogleLogin
:
this
.
_isGoogleLogin
,
isMicrosoftLogin
:
this
.
_isMicrosoftLogin
isMicrosoftLogin
:
this
.
_isMicrosoftLogin
})
})
console
.
log
(
'
Cloudflare Cookie:
'
,
authInfo
.
clearanceToken
)
console
.
log
(
'
Useragent:
'
,
authInfo
.
userAgent
)
}
catch
(
err
)
{
}
catch
(
err
)
{
if
(
this
.
_browser
)
{
if
(
this
.
_browser
)
{
await
this
.
_browser
.
close
()
await
this
.
_browser
.
close
()
...
@@ -278,7 +282,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -278,7 +282,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
}
}
if
(
url
.
endsWith
(
'
/conversation
'
))
{
if
(
url
.
endsWith
(
'
/conversation
'
))
{
if
(
status
===
403
)
{
if
(
status
===
403
&&
!
this
.
_isRefreshing
)
{
await
this
.
refreshSession
()
await
this
.
refreshSession
()
}
}
}
else
if
(
url
.
endsWith
(
'
api/auth/session
'
))
{
}
else
if
(
url
.
endsWith
(
'
api/auth/session
'
))
{
...
@@ -325,15 +329,45 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -325,15 +329,45 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
* Attempts to handle 403 errors by refreshing the page.
* Attempts to handle 403 errors by refreshing the page.
*/
*/
async
refreshSession
()
{
async
refreshSession
()
{
this
.
_isRefreshing
=
true
console
.
log
(
`ChatGPT "
${
this
.
_email
}
" session expired (403); refreshing...`
)
console
.
log
(
`ChatGPT "
${
this
.
_email
}
" session expired (403); refreshing...`
)
try
{
try
{
if
(
!
this
.
_minimize
)
{
if
(
!
this
.
_minimize
)
{
await
maximizePage
(
this
.
_page
)
await
maximizePage
(
this
.
_page
)
}
}
await
this
.
_page
.
reload
({
await
this
.
_page
.
reload
()
waitUntil
:
'
networkidle2
'
,
let
response
timeout
:
2
*
60
*
1000
// 2 minutes
const
timeout
=
120000
// 2 minutes in milliseconds
})
// Wait for a response that includes the 'cf_clearance' cookie
try
{
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
let
check
=
setCookie
.
includes
(
'
cf_clearance=
'
)
if
(
check
)
{
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
let
cf_clearance
=
setCookie
.
split
(
'
cf_clearance=
'
)[
1
]
.
split
(
'
;
'
)[
0
]
console
.
log
(
'
Cloudflare Cookie:
'
,
cf_clearance
)
}
return
check
}
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
)
}
}
...
@@ -343,6 +377,8 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -343,6 +377,8 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
`ChatGPT "
${
this
.
_email
}
" error refreshing session`
,
`ChatGPT "
${
this
.
_email
}
" error refreshing session`
,
err
.
toString
()
err
.
toString
()
)
)
}
finally
{
this
.
_isRefreshing
=
false
}
}
}
}
...
@@ -532,6 +568,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -532,6 +568,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
}
else
{
}
else
{
await
this
.
refreshSession
()
await
this
.
refreshSession
()
await
delay
(
1000
)
await
delay
(
1000
)
result
=
null
continue
continue
}
}
}
else
{
}
else
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment