From f3e8d051ea23d419cb775268eee1d4da6e87de5d Mon Sep 17 00:00:00 2001 From: Q3CC <106431792+q3cc@users.noreply.github.com> Date: Fri, 24 Apr 2026 23:30:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=AD=A5=E9=AA=A41=E6=89=93?= =?UTF-8?q?=E5=BC=80=E5=AE=98=E7=BD=91=E5=89=8D=E6=B8=85=E7=90=86OpenAI=20?= =?UTF-8?q?Cookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- background/steps/open-chatgpt.js | 128 +++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/background/steps/open-chatgpt.js b/background/steps/open-chatgpt.js index 2476a9d..222454b 100644 --- a/background/steps/open-chatgpt.js +++ b/background/steps/open-chatgpt.js @@ -1,14 +1,142 @@ (function attachBackgroundStep1(root, factory) { root.MultiPageBackgroundStep1 = factory(); })(typeof self !== 'undefined' ? self : globalThis, function createBackgroundStep1Module() { + const STEP1_COOKIE_CLEAR_DOMAINS = [ + 'chatgpt.com', + 'chat.openai.com', + 'openai.com', + 'auth.openai.com', + 'auth0.openai.com', + 'accounts.openai.com', + ]; + const STEP1_COOKIE_CLEAR_ORIGINS = [ + 'https://chatgpt.com', + 'https://chat.openai.com', + 'https://auth.openai.com', + 'https://auth0.openai.com', + 'https://accounts.openai.com', + 'https://openai.com', + ]; + + function normalizeCookieDomainForStep1(domain) { + return String(domain || '').trim().replace(/^\.+/, '').toLowerCase(); + } + + function shouldClearStep1Cookie(cookie) { + const domain = normalizeCookieDomainForStep1(cookie?.domain); + if (!domain) return false; + return STEP1_COOKIE_CLEAR_DOMAINS.some((target) => ( + domain === target || domain.endsWith(`.${target}`) + )); + } + + function buildStep1CookieRemovalUrl(cookie) { + const host = normalizeCookieDomainForStep1(cookie?.domain); + const rawPath = String(cookie?.path || '/'); + const path = rawPath.startsWith('/') ? rawPath : `/${rawPath}`; + return `https://${host}${path}`; + } + + function getStep1ErrorMessage(error) { + return error?.message || String(error || '未知错误'); + } + + async function collectStep1Cookies(chromeApi) { + if (!chromeApi.cookies?.getAll) { + return []; + } + + const stores = chromeApi.cookies.getAllCookieStores + ? await chromeApi.cookies.getAllCookieStores() + : [{ id: undefined }]; + const cookies = []; + const seen = new Set(); + + for (const store of stores) { + const storeId = store?.id; + const batch = await chromeApi.cookies.getAll(storeId ? { storeId } : {}); + for (const cookie of batch || []) { + if (!shouldClearStep1Cookie(cookie)) continue; + const key = [ + cookie.storeId || storeId || '', + cookie.domain || '', + cookie.path || '', + cookie.name || '', + cookie.partitionKey ? JSON.stringify(cookie.partitionKey) : '', + ].join('|'); + if (seen.has(key)) continue; + seen.add(key); + cookies.push(cookie); + } + } + + return cookies; + } + + async function removeStep1Cookie(chromeApi, cookie) { + const details = { + url: buildStep1CookieRemovalUrl(cookie), + name: cookie.name, + }; + if (cookie.storeId) { + details.storeId = cookie.storeId; + } + if (cookie.partitionKey) { + details.partitionKey = cookie.partitionKey; + } + + try { + const result = await chromeApi.cookies.remove(details); + return Boolean(result); + } catch (error) { + console.warn('[MultiPage:step1] remove cookie failed', { + domain: cookie?.domain, + name: cookie?.name, + message: getStep1ErrorMessage(error), + }); + return false; + } + } + function createStep1Executor(deps = {}) { const { addLog, + chrome: chromeApi = globalThis.chrome, completeStepFromBackground, openSignupEntryTab, } = deps; + async function clearOpenAiCookiesBeforeStep1() { + if (!chromeApi?.cookies?.getAll || !chromeApi.cookies?.remove) { + await addLog('步骤 1:当前浏览器不支持 cookies API,跳过打开官网前 cookie 清理。', 'warn'); + return; + } + + await addLog('步骤 1:打开 ChatGPT 官网前清理 ChatGPT / OpenAI cookies...', 'info'); + const cookies = await collectStep1Cookies(chromeApi); + let removedCount = 0; + for (const cookie of cookies) { + if (await removeStep1Cookie(chromeApi, cookie)) { + removedCount += 1; + } + } + + if (chromeApi.browsingData?.removeCookies) { + try { + await chromeApi.browsingData.removeCookies({ + since: 0, + origins: STEP1_COOKIE_CLEAR_ORIGINS, + }); + } catch (error) { + await addLog(`步骤 1:browsingData 补扫 cookies 失败:${getStep1ErrorMessage(error)}`, 'warn'); + } + } + + await addLog(`步骤 1:已清理 ${removedCount} 个 ChatGPT / OpenAI cookies。`, 'ok'); + } + async function executeStep1() { + await clearOpenAiCookiesBeforeStep1(); await addLog('步骤 1:正在打开 ChatGPT 官网...'); await openSignupEntryTab(1); await completeStepFromBackground(1, {});