114 lines
4.3 KiB
JavaScript
114 lines
4.3 KiB
JavaScript
(function attachBackgroundPlusCheckoutCreate(root, factory) {
|
||
root.MultiPageBackgroundPlusCheckoutCreate = factory();
|
||
})(typeof self !== 'undefined' ? self : globalThis, function createBackgroundPlusCheckoutCreateModule() {
|
||
const PLUS_CHECKOUT_SOURCE = 'plus-checkout';
|
||
const PLUS_CHECKOUT_ENTRY_URL = 'https://chatgpt.com/';
|
||
const PLUS_CHECKOUT_INJECT_FILES = ['content/utils.js', 'content/plus-checkout.js'];
|
||
const PLUS_PAYMENT_METHOD_GOPAY = 'gopay';
|
||
|
||
function createPlusCheckoutCreateExecutor(deps = {}) {
|
||
const {
|
||
addLog,
|
||
chrome,
|
||
completeStepFromBackground,
|
||
ensureContentScriptReadyOnTabUntilStopped,
|
||
registerTab,
|
||
sendTabMessageUntilStopped,
|
||
setState,
|
||
sleepWithStop,
|
||
waitForTabCompleteUntilStopped,
|
||
} = deps;
|
||
|
||
function normalizePlusPaymentMethod(value = '') {
|
||
return String(value || '').trim().toLowerCase() === 'gopay' ? 'gopay' : 'paypal';
|
||
}
|
||
|
||
function getCheckoutModeLabel(state = {}) {
|
||
return normalizePlusPaymentMethod(state?.plusPaymentMethod) === 'gopay'
|
||
? 'GoPay 订阅页'
|
||
: 'Plus Checkout';
|
||
}
|
||
|
||
async function openFreshChatGptTabForCheckoutCreate() {
|
||
const tab = await chrome.tabs.create({ url: PLUS_CHECKOUT_ENTRY_URL, active: true });
|
||
const tabId = Number(tab?.id);
|
||
if (!Number.isInteger(tabId)) {
|
||
throw new Error('步骤 6:打开 ChatGPT 页面失败,无法创建订阅页。');
|
||
}
|
||
if (typeof registerTab === 'function') {
|
||
await registerTab(PLUS_CHECKOUT_SOURCE, tabId);
|
||
}
|
||
return tabId;
|
||
}
|
||
|
||
function normalizePlusPaymentMethod(value = '') {
|
||
return String(value || '').trim().toLowerCase() === PLUS_PAYMENT_METHOD_GOPAY ? 'gopay' : 'paypal';
|
||
}
|
||
|
||
function getPlusPaymentMethodLabel(method = 'paypal') {
|
||
return normalizePlusPaymentMethod(method) === PLUS_PAYMENT_METHOD_GOPAY ? 'GoPay' : 'PayPal';
|
||
}
|
||
|
||
async function executePlusCheckoutCreate(state = {}) {
|
||
const paymentMethod = normalizePlusPaymentMethod(state?.plusPaymentMethod);
|
||
const paymentMethodLabel = getPlusPaymentMethodLabel(paymentMethod);
|
||
const checkoutModeLabel = getCheckoutModeLabel(state);
|
||
await addLog('步骤 6:正在新打开 ChatGPT 会话页,准备创建 Plus Checkout...', 'info');
|
||
const tabId = await openFreshChatGptTabForCheckoutCreate();
|
||
|
||
await waitForTabCompleteUntilStopped(tabId);
|
||
await sleepWithStop(1000);
|
||
await ensureContentScriptReadyOnTabUntilStopped(PLUS_CHECKOUT_SOURCE, tabId, {
|
||
inject: PLUS_CHECKOUT_INJECT_FILES,
|
||
injectSource: PLUS_CHECKOUT_SOURCE,
|
||
logMessage: '步骤 6:正在等待 ChatGPT 页面完成加载,再继续创建订阅页...',
|
||
});
|
||
|
||
const result = await sendTabMessageUntilStopped(tabId, PLUS_CHECKOUT_SOURCE, {
|
||
type: 'CREATE_PLUS_CHECKOUT',
|
||
source: 'background',
|
||
payload: { paymentMethod },
|
||
});
|
||
|
||
if (result?.error) {
|
||
throw new Error(result.error);
|
||
}
|
||
if (!result?.checkoutUrl) {
|
||
throw new Error(`步骤 6:${checkoutModeLabel}未返回可用的订阅链接。`);
|
||
}
|
||
|
||
await addLog(`步骤 6:${checkoutModeLabel}已创建,正在打开订阅页面...`, 'ok');
|
||
await chrome.tabs.update(tabId, { url: result.checkoutUrl, active: true });
|
||
await waitForTabCompleteUntilStopped(tabId);
|
||
await sleepWithStop(1000);
|
||
await ensureContentScriptReadyOnTabUntilStopped(PLUS_CHECKOUT_SOURCE, tabId, {
|
||
inject: PLUS_CHECKOUT_INJECT_FILES,
|
||
injectSource: PLUS_CHECKOUT_SOURCE,
|
||
logMessage: '步骤 6:正在等待订阅页面完成加载...',
|
||
});
|
||
|
||
await setState({
|
||
plusCheckoutTabId: tabId,
|
||
plusCheckoutUrl: result.checkoutUrl,
|
||
plusCheckoutCountry: result.country || 'DE',
|
||
plusCheckoutCurrency: result.currency || 'EUR',
|
||
});
|
||
|
||
await addLog(`步骤 6:Plus Checkout 页面已就绪(${paymentMethodLabel} / ${result.country || 'DE'} ${result.currency || 'EUR'}),准备继续下一步。`, 'info');
|
||
|
||
await completeStepFromBackground(6, {
|
||
plusCheckoutCountry: result.country || 'DE',
|
||
plusCheckoutCurrency: result.currency || 'EUR',
|
||
});
|
||
}
|
||
|
||
return {
|
||
executePlusCheckoutCreate,
|
||
};
|
||
}
|
||
|
||
return {
|
||
createPlusCheckoutCreateExecutor,
|
||
};
|
||
});
|