diff --git a/flows/openai/background/steps/create-plus-checkout.js b/flows/openai/background/steps/create-plus-checkout.js index 585b36e..9a011c2 100644 --- a/flows/openai/background/steps/create-plus-checkout.js +++ b/flows/openai/background/steps/create-plus-checkout.js @@ -309,10 +309,11 @@ function FindProxyForURL(url, host) { return paymentMethod === PLUS_PAYMENT_METHOD_GOPAY ? 'GoPay' : 'PayPal'; } - async function openFreshChatGptTabForCheckoutCreate() { + async function openFreshChatGptTabForCheckoutCreate(options = {}) { + const active = options?.active !== false; const tab = typeof createAutomationTab === 'function' - ? await createAutomationTab({ url: PLUS_CHECKOUT_ENTRY_URL, active: true }) - : await chrome.tabs.create({ url: PLUS_CHECKOUT_ENTRY_URL, active: true }); + ? await createAutomationTab({ url: PLUS_CHECKOUT_ENTRY_URL, active }) + : await chrome.tabs.create({ url: PLUS_CHECKOUT_ENTRY_URL, active }); const tabId = Number(tab?.id); if (!Number.isInteger(tabId)) { throw new Error('步骤 6:打开 ChatGPT 页面失败,无法创建订阅页。'); @@ -1609,7 +1610,10 @@ function FindProxyForURL(url, host) { await addLog('步骤 6:正在打开 GPC 页面并准备卡密充值模式...', 'info'); const { tabId, portalUrl } = await openOrReuseGpcPortalTab(state); await addLog('步骤 6:正在从 ChatGPT 获取完整 session...', 'info'); - const sessionTabId = await openFreshChatGptTabForCheckoutCreate(); + const sessionTabId = await openFreshChatGptTabForCheckoutCreate({ active: false }); + if (chrome?.tabs?.update) { + await chrome.tabs.update(tabId, { active: true }).catch(() => {}); + } let session = null; try { session = await readSessionFromChatGptSessionTab(sessionTabId); @@ -1617,6 +1621,9 @@ function FindProxyForURL(url, host) { if (chrome?.tabs?.remove && Number.isInteger(sessionTabId)) { await chrome.tabs.remove(sessionTabId).catch(() => {}); } + if (chrome?.tabs?.update) { + await chrome.tabs.update(tabId, { active: true }).catch(() => {}); + } } const sessionJson = JSON.stringify(session); diff --git a/tests/plus-checkout-create-wait.test.js b/tests/plus-checkout-create-wait.test.js index a396ccd..8de28c5 100644 --- a/tests/plus-checkout-create-wait.test.js +++ b/tests/plus-checkout-create-wait.test.js @@ -1013,6 +1013,15 @@ test('GPC checkout prepare opens page, fills segmented card key, and writes full const executeEvent = events.find((event) => event.type === 'execute-script' && Array.isArray(event.args)); assert.equal(executeEvent.target.tabId, 77); assert.equal(executeEvent.args[1], JSON.stringify(session)); + const chatGptTabCreate = events.find((event) => ( + event.type === 'automation-tab-create' + && event.payload?.url === 'https://chatgpt.com/' + )); + assert.equal(chatGptTabCreate.payload.active, false); + assert.equal( + events.some((event) => event.type === 'tab-update' && event.tabId === 77 && event.payload?.active === true), + true + ); const statePayload = events.find((event) => event.type === 'set-state')?.payload || {}; assert.equal(statePayload.plusCheckoutTabId, 77); assert.equal(statePayload.plusCheckoutSource, 'gpc-helper');