feat: replace cookie cleanup step with registration success wait

This commit is contained in:
QLHazyCoder
2026-05-06 14:38:45 +08:00
parent 1bfb89eeb6
commit 9964b5a317
11 changed files with 69 additions and 189 deletions
+7 -140
View File
@@ -32,7 +32,7 @@ importScripts(
'background/steps/fill-password.js',
'background/steps/fetch-signup-code.js',
'background/steps/fill-profile.js',
'background/steps/clear-login-cookies.js',
'background/steps/wait-registration-success.js',
'background/steps/create-plus-checkout.js',
'background/steps/fill-plus-checkout.js',
'background/steps/gopay-manual-confirm.js',
@@ -709,23 +709,7 @@ const PERSISTED_SETTING_DEFAULTS = {
const PERSISTED_SETTING_KEYS = Object.keys(PERSISTED_SETTING_DEFAULTS);
const SETTINGS_EXPORT_SCHEMA_VERSION = 1;
const SETTINGS_EXPORT_FILENAME_PREFIX = 'multipage-settings';
const STEP6_PRE_LOGIN_COOKIE_CLEAR_DELAY_MS = 25000;
const PRE_LOGIN_COOKIE_CLEAR_DOMAINS = [
'chatgpt.com',
'chat.openai.com',
'openai.com',
'auth.openai.com',
'auth0.openai.com',
'accounts.openai.com',
];
const PRE_LOGIN_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',
];
const STEP6_REGISTRATION_SUCCESS_WAIT_MS = 20000;
const DEFAULT_STATE = {
currentStep: 0, // 当前流程执行到的步骤编号。
@@ -8413,7 +8397,7 @@ const AUTO_RUN_BACKGROUND_COMPLETED_STEP_KEYS = new Set([
'open-chatgpt',
'submit-signup-email',
'fetch-signup-code',
'clear-login-cookies',
'wait-registration-success',
'plus-checkout-create',
'plus-checkout-billing',
'paypal-approve',
@@ -10419,8 +10403,10 @@ const step5Executor = self.MultiPageBackgroundStep5?.createStep5Executor({
sendToContentScript,
});
const step6Executor = self.MultiPageBackgroundStep6?.createStep6Executor({
addLog,
completeStepFromBackground,
runPreStep6CookieCleanup,
registrationSuccessWaitMs: STEP6_REGISTRATION_SUCCESS_WAIT_MS,
sleepWithStop,
});
const step7Executor = self.MultiPageBackgroundStep7?.createStep7Executor({
addLog,
@@ -10581,7 +10567,7 @@ const stepExecutorsByKey = {
'fill-password': (state) => step3Executor.executeStep3(state),
'fetch-signup-code': (state) => step4Executor.executeStep4(state),
'fill-profile': (state) => step5Executor.executeStep5(state),
'clear-login-cookies': () => step6Executor.executeStep6(),
'wait-registration-success': () => step6Executor.executeStep6(),
'plus-checkout-create': (state) => plusCheckoutCreateExecutor.executePlusCheckoutCreate(state),
'plus-checkout-billing': (state) => plusCheckoutBillingExecutor.executePlusCheckoutBilling(state),
'gopay-subscription-confirm': (state) => goPayManualConfirmExecutor.executeGoPayManualConfirm(state),
@@ -10960,125 +10946,6 @@ async function executeStep5(state) {
return step5Executor.executeStep5(state);
}
// ============================================================
// Step 6 Cookie Cleanup
// ============================================================
function normalizeCookieDomainForMatch(domain) {
return String(domain || '').trim().replace(/^\.+/, '').toLowerCase();
}
function shouldClearPreLoginCookie(cookie) {
const domain = normalizeCookieDomainForMatch(cookie?.domain);
if (!domain) return false;
return PRE_LOGIN_COOKIE_CLEAR_DOMAINS.some((target) => (
domain === target || domain.endsWith(`.${target}`)
));
}
function buildCookieRemovalUrl(cookie) {
const host = normalizeCookieDomainForMatch(cookie?.domain);
const path = String(cookie?.path || '/').startsWith('/')
? String(cookie?.path || '/')
: `/${String(cookie?.path || '')}`;
return `https://${host}${path}`;
}
async function collectCookiesForPreLoginCleanup() {
if (!chrome.cookies?.getAll) {
return [];
}
const stores = chrome.cookies.getAllCookieStores
? await chrome.cookies.getAllCookieStores()
: [{ id: undefined }];
const cookies = [];
const seen = new Set();
for (const store of stores) {
const storeId = store?.id;
const batch = await chrome.cookies.getAll(storeId ? { storeId } : {});
for (const cookie of batch || []) {
if (!shouldClearPreLoginCookie(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 removeCookieDirectly(cookie) {
const details = {
url: buildCookieRemovalUrl(cookie),
name: cookie.name,
};
if (cookie.storeId) {
details.storeId = cookie.storeId;
}
if (cookie.partitionKey) {
details.partitionKey = cookie.partitionKey;
}
try {
const result = await chrome.cookies.remove(details);
return Boolean(result);
} catch (err) {
console.warn(LOG_PREFIX, '[removeCookieDirectly] failed', {
domain: cookie?.domain,
name: cookie?.name,
message: getErrorMessage(err),
});
return false;
}
}
async function runPreStep6CookieCleanup() {
await addLog(
`步骤 6:开始前等待 ${Math.round(STEP6_PRE_LOGIN_COOKIE_CLEAR_DELAY_MS / 1000)} 秒,然后直接删除 ChatGPT / OpenAI cookies...`,
'info'
);
await sleepWithStop(STEP6_PRE_LOGIN_COOKIE_CLEAR_DELAY_MS);
if (!chrome.cookies?.getAll || !chrome.cookies?.remove) {
await addLog('步骤 6:当前浏览器不支持 cookies API,无法直接删除 cookies。', 'warn');
return;
}
const cookies = await collectCookiesForPreLoginCleanup();
let removedCount = 0;
for (const cookie of cookies) {
throwIfStopped();
if (await removeCookieDirectly(cookie)) {
removedCount += 1;
}
}
if (chrome.browsingData?.removeCookies) {
try {
await chrome.browsingData.removeCookies({
since: 0,
origins: PRE_LOGIN_COOKIE_CLEAR_ORIGINS,
});
} catch (err) {
await addLog(`步骤 6browsingData 补扫 cookies 失败:${getErrorMessage(err)}`, 'warn');
}
}
await addLog(`步骤 6:已直接删除 ${removedCount} 个 ChatGPT / OpenAI cookies,准备继续获取链接并登录。`, 'ok');
}
// ============================================================
// Step 7: Login and ensure the auth page reaches the login verification page
// ============================================================