refactor: 移除未使用的函数和变量,简化 Plus Checkout 创建逻辑
This commit is contained in:
@@ -2,21 +2,8 @@
|
||||
root.MultiPageBackgroundPlusCheckoutCreate = factory();
|
||||
})(typeof self !== 'undefined' ? self : globalThis, function createBackgroundPlusCheckoutCreateModule() {
|
||||
const PLUS_CHECKOUT_SOURCE = 'plus-checkout';
|
||||
const SIGNUP_PAGE_SOURCE = 'signup-page';
|
||||
const PLUS_CHECKOUT_ENTRY_URL = 'https://chatgpt.com/';
|
||||
const PLUS_CHECKOUT_INJECT_FILES = ['content/utils.js', 'content/plus-checkout.js'];
|
||||
const PLUS_CHECKOUT_ONLY_INJECT_FILES = ['content/plus-checkout.js'];
|
||||
|
||||
function isReusableChatGptTabUrl(url = '') {
|
||||
try {
|
||||
const parsed = new URL(String(url || ''));
|
||||
const hostname = parsed.hostname.toLowerCase();
|
||||
return ['chatgpt.com', 'www.chatgpt.com', 'chat.openai.com'].includes(hostname)
|
||||
&& !/^\/checkout(?:\/|$)/i.test(parsed.pathname || '');
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function createPlusCheckoutCreateExecutor(deps = {}) {
|
||||
const {
|
||||
@@ -24,59 +11,33 @@
|
||||
chrome,
|
||||
completeStepFromBackground,
|
||||
ensureContentScriptReadyOnTabUntilStopped,
|
||||
getTabId,
|
||||
isTabAlive,
|
||||
registerTab,
|
||||
reuseOrCreateTab,
|
||||
sendTabMessageUntilStopped,
|
||||
setState,
|
||||
sleepWithStop,
|
||||
waitForTabCompleteUntilStopped,
|
||||
} = deps;
|
||||
|
||||
async function getReusableSignupChatGptTabId() {
|
||||
if (typeof getTabId !== 'function' || typeof isTabAlive !== 'function') {
|
||||
return null;
|
||||
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 页面失败,未获取到有效标签页 ID。');
|
||||
}
|
||||
const tabId = await getTabId(SIGNUP_PAGE_SOURCE);
|
||||
if (!tabId || !(await isTabAlive(SIGNUP_PAGE_SOURCE))) {
|
||||
return null;
|
||||
if (typeof registerTab === 'function') {
|
||||
await registerTab(PLUS_CHECKOUT_SOURCE, tabId);
|
||||
}
|
||||
const tab = await chrome.tabs.get(tabId).catch(() => null);
|
||||
return tab && isReusableChatGptTabUrl(tab.url) ? tabId : null;
|
||||
}
|
||||
|
||||
async function resolveChatGptTabForCheckoutCreate() {
|
||||
const existingSignupTabId = await getReusableSignupChatGptTabId();
|
||||
if (existingSignupTabId) {
|
||||
await chrome.tabs.update(existingSignupTabId, { active: true });
|
||||
if (typeof registerTab === 'function') {
|
||||
await registerTab(PLUS_CHECKOUT_SOURCE, existingSignupTabId);
|
||||
}
|
||||
await addLog('步骤 6:检测到第 5 步已打开 ChatGPT 页面,直接接管当前标签页创建 Plus Checkout。', 'info');
|
||||
return {
|
||||
tabId: existingSignupTabId,
|
||||
injectFiles: PLUS_CHECKOUT_ONLY_INJECT_FILES,
|
||||
};
|
||||
}
|
||||
|
||||
const tabId = await reuseOrCreateTab(PLUS_CHECKOUT_SOURCE, PLUS_CHECKOUT_ENTRY_URL, {
|
||||
reloadIfSameUrl: false,
|
||||
});
|
||||
return {
|
||||
tabId,
|
||||
injectFiles: PLUS_CHECKOUT_INJECT_FILES,
|
||||
};
|
||||
return tabId;
|
||||
}
|
||||
|
||||
async function executePlusCheckoutCreate() {
|
||||
await addLog('步骤 6:正在打开 ChatGPT 会话页,准备创建 Plus Checkout...', 'info');
|
||||
const { tabId, injectFiles } = await resolveChatGptTabForCheckoutCreate();
|
||||
await addLog('步骤 6:正在新打开 ChatGPT 会话页,准备创建 Plus Checkout...', 'info');
|
||||
const tabId = await openFreshChatGptTabForCheckoutCreate();
|
||||
|
||||
await waitForTabCompleteUntilStopped(tabId);
|
||||
await sleepWithStop(1000);
|
||||
await ensureContentScriptReadyOnTabUntilStopped(PLUS_CHECKOUT_SOURCE, tabId, {
|
||||
inject: injectFiles,
|
||||
inject: PLUS_CHECKOUT_INJECT_FILES,
|
||||
injectSource: PLUS_CHECKOUT_SOURCE,
|
||||
logMessage: '步骤 6:ChatGPT 页面仍在加载,等待 Plus Checkout 脚本就绪...',
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user