From 4859e9a905e8a405d2a5c40d526bec1b2ea112ed Mon Sep 17 00:00:00 2001 From: josc146 Date: Mon, 30 Oct 2023 12:58:04 +0800 Subject: [PATCH] fix chatgpt web mode arkose_token (#469, #543, #545) --- src/_locales/de/main.json | 3 ++- src/_locales/en/main.json | 3 ++- src/_locales/es/main.json | 3 ++- src/_locales/fr/main.json | 3 ++- src/_locales/in/main.json | 3 ++- src/_locales/it/main.json | 3 ++- src/_locales/ja/main.json | 3 ++- src/_locales/ko/main.json | 3 ++- src/_locales/pt/main.json | 3 ++- src/_locales/ru/main.json | 3 ++- src/_locales/tr/main.json | 3 ++- src/_locales/zh-hans/main.json | 3 ++- src/_locales/zh-hant/main.json | 3 ++- src/background/index.mjs | 26 +++++++++++++++++- src/config/index.mjs | 3 +++ src/content-script/index.jsx | 12 +++++++++ src/rules.json | 44 +++++++++++++++++++++++++++++++ src/services/apis/chatgpt-web.mjs | 25 ++++++++++++------ 18 files changed, 127 insertions(+), 22 deletions(-) diff --git a/src/_locales/de/main.json b/src/_locales/de/main.json index fe0f698..1fa7535 100644 --- a/src/_locales/de/main.json +++ b/src/_locales/de/main.json @@ -126,5 +126,6 @@ "Exceeded quota": "Überschrittenes Kontingent: Prüfen Sie Ihr Guthaben oder Ablaufdatum unter folgendem Link: https://platform.openai.com/account/usage", "Rate limit": "Rate-Limit erreicht", "Jump to bottom": "Zum Ende springen", - "Explain": "Erklären" + "Explain": "Erklären", + "Failed to get arkose token, please keep https://chat.openai.com open": "Arkose-Token konnten nicht abgerufen werden. Bitte halten Sie https://chat.openai.com geöffnet" } diff --git a/src/_locales/en/main.json b/src/_locales/en/main.json index 1302e08..cabe9ac 100644 --- a/src/_locales/en/main.json +++ b/src/_locales/en/main.json @@ -126,5 +126,6 @@ "Exceeded quota": "You exceeded your current quota, check https://platform.openai.com/account/usage", "Rate limit": "Rate limit exceeded", "Jump to bottom": "Jump to bottom", - "Explain": "Explain" + "Explain": "Explain", + "Failed to get arkose token, please keep https://chat.openai.com open": "Failed to get arkose token, please keep https://chat.openai.com open" } diff --git a/src/_locales/es/main.json b/src/_locales/es/main.json index eed637c..d460fbc 100644 --- a/src/_locales/es/main.json +++ b/src/_locales/es/main.json @@ -126,5 +126,6 @@ "Exceeded quota": "Cuota superada: Verifique su saldo o fecha de vencimiento en el siguiente enlace: https://platform.openai.com/account/usage", "Rate limit": "Límite de velocidad alcanzado", "Jump to bottom": "Saltar al final", - "Explain": "Explicar" + "Explain": "Explicar", + "Failed to get arkose token, please keep https://chat.openai.com open": "No se pudo obtener el token de arkose, por favor mantenga https://chat.openai.com abierto" } diff --git a/src/_locales/fr/main.json b/src/_locales/fr/main.json index 292049e..a020240 100644 --- a/src/_locales/fr/main.json +++ b/src/_locales/fr/main.json @@ -126,5 +126,6 @@ "Exceeded quota": "Quota dépassé : Vérifiez votre solde ou la date d'expiration à l'adresse suivante: https://platform.openai.com/account/usage", "Rate limit": "Limite de taux atteinte", "Jump to bottom": "Aller en bas", - "Explain": "Expliquer" + "Explain": "Expliquer", + "Failed to get arkose token, please keep https://chat.openai.com open": "Impossible d'obtenir le jeton arkose, veuillez garder https://chat.openai.com ouvert" } diff --git a/src/_locales/in/main.json b/src/_locales/in/main.json index 5c7465e..9c2cecf 100644 --- a/src/_locales/in/main.json +++ b/src/_locales/in/main.json @@ -126,5 +126,6 @@ "Exceeded quota": "Anda telah melebihi kuota saat ini, periksa https://platform.openai.com/account/usage", "Rate limit": "Batas penggunaan terlampaui", "Jump to bottom": "Lompat ke bawah", - "Explain": "Jelaskan" + "Explain": "Jelaskan", + "Failed to get arkose token, please keep https://chat.openai.com open": "Gagal mendapatkan token arkose, harap tetap buka https://chat.openai.com" } diff --git a/src/_locales/it/main.json b/src/_locales/it/main.json index ba163ea..f3ee2fa 100644 --- a/src/_locales/it/main.json +++ b/src/_locales/it/main.json @@ -126,5 +126,6 @@ "Exceeded quota": "Limite superato o scaduto, controlla questo link: https://platform.openai.com/account/usage", "Rate limit": "Limite di frequenza delle richieste raggiunto", "Jump to bottom": "Salta in fondo", - "Explain": "Spiega" + "Explain": "Spiega", + "Failed to get arkose token, please keep https://chat.openai.com open": "Impossibile ottenere il token arkose, per favore mantieni aperto https://chat.openai.com" } diff --git a/src/_locales/ja/main.json b/src/_locales/ja/main.json index 229b100..4aafb88 100644 --- a/src/_locales/ja/main.json +++ b/src/_locales/ja/main.json @@ -126,5 +126,6 @@ "Exceeded quota": "クォータを超過しました。https://platform.openai.com/account/usage で残高を確認してください", "Rate limit": "レート制限", "Jump to bottom": "最下部にジャンプ", - "Explain": "説明" + "Explain": "説明", + "Failed to get arkose token, please keep https://chat.openai.com open": "arkoseトークンを取得できませんでした。https://chat.openai.com を開いたままにしてください" } diff --git a/src/_locales/ko/main.json b/src/_locales/ko/main.json index cc23c7d..cbddfbb 100644 --- a/src/_locales/ko/main.json +++ b/src/_locales/ko/main.json @@ -126,5 +126,6 @@ "Exceeded quota": "할당량 초과: https://platform.openai.com/account/usage 링크에서 잔액을 확인하세요.", "Rate limit": "요청 비율 제한", "Jump to bottom": "아래로 이동", - "Explain": "설명" + "Explain": "설명", + "Failed to get arkose token, please keep https://chat.openai.com open": "arkose 토큰을 가져오지 못했습니다. https://chat.openai.com 을 열어두세요." } diff --git a/src/_locales/pt/main.json b/src/_locales/pt/main.json index 36f1ebf..d047ffa 100644 --- a/src/_locales/pt/main.json +++ b/src/_locales/pt/main.json @@ -126,5 +126,6 @@ "Exceeded quota": "Cota excedida: Verifique o seu saldo ou a validade em https://platform.openai.com/account/usage.", "Rate limit": "Limite de taxa atingido", "Jump to bottom": "Ir para o fundo", - "Explain": "Explicar" + "Explain": "Explicar", + "Failed to get arkose token, please keep https://chat.openai.com open": "Falha ao obter o token arkose, por favor, mantenha https://chat.openai.com aberto" } diff --git a/src/_locales/ru/main.json b/src/_locales/ru/main.json index 9497bba..6c67bfd 100644 --- a/src/_locales/ru/main.json +++ b/src/_locales/ru/main.json @@ -126,5 +126,6 @@ "Exceeded quota": "Превышено квоту: Проверьте ваш баланс или срок годности по следующей ссылке: https://platform.openai.com/account/usage", "Rate limit": "Лимит запросов", "Jump to bottom": "Перейти вниз", - "Explain": "Объяснить" + "Explain": "Объяснить", + "Failed to get arkose token, please keep https://chat.openai.com open": "Не удалось получить токен arkose, пожалуйста, оставьте открытым https://chat.openai.com" } diff --git a/src/_locales/tr/main.json b/src/_locales/tr/main.json index ba3e8ba..70872c6 100644 --- a/src/_locales/tr/main.json +++ b/src/_locales/tr/main.json @@ -126,5 +126,6 @@ "Exceeded quota": "Mevcut kotanızı aştınız, https://platform.openai.com/account/usage adresini kontrol edin", "Rate limit": "Hız sınırı aşıldı", "Jump to bottom": "En alta git", - "Explain": "Açıkla" + "Explain": "Açıkla", + "Failed to get arkose token, please keep https://chat.openai.com open": "Arkose jetonu alınamadı, lütfen https://chat.openai.com adresini açık tutun" } diff --git a/src/_locales/zh-hans/main.json b/src/_locales/zh-hans/main.json index ee46d01..19ee001 100644 --- a/src/_locales/zh-hans/main.json +++ b/src/_locales/zh-hans/main.json @@ -126,5 +126,6 @@ "Exceeded quota": "余额不足或过期, 检查此链接: https://platform.openai.com/account/usage", "Rate limit": "请求频率过高", "Jump to bottom": "跳转到底部", - "Explain": "解释" + "Explain": "解释", + "Failed to get arkose token, please keep https://chat.openai.com open": "获取arkose token失败, 请保持 https://chat.openai.com 打开" } diff --git a/src/_locales/zh-hant/main.json b/src/_locales/zh-hant/main.json index 6502bc4..8e6a6de 100644 --- a/src/_locales/zh-hant/main.json +++ b/src/_locales/zh-hant/main.json @@ -126,5 +126,6 @@ "Exceeded quota": "您已超出目前的使用額度,請至 OpenAI Usage 頁面請確認您的方案和帳單詳細資訊: https://platform.openai.com/account/usage", "Rate limit": "請求太頻繁,超過限制次數", "Jump to bottom": "轉跳至底部", - "Explain": "解釋" + "Explain": "解釋", + "Failed to get arkose token, please keep https://chat.openai.com open": "無法取得 arkose token,請保持 https://chat.openai.com 開啟" } diff --git a/src/background/index.mjs b/src/background/index.mjs index ee01979..e0fb90b 100644 --- a/src/background/index.mjs +++ b/src/background/index.mjs @@ -26,7 +26,6 @@ import { getUserConfig, githubThirdPartyApiModelKeys, gptApiModelKeys, - Models, poeWebModelKeys, setUserConfig, } from '../config/index.mjs' @@ -230,6 +229,31 @@ Browser.runtime.onMessage.addListener(async (message, sender) => { } }) +Browser.webRequest.onBeforeRequest.addListener( + (details) => { + if ( + details.url.includes('/public_key') && + !details.url.includes(defaultConfig.chatgptArkoseReqParams) + ) { + let formData = new URLSearchParams() + for (const k in details.requestBody.formData) { + formData.append(k, details.requestBody.formData[k]) + } + setUserConfig({ + chatgptArkoseReqUrl: details.url, + chatgptArkoseReqForm: formData.toString(), + }).then(() => { + console.log('Arkose req url and form saved') + }) + } + }, + { + urls: ['https://*.openai.com/*'], + types: ['xmlhttprequest'], + }, + ['requestBody'], +) + Browser.webRequest.onBeforeSendHeaders.addListener( (details) => { const headers = details.requestHeaders diff --git a/src/config/index.mjs b/src/config/index.mjs index ba6955d..7d56e78 100644 --- a/src/config/index.mjs +++ b/src/config/index.mjs @@ -217,11 +217,14 @@ export const defaultConfig = { bingAccessToken: '', chatgptJumpBackTabId: 0, chatgptTabId: 0, + chatgptArkoseReqUrl: '', + chatgptArkoseReqForm: '', // unchangeable userLanguage: getNavigatorLanguage(), apiModes: Object.keys(Models), + chatgptArkoseReqParams: 'cgb=vhwi', selectionTools: [ 'explain', 'translate', diff --git a/src/content-script/index.jsx b/src/content-script/index.jsx index e39133b..d2f93da 100644 --- a/src/content-script/index.jsx +++ b/src/content-script/index.jsx @@ -319,6 +319,18 @@ async function prepareForForegroundRequests() { render(, div) } + if (location.pathname === '/') { + const input = document.querySelector('#prompt-textarea') + if (input) { + input.textContent = ' ' + input.dispatchEvent(new Event('input', { bubbles: true })) + setTimeout(() => { + input.textContent = '' + input.dispatchEvent(new Event('input', { bubbles: true })) + }, 300) + } + } + await Browser.runtime.sendMessage({ type: 'SET_CHATGPT_TAB', data: {}, diff --git a/src/rules.json b/src/rules.json index 0194961..5118553 100644 --- a/src/rules.json +++ b/src/rules.json @@ -20,5 +20,49 @@ "requestDomains": ["sydney.bing.com", "www.bing.com"], "resourceTypes": ["xmlhttprequest", "websocket"] } + }, + { + "id": 2, + "action": { + "type": "modifyHeaders", + "requestHeaders": [ + { + "operation": "set", + "header": "origin", + "value": "https://chat.openai.com" + }, + { + "operation": "set", + "header": "referer", + "value": "https://chat.openai.com" + } + ] + }, + "condition": { + "requestDomains": ["chat.openai.com"], + "resourceTypes": ["xmlhttprequest"] + } + }, + { + "id": 3, + "action": { + "type": "modifyHeaders", + "requestHeaders": [ + { + "operation": "set", + "header": "origin", + "value": "https://tcr9i.chat.openai.com" + }, + { + "operation": "set", + "header": "referer", + "value": "https://tcr9i.chat.openai.com/v2/1.5.5/enforcement.fbfc14b0d793c6ef8359e0e4b4a91f67.html" + } + ] + }, + "condition": { + "requestDomains": ["tcr9i.chat.openai.com"], + "resourceTypes": ["xmlhttprequest"] + } } ] diff --git a/src/services/apis/chatgpt-web.mjs b/src/services/apis/chatgpt-web.mjs index ec51ba7..62164d3 100644 --- a/src/services/apis/chatgpt-web.mjs +++ b/src/services/apis/chatgpt-web.mjs @@ -83,6 +83,22 @@ export async function generateAnswersWithChatgptWebApi(port, question, session, }) .join('; ') + if (!config.chatgptArkoseReqUrl) throw new Error('Please login at https://chat.openai.com first') + const arkoseToken = await fetch( + config.chatgptArkoseReqUrl + '?' + config.chatgptArkoseReqParams, + { + method: 'POST', + body: config.chatgptArkoseReqForm, + headers: { + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', + }, + }, + ) + .then((resp) => resp.json()) + .then((resp) => resp.token) + .catch(() => null) + if (!arkoseToken) + throw new Error('Failed to get arkose token, please keep https://chat.openai.com open') let answer = '' await fetchSSE(`${config.customChatGptWebApiUrl}${config.customChatGptWebApiPath}`, { method: 'POST', @@ -111,15 +127,8 @@ export async function generateAnswersWithChatgptWebApi(port, question, session, model: usedModel, parent_message_id: session.parentMessageId, timezone_offset_min: new Date().getTimezoneOffset(), - variant_purpose: 'none', history_and_training_disabled: config.disableWebModeHistory, - arkose_token: usedModel.startsWith('gpt-4') - ? `${Array.from({ length: 17 }, () => Math.floor(Math.random() * 16).toString(16)).join( - '', - )}|r=ap-southeast-1|meta=3|meta_width=300|metabgclr=transparent|metaiconclr=%23555555|guitextcolor=%23000000|pk=35536E1E-65B4-4D96-9D97-6ADB7EFF8147|at=40|sup=1|rid=${ - Math.floor(Math.random() * 99) + 1 - }|ag=101|cdn_url=https%3A%2F%2Ftcr9i.chat.openai.com%2Fcdn%2Ffc|lurl=https%3A%2F%2Faudio-ap-southeast-1.arkoselabs.com|surl=https%3A%2F%2Ftcr9i.chat.openai.com|smurl=https%3A%2F%2Ftcr9i.chat.openai.com%2Fcdn%2Ffc%2Fassets%2Fstyle-manager` - : undefined, + arkose_token: arkoseToken, }), onMessage(message) { console.debug('sse message', message)