fix: keep GPC tab focused while reading session
This commit is contained in:
@@ -309,10 +309,11 @@ function FindProxyForURL(url, host) {
|
|||||||
return paymentMethod === PLUS_PAYMENT_METHOD_GOPAY ? 'GoPay' : 'PayPal';
|
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'
|
const tab = typeof createAutomationTab === 'function'
|
||||||
? await createAutomationTab({ 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: true });
|
: await chrome.tabs.create({ url: PLUS_CHECKOUT_ENTRY_URL, active });
|
||||||
const tabId = Number(tab?.id);
|
const tabId = Number(tab?.id);
|
||||||
if (!Number.isInteger(tabId)) {
|
if (!Number.isInteger(tabId)) {
|
||||||
throw new Error('步骤 6:打开 ChatGPT 页面失败,无法创建订阅页。');
|
throw new Error('步骤 6:打开 ChatGPT 页面失败,无法创建订阅页。');
|
||||||
@@ -1609,7 +1610,10 @@ function FindProxyForURL(url, host) {
|
|||||||
await addLog('步骤 6:正在打开 GPC 页面并准备卡密充值模式...', 'info');
|
await addLog('步骤 6:正在打开 GPC 页面并准备卡密充值模式...', 'info');
|
||||||
const { tabId, portalUrl } = await openOrReuseGpcPortalTab(state);
|
const { tabId, portalUrl } = await openOrReuseGpcPortalTab(state);
|
||||||
await addLog('步骤 6:正在从 ChatGPT 获取完整 session...', 'info');
|
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;
|
let session = null;
|
||||||
try {
|
try {
|
||||||
session = await readSessionFromChatGptSessionTab(sessionTabId);
|
session = await readSessionFromChatGptSessionTab(sessionTabId);
|
||||||
@@ -1617,6 +1621,9 @@ function FindProxyForURL(url, host) {
|
|||||||
if (chrome?.tabs?.remove && Number.isInteger(sessionTabId)) {
|
if (chrome?.tabs?.remove && Number.isInteger(sessionTabId)) {
|
||||||
await chrome.tabs.remove(sessionTabId).catch(() => {});
|
await chrome.tabs.remove(sessionTabId).catch(() => {});
|
||||||
}
|
}
|
||||||
|
if (chrome?.tabs?.update) {
|
||||||
|
await chrome.tabs.update(tabId, { active: true }).catch(() => {});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const sessionJson = JSON.stringify(session);
|
const sessionJson = JSON.stringify(session);
|
||||||
|
|||||||
@@ -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));
|
const executeEvent = events.find((event) => event.type === 'execute-script' && Array.isArray(event.args));
|
||||||
assert.equal(executeEvent.target.tabId, 77);
|
assert.equal(executeEvent.target.tabId, 77);
|
||||||
assert.equal(executeEvent.args[1], JSON.stringify(session));
|
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 || {};
|
const statePayload = events.find((event) => event.type === 'set-state')?.payload || {};
|
||||||
assert.equal(statePayload.plusCheckoutTabId, 77);
|
assert.equal(statePayload.plusCheckoutTabId, 77);
|
||||||
assert.equal(statePayload.plusCheckoutSource, 'gpc-helper');
|
assert.equal(statePayload.plusCheckoutSource, 'gpc-helper');
|
||||||
|
|||||||
Reference in New Issue
Block a user