重构duckduckgo邮箱获取
This commit is contained in:
@@ -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,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user