重构duckduckgo邮箱获取

This commit is contained in:
QLHazyCoder
2026-05-12 02:26:46 +08:00
parent 745010d6c9
commit f81cc74c29
16 changed files with 492 additions and 53 deletions
+1 -1
View File
@@ -185,7 +185,7 @@
throw err;
}
}
await setEmailState(address);
await setEmailState(address, { source: 'generated:cloudmail' });
await addLog(`Cloud Mail:已生成 ${address}`, 'ok');
return address;
}
+20 -10
View File
@@ -14,6 +14,7 @@
getCloudflareTempEmailAddressFromResponse,
getCloudflareTempEmailConfig,
getCustomEmailPoolEmail,
getRegistrationEmailBaseline,
getState,
ensureMail2925AccountForFlow,
joinCloudflareTempEmailUrl,
@@ -59,7 +60,7 @@
const localPart = String(options.localPart || '').trim().toLowerCase() || generateCloudflareAliasLocalPart();
const aliasEmail = `${localPart}@${domain}`;
await setEmailState(aliasEmail);
await setEmailState(aliasEmail, { source: 'generated:cloudflare' });
await addLog(`Cloudflare 邮箱:已生成 ${aliasEmail}`, 'ok');
return aliasEmail;
}
@@ -161,7 +162,7 @@
throw new Error('Cloudflare Temp Email 未返回可用邮箱地址。');
}
await setEmailState(address);
await setEmailState(address, { source: 'generated:cloudflare-temp-email' });
await addLog(`Cloudflare Temp Email:已生成 ${address}`, 'ok');
return address;
}
@@ -174,7 +175,7 @@
throwIfStopped();
const {
generateNew = true,
previousEmail = '',
baselineEmail = '',
} = options;
await addLog(`Duck 邮箱:正在打开自动填充设置(${generateNew ? '生成新地址' : '复用当前地址'}...`);
@@ -185,7 +186,7 @@
source: 'background',
payload: {
generateNew,
previousEmail: normalizeEmailForComparison(previousEmail),
baselineEmail: normalizeEmailForComparison(baselineEmail),
},
});
@@ -196,7 +197,7 @@
throw new Error('未返回 Duck 邮箱地址。');
}
await setEmailState(result.email);
await setEmailState(result.email, { source: 'generated:duck' });
await addLog(`Duck 邮箱:${result.generated ? '已生成' : '已读取'} ${result.email}`, 'ok');
return result.email;
}
@@ -214,7 +215,7 @@
);
}
await setEmailState(email);
await setEmailState(email, { source: 'generated:custom-pool' });
await addLog(`自定义邮箱池:已取用 ${email}`, 'ok');
return email;
}
@@ -253,7 +254,7 @@
}
const email = buildGeneratedAliasEmail(mergedState);
await setEmailState(email);
await setEmailState(email, { source: `generated:${provider || 'alias'}` });
await addLog(`${provider === 'gmail' ? 'Gmail +tag' : '2925'}:已生成 ${email}`, 'ok');
return email;
}
@@ -311,11 +312,20 @@
if (generator === CLOUDFLARE_TEMP_EMAIL_GENERATOR) {
return fetchCloudflareTempEmailAddress(mergedState, options);
}
const resolvedDuckBaselineEmail = typeof getRegistrationEmailBaseline === 'function'
? getRegistrationEmailBaseline(mergedState, {
preferredEmail: options.currentEmail,
fallbackEmail: options.baselineEmail,
})
: String(
options.currentEmail
|| options.baselineEmail
|| mergedState.email
|| ''
).trim();
return fetchDuckEmail({
...options,
previousEmail: options.previousEmail !== undefined
? options.previousEmail
: mergedState.email,
baselineEmail: resolvedDuckBaselineEmail,
});
}
+2 -2
View File
@@ -1320,7 +1320,7 @@
throw new Error('自动流程运行中,当前不能手动修改邮箱。');
}
const email = String(message.payload?.email || '').trim() || null;
await setEmailStateSilently(email);
await setEmailStateSilently(email, { source: 'manual' });
return { ok: true, email };
}
@@ -1329,7 +1329,7 @@
if (isAutoRunLockedState(state)) {
throw new Error('自动流程运行中,当前不能手动修改邮箱。');
}
await setEmailState(message.payload.email);
await setEmailState(message.payload.email, { source: 'manual' });
await resumeAutoRun();
return { ok: true, email: message.payload.email };
}
+87
View File
@@ -0,0 +1,87 @@
(function attachBackgroundRegistrationEmailState(root, factory) {
root.MultiPageRegistrationEmailState = factory();
})(typeof self !== 'undefined' ? self : globalThis, function createBackgroundRegistrationEmailStateModule() {
const DEFAULT_REGISTRATION_EMAIL_STATE = Object.freeze({
current: '',
previous: '',
source: '',
updatedAt: 0,
});
function createRegistrationEmailStateHelpers() {
function normalizeEmailValue(value) {
return String(value || '').trim();
}
function cloneDefaultRegistrationEmailState() {
return {
current: DEFAULT_REGISTRATION_EMAIL_STATE.current,
previous: DEFAULT_REGISTRATION_EMAIL_STATE.previous,
source: DEFAULT_REGISTRATION_EMAIL_STATE.source,
updatedAt: DEFAULT_REGISTRATION_EMAIL_STATE.updatedAt,
};
}
function normalizeRegistrationEmailState(value, fallbackEmail = '') {
const fallback = normalizeEmailValue(fallbackEmail);
const candidate = value && typeof value === 'object' && !Array.isArray(value)
? value
: null;
const current = normalizeEmailValue(candidate?.current || fallback);
const previous = normalizeEmailValue(candidate?.previous || current || fallback);
const source = String(candidate?.source || '').trim();
const updatedAt = Number(candidate?.updatedAt) || 0;
return {
current,
previous,
source,
updatedAt: updatedAt > 0 ? updatedAt : 0,
};
}
function getRegistrationEmailState(state = {}) {
return normalizeRegistrationEmailState(state?.registrationEmailState, state?.email);
}
function buildRegistrationEmailStateUpdates(state = {}, options = {}) {
const currentState = getRegistrationEmailState(state);
const currentEmail = normalizeEmailValue(options.currentEmail);
const preservePrevious = Boolean(options.preservePrevious);
const source = String(options.source || '').trim();
const nextState = cloneDefaultRegistrationEmailState();
nextState.current = currentEmail;
nextState.previous = currentEmail || (preservePrevious ? currentState.previous : '');
nextState.source = currentEmail
? (source || currentState.source)
: (preservePrevious ? currentState.source : '');
nextState.updatedAt = currentEmail || (preservePrevious && currentState.previous)
? Date.now()
: 0;
return {
email: currentEmail || null,
registrationEmailState: nextState,
};
}
function getRegistrationEmailBaseline(state = {}, options = {}) {
const currentState = getRegistrationEmailState(state);
const preferredEmail = normalizeEmailValue(options.preferredEmail);
const fallbackEmail = normalizeEmailValue(options.fallbackEmail);
return preferredEmail || currentState.current || currentState.previous || fallbackEmail || '';
}
return {
DEFAULT_REGISTRATION_EMAIL_STATE: cloneDefaultRegistrationEmailState(),
buildRegistrationEmailStateUpdates,
getRegistrationEmailBaseline,
getRegistrationEmailState,
normalizeRegistrationEmailState,
};
}
return {
createRegistrationEmailStateHelpers,
};
});
+9 -5
View File
@@ -329,11 +329,12 @@
return;
}
const preservedPhoneIdentity = getPreservedPhoneIdentityForEmailResolution(state, options);
const generatedEmailAlreadyPersisted = Boolean(options?.generatedEmailAlreadyPersisted);
if (preservedPhoneIdentity && typeof setState === 'function') {
await setState({
email: resolvedEmail,
...preservedPhoneIdentity,
});
if (!generatedEmailAlreadyPersisted && resolvedEmail !== state.email) {
await setEmailState(resolvedEmail, { source: 'flow' });
}
await setState(preservedPhoneIdentity);
return;
}
if (resolvedEmail !== state.email) {
@@ -377,7 +378,10 @@
}
if (!generatedEmailAlreadyPersisted || options?.preserveAccountIdentity) {
await persistResolvedSignupEmail(resolvedEmail, state, options);
await persistResolvedSignupEmail(resolvedEmail, state, {
...options,
generatedEmailAlreadyPersisted,
});
}
return resolvedEmail;
+9 -1
View File
@@ -30,6 +30,7 @@
rerunStep7ForStep8Recovery,
reuseOrCreateTab,
sendToContentScriptResilient,
buildRegistrationEmailStateUpdates = null,
phoneVerificationHelpers = null,
setState,
shouldUseCustomRegistrationEmail,
@@ -258,8 +259,15 @@
async function resetStep8AfterEmailInUse(state, visibleStep) {
const currentEmail = String(state?.email || '').trim();
const registrationEmailUpdates = typeof buildRegistrationEmailStateUpdates === 'function'
? buildRegistrationEmailStateUpdates(state, {
currentEmail: null,
preservePrevious: true,
source: 'step8_recovery',
})
: { email: null };
await setState({
email: null,
...registrationEmailUpdates,
step8VerificationTargetEmail: '',
loginVerificationRequestedAt: null,
});