feat: 添加进入贡献模式的功能,并更新相关测试用例

This commit is contained in:
QLHazyCoder
2026-04-26 18:13:39 +08:00
parent 31b722cedb
commit 48bc07b94c
2 changed files with 70 additions and 21 deletions
+48 -20
View File
@@ -1190,6 +1190,29 @@ function openPlusContributionSupportModal() {
}); });
} }
async function enterContributionModeFromPlusPrompt() {
if (typeof chrome === 'undefined' || !chrome.runtime?.sendMessage) {
return null;
}
const response = await chrome.runtime.sendMessage({
type: 'SET_CONTRIBUTION_MODE',
source: 'sidepanel',
payload: { enabled: true },
});
if (response?.error) {
throw new Error(response.error);
}
if (response?.state && typeof applySettingsState === 'function') {
applySettingsState(response.state);
}
if (typeof renderContributionMode === 'function') {
renderContributionMode();
}
return response?.state || null;
}
async function maybeShowPlusContributionPromptBeforeAutoRun(plusModeEnabled) { async function maybeShowPlusContributionPromptBeforeAutoRun(plusModeEnabled) {
const records = Array.isArray(latestState?.accountRunHistory) ? latestState.accountRunHistory : []; const records = Array.isArray(latestState?.accountRunHistory) ? latestState.accountRunHistory : [];
if (!shouldShowPlusContributionPrompt(records, plusModeEnabled)) { if (!shouldShowPlusContributionPrompt(records, plusModeEnabled)) {
@@ -1205,7 +1228,12 @@ async function maybeShowPlusContributionPromptBeforeAutoRun(plusModeEnabled) {
} }
if (choice === 'contribute') { if (choice === 'contribute') {
openExternalUrl(getContributionPortalUrl()); openExternalUrl(getContributionPortalUrl());
showToast('已打开贡献页面,可以按页面提示贡献 Plus 账号。', 'info', 2200); try {
await enterContributionModeFromPlusPrompt();
showToast('已进入贡献模式,并打开贡献页面。', 'info', 2200);
} catch (error) {
showToast(`贡献模式开启失败:${error.message}`, 'error', 2600);
}
return false; return false;
} }
return true; return true;
@@ -3253,7 +3281,7 @@ function syncMail2925BaseEmailFromCurrentAccount(state = latestState, options =
syncLatestState({ mail2925BaseEmail: currentEmail }); syncLatestState({ mail2925BaseEmail: currentEmail });
if (persist) { if (persist) {
saveSettings({ silent: true }).catch(() => {}); saveSettings({ silent: true }).catch(() => { });
} }
return true; return true;
} }
@@ -3591,9 +3619,9 @@ function updateMailProviderUI() {
? '请先校验并选择一个 Hotmail 账号' ? '请先校验并选择一个 Hotmail 账号'
: (useLuckmail : (useLuckmail
? '步骤 3 会自动购买 LuckMail 邮箱并用于收码' ? '步骤 3 会自动购买 LuckMail 邮箱并用于收码'
: (useGeneratedAlias : (useGeneratedAlias
? '步骤 3 会自动生成邮箱,无需手动获取' ? '步骤 3 会自动生成邮箱,无需手动获取'
: (useCustomEmail ? '请先填写自定义注册邮箱,成功一轮后会自动清空' : `先自动获取${uiCopy.label},或手动粘贴邮箱后再继续`))); : (useCustomEmail ? '请先填写自定义注册邮箱,成功一轮后会自动清空' : `先自动获取${uiCopy.label},或手动粘贴邮箱后再继续`)));
} }
if (autoHintText && useCustomEmailPool) { if (autoHintText && useCustomEmailPool) {
autoHintText.textContent = getCustomEmailPoolSize() > 0 autoHintText.textContent = getCustomEmailPoolSize() > 0
@@ -3799,12 +3827,12 @@ function updateButtonStates() {
} else if (step === 1) { } else if (step === 1) {
btn.disabled = false; btn.disabled = false;
} else { } else {
const currentIndex = STEP_IDS.indexOf(step); const currentIndex = STEP_IDS.indexOf(step);
const prevStep = currentIndex > 0 ? STEP_IDS[currentIndex - 1] : null; const prevStep = currentIndex > 0 ? STEP_IDS[currentIndex - 1] : null;
const prevStatus = prevStep === null ? 'completed' : statuses[prevStep]; const prevStatus = prevStep === null ? 'completed' : statuses[prevStep];
const currentStatus = statuses[step]; const currentStatus = statuses[step];
btn.disabled = !(isDoneStatus(prevStatus) || currentStatus === 'failed' || isDoneStatus(currentStatus) || currentStatus === 'stopped'); btn.disabled = !(isDoneStatus(prevStatus) || currentStatus === 'failed' || isDoneStatus(currentStatus) || currentStatus === 'stopped');
} }
} }
document.querySelectorAll('.step-manual-btn').forEach((btn) => { document.querySelectorAll('.step-manual-btn').forEach((btn) => {
@@ -3999,8 +4027,8 @@ async function fetchGeneratedEmail(options = {}) {
mail2925Mode: getSelectedMail2925Mode(), mail2925Mode: getSelectedMail2925Mode(),
...(getSelectedEmailGenerator() === CUSTOM_EMAIL_POOL_GENERATOR ...(getSelectedEmailGenerator() === CUSTOM_EMAIL_POOL_GENERATOR
? { ? {
customEmailPool: normalizeCustomEmailPoolEntries(inputCustomEmailPool?.value), customEmailPool: normalizeCustomEmailPoolEntries(inputCustomEmailPool?.value),
} }
: {}), : {}),
...buildManagedAliasBaseEmailPayload(), ...buildManagedAliasBaseEmailPayload(),
}, },
@@ -5267,9 +5295,9 @@ inputEmailPrefix.addEventListener('input', () => {
scheduleSettingsAutoSave(); scheduleSettingsAutoSave();
}); });
inputEmailPrefix.addEventListener('blur', () => { inputEmailPrefix.addEventListener('blur', () => {
maybeClearGeneratedAliasAfterEmailPrefixChange().catch(() => {}); maybeClearGeneratedAliasAfterEmailPrefixChange().catch(() => { });
syncManagedAliasBaseEmailDraftFromInput(); syncManagedAliasBaseEmailDraftFromInput();
saveSettings({ silent: true }).catch(() => {}); saveSettings({ silent: true }).catch(() => { });
}); });
inputCustomEmailPool?.addEventListener('input', () => { inputCustomEmailPool?.addEventListener('input', () => {
@@ -5282,7 +5310,7 @@ inputCustomEmailPool?.addEventListener('blur', () => {
inputCustomEmailPool.value = normalizeCustomEmailPoolEntries(inputCustomEmailPool.value).join('\n'); inputCustomEmailPool.value = normalizeCustomEmailPoolEntries(inputCustomEmailPool.value).join('\n');
syncRunCountFromConfiguredEmailPool(); syncRunCountFromConfiguredEmailPool();
updateMailProviderUI(); updateMailProviderUI();
saveSettings({ silent: true }).catch(() => {}); saveSettings({ silent: true }).catch(() => { });
}); });
inputCustomMailProviderPool?.addEventListener('input', () => { inputCustomMailProviderPool?.addEventListener('input', () => {
@@ -5295,14 +5323,14 @@ inputCustomMailProviderPool?.addEventListener('blur', () => {
inputCustomMailProviderPool.value = normalizeCustomEmailPoolEntries(inputCustomMailProviderPool.value).join('\n'); inputCustomMailProviderPool.value = normalizeCustomEmailPoolEntries(inputCustomMailProviderPool.value).join('\n');
syncRunCountFromConfiguredEmailPool(); syncRunCountFromConfiguredEmailPool();
updateMailProviderUI(); updateMailProviderUI();
saveSettings({ silent: true }).catch(() => {}); saveSettings({ silent: true }).catch(() => { });
}); });
selectMail2925PoolAccount?.addEventListener('change', async () => { selectMail2925PoolAccount?.addEventListener('change', async () => {
try { try {
await syncSelectedMail2925PoolAccount(); await syncSelectedMail2925PoolAccount();
markSettingsDirty(true); markSettingsDirty(true);
saveSettings({ silent: true }).catch(() => {}); saveSettings({ silent: true }).catch(() => { });
} catch (err) { } catch (err) {
showToast(err.message, 'error'); showToast(err.message, 'error');
} }
@@ -5325,7 +5353,7 @@ inputMail2925UseAccountPool?.addEventListener('change', async () => {
setManagedAliasBaseEmailInputForProvider('2925', latestState); setManagedAliasBaseEmailInputForProvider('2925', latestState);
updateMailProviderUI(); updateMailProviderUI();
markSettingsDirty(true); markSettingsDirty(true);
saveSettings({ silent: true }).catch(() => {}); saveSettings({ silent: true }).catch(() => { });
}); });
inputInbucketMailbox.addEventListener('input', () => { inputInbucketMailbox.addEventListener('input', () => {
@@ -5522,7 +5550,7 @@ chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
alert: message.payload?.alert || { text: '检测到 Cloudflare 风控,请暂停当前操作。', tone: 'danger' }, alert: message.payload?.alert || { text: '检测到 Cloudflare 风控,请暂停当前操作。', tone: 'danger' },
confirmLabel: '我知道了', confirmLabel: '我知道了',
confirmVariant: 'btn-danger', confirmVariant: 'btn-danger',
}).catch(() => {}); }).catch(() => { });
break; break;
} }
@@ -184,6 +184,7 @@ test('Plus contribution prompt marks shown and donated choice adds twenty credit
'shouldShowPlusContributionPrompt', 'shouldShowPlusContributionPrompt',
'markPlusContributionPromptShown', 'markPlusContributionPromptShown',
'addPlusContributionPromptCredit', 'addPlusContributionPromptCredit',
'enterContributionModeFromPlusPrompt',
'maybeShowPlusContributionPromptBeforeAutoRun', 'maybeShowPlusContributionPromptBeforeAutoRun',
]); ]);
@@ -246,6 +247,7 @@ test('Plus contribution prompt opens portal and aborts normal auto run when cont
'shouldShowPlusContributionPrompt', 'shouldShowPlusContributionPrompt',
'markPlusContributionPromptShown', 'markPlusContributionPromptShown',
'addPlusContributionPromptCredit', 'addPlusContributionPromptCredit',
'enterContributionModeFromPlusPrompt',
'maybeShowPlusContributionPromptBeforeAutoRun', 'maybeShowPlusContributionPromptBeforeAutoRun',
]); ]);
@@ -274,6 +276,20 @@ function openExternalUrl(url) {
function getContributionPortalUrl() { function getContributionPortalUrl() {
return 'https://apikey.qzz.io'; return 'https://apikey.qzz.io';
} }
const chrome = {
runtime: {
async sendMessage(message) {
events.push({ type: 'runtime', message });
return { state: { contributionMode: true } };
},
},
};
function applySettingsState(state) {
events.push({ type: 'apply', state });
}
function renderContributionMode() {
events.push({ type: 'render' });
}
${bundle} ${bundle}
return { return {
maybeShowPlusContributionPromptBeforeAutoRun, maybeShowPlusContributionPromptBeforeAutoRun,
@@ -286,6 +302,11 @@ return {
const result = await api.maybeShowPlusContributionPromptBeforeAutoRun(true); const result = await api.maybeShowPlusContributionPromptBeforeAutoRun(true);
assert.equal(result, false); assert.equal(result, false);
assert.deepEqual(api.getEvents().map((event) => event.type), ['modal', 'open', 'toast']); assert.deepEqual(api.getEvents().map((event) => event.type), ['modal', 'open', 'runtime', 'apply', 'render', 'toast']);
assert.equal(api.getEvents()[1].url, 'https://apikey.qzz.io'); assert.equal(api.getEvents()[1].url, 'https://apikey.qzz.io');
assert.deepEqual(api.getEvents()[2].message, {
type: 'SET_CONTRIBUTION_MODE',
source: 'sidepanel',
payload: { enabled: true },
});
}); });