Skip to content

Commit 9589509

Browse files
committed
improve 074b6cc
1 parent 074b6cc commit 9589509

File tree

1 file changed

+51
-38
lines changed

1 file changed

+51
-38
lines changed

src/services/apis/chatgpt-web.mjs

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,39 @@ export async function getRequirementsToken(accessToken) {
5858
}
5959
}
6060

61+
export async function getArkoseToken(config) {
62+
if (!config.chatgptArkoseReqUrl)
63+
throw new Error(
64+
t('Please login at https://chat.openai.com first') +
65+
'\n\n' +
66+
t(
67+
"Please keep https://chat.openai.com open and try again. If it still doesn't work, type some characters in the input box of chatgpt web page and try again.",
68+
),
69+
)
70+
const arkoseToken = await fetch(
71+
config.chatgptArkoseReqUrl + '?' + config.chatgptArkoseReqParams,
72+
{
73+
method: 'POST',
74+
body: config.chatgptArkoseReqForm,
75+
headers: {
76+
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
77+
},
78+
},
79+
)
80+
.then((resp) => resp.json())
81+
.then((resp) => resp.token)
82+
.catch(() => null)
83+
if (!arkoseToken)
84+
throw new Error(
85+
t('Failed to get arkose token.') +
86+
'\n\n' +
87+
t(
88+
"Please keep https://chat.openai.com open and try again. If it still doesn't work, type some characters in the input box of chatgpt web page and try again.",
89+
),
90+
)
91+
return arkoseToken
92+
}
93+
6194
/**
6295
* @param {Runtime.Port} port
6396
* @param {string} question
@@ -82,52 +115,34 @@ export async function generateAnswersWithChatgptWebApi(port, question, session,
82115
},
83116
)
84117

85-
const models = await getModels(accessToken).catch(cleanController)
86-
const requirementsToken = await getRequirementsToken(accessToken)
87-
console.debug('models', models)
88118
const config = await getUserConfig()
119+
const [models, requirementsToken, arkoseToken] = await Promise.all([
120+
getModels(accessToken).catch(cleanController),
121+
getRequirementsToken(accessToken),
122+
getArkoseToken(config),
123+
])
124+
console.debug('models', models)
89125
const selectedModel = Models[session.modelName].value
90126
const usedModel =
91127
models && models.includes(selectedModel) ? selectedModel : Models[chatgptWebModelKeys[0]].value
92128
console.debug('usedModel', usedModel)
93129

94130
let cookie
95-
if (Browser.cookies && Browser.cookies.getAll)
131+
let oaiDeviceId
132+
if (Browser.cookies && Browser.cookies.getAll) {
96133
cookie = (await Browser.cookies.getAll({ url: 'https://chat.openai.com/' }))
97134
.map((cookie) => {
98135
return `${cookie.name}=${cookie.value}`
99136
})
100137
.join('; ')
101-
102-
const needArkoseToken = !usedModel.includes(Models[chatgptWebModelKeys[0]].value)
103-
if (needArkoseToken && !config.chatgptArkoseReqUrl)
104-
throw new Error(
105-
t('Please login at https://chat.openai.com first') +
106-
'\n\n' +
107-
t(
108-
"Please keep https://chat.openai.com open and try again. If it still doesn't work, type some characters in the input box of chatgpt web page and try again.",
109-
),
110-
)
111-
const arkoseToken = config.chatgptArkoseReqUrl
112-
? await fetch(config.chatgptArkoseReqUrl + '?' + config.chatgptArkoseReqParams, {
113-
method: 'POST',
114-
body: config.chatgptArkoseReqForm,
115-
headers: {
116-
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
117-
},
138+
oaiDeviceId = (
139+
await Browser.cookies.get({
140+
url: 'https://openai.com/',
141+
name: 'oai-did',
118142
})
119-
.then((resp) => resp.json())
120-
.then((resp) => resp.token)
121-
.catch(() => null)
122-
: null
123-
if (needArkoseToken && !arkoseToken)
124-
throw new Error(
125-
t('Failed to get arkose token.') +
126-
'\n\n' +
127-
t(
128-
"Please keep https://chat.openai.com open and try again. If it still doesn't work, type some characters in the input box of chatgpt web page and try again.",
129-
),
130-
)
143+
).value
144+
}
145+
131146
let answer = ''
132147
let generationPrefixAnswer = ''
133148
let generatedImageUrl = ''
@@ -144,6 +159,8 @@ export async function generateAnswersWithChatgptWebApi(port, question, session,
144159
...(cookie && { Cookie: cookie }),
145160
'Openai-Sentinel-Arkose-Token': arkoseToken || '',
146161
'Openai-Sentinel-Chat-Requirements-Token': requirementsToken || '',
162+
'Oai-Device-Id': oaiDeviceId,
163+
'Oai-Language': 'en-US',
147164
},
148165
body: JSON.stringify({
149166
action: 'next',
@@ -177,11 +194,7 @@ export async function generateAnswersWithChatgptWebApi(port, question, session,
177194
onMessage(message) {
178195
function handleMessage(data) {
179196
if (data.error) {
180-
if (data.error.includes('unusual activity'))
181-
throw new Error(
182-
"Please keep https://chat.openai.com open and try again. If it still doesn't work, type some characters in the input box of chatgpt web page and try again.",
183-
)
184-
else throw new Error(data.error)
197+
throw new Error(data.error)
185198
}
186199

187200
if (data.conversation_id) session.conversationId = data.conversation_id

0 commit comments

Comments
 (0)