feat: 添加进入贡献模式的功能,并更新相关测试用例
This commit is contained in:
+37
-9
@@ -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;
|
||||||
}
|
}
|
||||||
@@ -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 },
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user