feat: integrate iCloud hide my email generation into OAuth flow
- 合并 PR #72 的核心改动:新增 iCloud Hide My Email 生成、alias 管理与成功后自动标记/删除流程 - 本地补充修复:解决与 Cloudflare Temp Email 分支差异的冲突,并补齐合并后的测试常量 - 影响范围:background 邮箱生成与 Step 9 收尾、sidepanel alias 管理 UI、manifest/rules 与新增 iCloud 测试
This commit is contained in:
@@ -189,6 +189,20 @@ function cancelPendingCommands() {}
|
||||
function normalizeAutoRunFallbackThreadIntervalMinutes(value) {
|
||||
return Math.max(0, Math.floor(Number(value) || 0));
|
||||
}
|
||||
function buildAutoRunRoundSummaries(totalRuns, rawSummaries = []) {
|
||||
return Array.from({ length: totalRuns }, (_, index) => ({
|
||||
round: index + 1,
|
||||
status: rawSummaries[index]?.status || 'pending',
|
||||
attempts: rawSummaries[index]?.attempts || 0,
|
||||
failureReasons: [...(rawSummaries[index]?.failureReasons || [])],
|
||||
finalFailureReason: rawSummaries[index]?.finalFailureReason || '',
|
||||
}));
|
||||
}
|
||||
function serializeAutoRunRoundSummaries(totalRuns, roundSummaries = []) {
|
||||
return buildAutoRunRoundSummaries(totalRuns, roundSummaries);
|
||||
}
|
||||
async function logAutoRunFinalSummary() {}
|
||||
async function waitBetweenAutoRunRounds() {}
|
||||
|
||||
const chrome = {
|
||||
runtime: {
|
||||
|
||||
@@ -0,0 +1,265 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('fs');
|
||||
|
||||
const source = fs.readFileSync('background.js', 'utf8');
|
||||
|
||||
function extractFunction(name) {
|
||||
const markers = [`async function ${name}(`, `function ${name}(`];
|
||||
const start = markers
|
||||
.map((marker) => source.indexOf(marker))
|
||||
.find((index) => index >= 0);
|
||||
if (start < 0) {
|
||||
throw new Error(`missing function ${name}`);
|
||||
}
|
||||
|
||||
let parenDepth = 0;
|
||||
let signatureEnded = false;
|
||||
let braceStart = -1;
|
||||
for (let i = start; i < source.length; i += 1) {
|
||||
const ch = source[i];
|
||||
if (ch === '(') {
|
||||
parenDepth += 1;
|
||||
} else if (ch === ')') {
|
||||
parenDepth -= 1;
|
||||
if (parenDepth === 0) {
|
||||
signatureEnded = true;
|
||||
}
|
||||
} else if (ch === '{' && signatureEnded) {
|
||||
braceStart = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (braceStart < 0) {
|
||||
throw new Error(`missing body for function ${name}`);
|
||||
}
|
||||
|
||||
let depth = 0;
|
||||
let end = braceStart;
|
||||
for (; end < source.length; end += 1) {
|
||||
const ch = source[end];
|
||||
if (ch === '{') depth += 1;
|
||||
if (ch === '}') {
|
||||
depth -= 1;
|
||||
if (depth === 0) {
|
||||
end += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return source.slice(start, end);
|
||||
}
|
||||
|
||||
const bundle = [
|
||||
extractFunction('normalizeEmailGenerator'),
|
||||
extractFunction('getEmailGeneratorLabel'),
|
||||
extractFunction('normalizePersistentSettingValue'),
|
||||
extractFunction('finalizeIcloudAliasAfterSuccessfulFlow'),
|
||||
].join('\n');
|
||||
|
||||
function createApi(overrides = {}) {
|
||||
return new Function('overrides', `
|
||||
const HOTMAIL_PROVIDER = 'hotmail-api';
|
||||
const HOTMAIL_SERVICE_MODE_LOCAL = 'local';
|
||||
const CLOUDFLARE_TEMP_EMAIL_GENERATOR = 'cloudflare-temp-email';
|
||||
const DEFAULT_LOCAL_CPA_STEP9_MODE = 'submit';
|
||||
const DEFAULT_HOTMAIL_REMOTE_BASE_URL = '';
|
||||
const DEFAULT_HOTMAIL_LOCAL_BASE_URL = 'http://127.0.0.1:17373';
|
||||
const PERSISTED_SETTING_DEFAULTS = {
|
||||
mailProvider: '163',
|
||||
autoStepDelaySeconds: null,
|
||||
};
|
||||
|
||||
const calls = {
|
||||
setUsed: [],
|
||||
logs: [],
|
||||
deletes: [],
|
||||
listCalls: 0,
|
||||
};
|
||||
|
||||
function normalizeIcloudHost(value) {
|
||||
const normalized = String(value || '').trim().toLowerCase();
|
||||
return ['icloud.com', 'icloud.com.cn'].includes(normalized) ? normalized : '';
|
||||
}
|
||||
function normalizePanelMode(value = '') {
|
||||
return String(value || '').trim().toLowerCase() === 'sub2api' ? 'sub2api' : 'cpa';
|
||||
}
|
||||
function normalizeMailProvider(value = '') {
|
||||
return String(value || '').trim().toLowerCase() || '163';
|
||||
}
|
||||
function normalizeAutoRunFallbackThreadIntervalMinutes(value) {
|
||||
return Math.max(0, Math.floor(Number(value) || 0));
|
||||
}
|
||||
function normalizeAutoRunDelayMinutes(value) {
|
||||
return Math.max(1, Math.floor(Number(value) || 30));
|
||||
}
|
||||
function normalizeAutoStepDelaySeconds(value, fallback = null) {
|
||||
const numeric = Number(value);
|
||||
return Number.isFinite(numeric) ? Math.max(0, Math.floor(numeric)) : fallback;
|
||||
}
|
||||
function normalizeHotmailServiceMode() {
|
||||
return HOTMAIL_SERVICE_MODE_LOCAL;
|
||||
}
|
||||
function normalizeHotmailRemoteBaseUrl(value = '') {
|
||||
return String(value || '').trim() || DEFAULT_HOTMAIL_REMOTE_BASE_URL;
|
||||
}
|
||||
function normalizeHotmailLocalBaseUrl(value = '') {
|
||||
return String(value || '').trim() || DEFAULT_HOTMAIL_LOCAL_BASE_URL;
|
||||
}
|
||||
function normalizeCloudflareDomain(value = '') {
|
||||
return String(value || '').trim().toLowerCase();
|
||||
}
|
||||
function normalizeCloudflareDomains(values = []) {
|
||||
return Array.isArray(values) ? values : [];
|
||||
}
|
||||
function normalizeHotmailAccounts(values = []) {
|
||||
return Array.isArray(values) ? values : [];
|
||||
}
|
||||
function getManualAliasUsageMap(state) {
|
||||
return { ...(state?.manualAliasUsage || {}) };
|
||||
}
|
||||
function getPreservedAliasMap(state) {
|
||||
return { ...(state?.preservedAliases || {}) };
|
||||
}
|
||||
function isAliasPreserved(state, email) {
|
||||
return Boolean(getPreservedAliasMap(state)[String(email || '').trim().toLowerCase()]);
|
||||
}
|
||||
async function setIcloudAliasUsedState(payload, options = {}) {
|
||||
calls.setUsed.push({ payload, options });
|
||||
}
|
||||
async function addLog(message, level = 'info') {
|
||||
calls.logs.push({ message, level });
|
||||
}
|
||||
async function deleteIcloudAlias(alias) {
|
||||
calls.deletes.push(alias);
|
||||
}
|
||||
async function listIcloudAliases() {
|
||||
calls.listCalls += 1;
|
||||
return overrides.listIcloudAliases ? overrides.listIcloudAliases() : [];
|
||||
}
|
||||
function findIcloudAliasByEmail(aliases, email) {
|
||||
return (aliases || []).find((alias) => String(alias.email || '').toLowerCase() === String(email || '').toLowerCase()) || null;
|
||||
}
|
||||
function getErrorMessage(error) {
|
||||
return String(typeof error === 'string' ? error : error?.message || '');
|
||||
}
|
||||
|
||||
${bundle}
|
||||
|
||||
return {
|
||||
calls,
|
||||
normalizeEmailGenerator,
|
||||
getEmailGeneratorLabel,
|
||||
normalizePersistentSettingValue,
|
||||
finalizeIcloudAliasAfterSuccessfulFlow,
|
||||
};
|
||||
`)(overrides);
|
||||
}
|
||||
|
||||
test('normalizeEmailGenerator and label support icloud', () => {
|
||||
const api = createApi();
|
||||
assert.equal(api.normalizeEmailGenerator('icloud'), 'icloud');
|
||||
assert.equal(api.getEmailGeneratorLabel('icloud'), 'iCloud 隐私邮箱');
|
||||
});
|
||||
|
||||
test('normalizePersistentSettingValue handles icloud settings', () => {
|
||||
const api = createApi();
|
||||
assert.equal(api.normalizePersistentSettingValue('icloudHostPreference', 'icloud.com'), 'icloud.com');
|
||||
assert.equal(api.normalizePersistentSettingValue('icloudHostPreference', 'bad-host'), 'auto');
|
||||
assert.equal(api.normalizePersistentSettingValue('autoDeleteUsedIcloudAlias', 1), true);
|
||||
});
|
||||
|
||||
test('finalizeIcloudAliasAfterSuccessfulFlow marks icloud aliases as used without deleting when auto-delete is off', async () => {
|
||||
const api = createApi();
|
||||
const result = await api.finalizeIcloudAliasAfterSuccessfulFlow({
|
||||
email: 'alias@icloud.com',
|
||||
emailGenerator: 'icloud',
|
||||
autoDeleteUsedIcloudAlias: false,
|
||||
manualAliasUsage: {},
|
||||
preservedAliases: {},
|
||||
});
|
||||
|
||||
assert.deepEqual(result, { handled: true, deleted: false });
|
||||
assert.equal(api.calls.setUsed.length, 1);
|
||||
assert.equal(api.calls.listCalls, 0);
|
||||
assert.equal(api.calls.deletes.length, 0);
|
||||
});
|
||||
|
||||
test('finalizeIcloudAliasAfterSuccessfulFlow skips deleting preserved aliases', async () => {
|
||||
const api = createApi();
|
||||
const result = await api.finalizeIcloudAliasAfterSuccessfulFlow({
|
||||
email: 'alias@icloud.com',
|
||||
emailGenerator: 'icloud',
|
||||
autoDeleteUsedIcloudAlias: true,
|
||||
manualAliasUsage: {},
|
||||
preservedAliases: { 'alias@icloud.com': true },
|
||||
});
|
||||
|
||||
assert.deepEqual(result, { handled: true, deleted: false });
|
||||
assert.equal(api.calls.setUsed.length, 1);
|
||||
assert.equal(api.calls.listCalls, 0);
|
||||
assert.equal(api.calls.deletes.length, 0);
|
||||
});
|
||||
|
||||
test('finalizeIcloudAliasAfterSuccessfulFlow skips deleting aliases that are preserved in the latest alias list', async () => {
|
||||
const api = createApi({
|
||||
listIcloudAliases() {
|
||||
return [
|
||||
{ email: 'alias@icloud.com', anonymousId: 'anon-1', preserved: true },
|
||||
];
|
||||
},
|
||||
});
|
||||
|
||||
const result = await api.finalizeIcloudAliasAfterSuccessfulFlow({
|
||||
email: 'alias@icloud.com',
|
||||
emailGenerator: 'icloud',
|
||||
autoDeleteUsedIcloudAlias: true,
|
||||
manualAliasUsage: {},
|
||||
preservedAliases: {},
|
||||
});
|
||||
|
||||
assert.deepEqual(result, { handled: true, deleted: false });
|
||||
assert.equal(api.calls.setUsed.length, 1);
|
||||
assert.equal(api.calls.listCalls, 1);
|
||||
assert.equal(api.calls.deletes.length, 0);
|
||||
});
|
||||
|
||||
test('finalizeIcloudAliasAfterSuccessfulFlow deletes alias when auto-delete is enabled and alias exists', async () => {
|
||||
const api = createApi({
|
||||
listIcloudAliases() {
|
||||
return [
|
||||
{ email: 'alias@icloud.com', anonymousId: 'anon-1', preserved: false },
|
||||
];
|
||||
},
|
||||
});
|
||||
|
||||
const result = await api.finalizeIcloudAliasAfterSuccessfulFlow({
|
||||
email: 'alias@icloud.com',
|
||||
emailGenerator: 'icloud',
|
||||
autoDeleteUsedIcloudAlias: true,
|
||||
manualAliasUsage: {},
|
||||
preservedAliases: {},
|
||||
});
|
||||
|
||||
assert.deepEqual(result, { handled: true, deleted: true });
|
||||
assert.equal(api.calls.setUsed.length, 1);
|
||||
assert.equal(api.calls.listCalls, 1);
|
||||
assert.deepEqual(api.calls.deletes, [
|
||||
{ email: 'alias@icloud.com', anonymousId: 'anon-1', preserved: false },
|
||||
]);
|
||||
});
|
||||
|
||||
test('finalizeIcloudAliasAfterSuccessfulFlow ignores non-icloud flows', async () => {
|
||||
const api = createApi();
|
||||
const result = await api.finalizeIcloudAliasAfterSuccessfulFlow({
|
||||
email: 'plain@example.com',
|
||||
emailGenerator: 'duck',
|
||||
autoDeleteUsedIcloudAlias: true,
|
||||
manualAliasUsage: {},
|
||||
preservedAliases: {},
|
||||
});
|
||||
|
||||
assert.deepEqual(result, { handled: false, deleted: false });
|
||||
assert.equal(api.calls.setUsed.length, 0);
|
||||
});
|
||||
@@ -0,0 +1,121 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const {
|
||||
findIcloudAliasArray,
|
||||
findIcloudAliasByEmail,
|
||||
getConfiguredIcloudHostPreference,
|
||||
getIcloudHostHintFromMessage,
|
||||
getIcloudLoginUrlForHost,
|
||||
getIcloudSetupUrlForHost,
|
||||
normalizeBooleanMap,
|
||||
normalizeIcloudAliasList,
|
||||
normalizeIcloudAliasRecord,
|
||||
normalizeIcloudHost,
|
||||
pickReusableIcloudAlias,
|
||||
toNormalizedEmailSet,
|
||||
} = require('../icloud-utils.js');
|
||||
|
||||
test('normalizeIcloudHost and host preference helpers resolve supported hosts', () => {
|
||||
assert.equal(normalizeIcloudHost('www.icloud.com'), 'icloud.com');
|
||||
assert.equal(normalizeIcloudHost('setup.icloud.com.cn'), 'icloud.com.cn');
|
||||
assert.equal(normalizeIcloudHost('example.com'), '');
|
||||
|
||||
assert.equal(getConfiguredIcloudHostPreference({ icloudHostPreference: 'icloud.com' }), 'icloud.com');
|
||||
assert.equal(getConfiguredIcloudHostPreference({ icloudHostPreference: 'auto' }), '');
|
||||
assert.equal(getIcloudLoginUrlForHost('icloud.com.cn'), 'https://www.icloud.com.cn/');
|
||||
assert.equal(getIcloudSetupUrlForHost('icloud.com'), 'https://setup.icloud.com/setup/ws/1');
|
||||
});
|
||||
|
||||
test('getIcloudHostHintFromMessage can infer host from error text', () => {
|
||||
assert.equal(getIcloudHostHintFromMessage('status 401 from setup.icloud.com.cn/setup/ws/1'), 'icloud.com.cn');
|
||||
assert.equal(getIcloudHostHintFromMessage('request failed at https://www.icloud.com/'), 'icloud.com');
|
||||
assert.equal(getIcloudHostHintFromMessage('unknown host'), '');
|
||||
});
|
||||
|
||||
test('findIcloudAliasArray finds nested alias collections', () => {
|
||||
const payload = {
|
||||
result: {
|
||||
data: {
|
||||
items: [
|
||||
{ hme: 'first@icloud.com', anonymousId: 'a1' },
|
||||
{ hme: 'second@icloud.com', anonymousId: 'a2' },
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
assert.deepEqual(findIcloudAliasArray(payload), payload.result.data.items);
|
||||
});
|
||||
|
||||
test('normalizeIcloudAliasRecord merges used and preserved state', () => {
|
||||
const alias = normalizeIcloudAliasRecord({
|
||||
anonymousId: 'alias-1',
|
||||
hme: 'Demo@iCloud.com',
|
||||
label: 'Test',
|
||||
note: 'Created by test',
|
||||
state: 'active',
|
||||
}, {
|
||||
usedEmails: ['demo@icloud.com'],
|
||||
preservedEmails: ['demo@icloud.com'],
|
||||
});
|
||||
|
||||
assert.deepEqual(alias, {
|
||||
anonymousId: 'alias-1',
|
||||
email: 'demo@icloud.com',
|
||||
label: 'Test',
|
||||
note: 'Created by test',
|
||||
state: 'active',
|
||||
active: true,
|
||||
used: true,
|
||||
preserved: true,
|
||||
createdAt: null,
|
||||
});
|
||||
});
|
||||
|
||||
test('normalizeIcloudAliasList orders active unused aliases before used aliases', () => {
|
||||
const aliases = normalizeIcloudAliasList({
|
||||
hmeEmails: [
|
||||
{ hme: 'used@icloud.com', anonymousId: 'u1', active: true },
|
||||
{ hme: 'fresh@icloud.com', anonymousId: 'f1', active: true },
|
||||
{ hme: 'inactive@icloud.com', anonymousId: 'i1', active: false },
|
||||
],
|
||||
}, {
|
||||
usedEmails: ['used@icloud.com'],
|
||||
preservedEmails: ['inactive@icloud.com'],
|
||||
});
|
||||
|
||||
assert.deepEqual(aliases.map((alias) => alias.email), [
|
||||
'fresh@icloud.com',
|
||||
'used@icloud.com',
|
||||
'inactive@icloud.com',
|
||||
]);
|
||||
assert.equal(aliases[2].preserved, true);
|
||||
});
|
||||
|
||||
test('pickReusableIcloudAlias and findIcloudAliasByEmail select expected aliases', () => {
|
||||
const aliases = normalizeIcloudAliasList({
|
||||
hmeEmails: [
|
||||
{ hme: 'used@icloud.com', anonymousId: 'u1', active: true },
|
||||
{ hme: 'fresh@icloud.com', anonymousId: 'f1', active: true },
|
||||
],
|
||||
}, {
|
||||
usedEmails: ['used@icloud.com'],
|
||||
});
|
||||
|
||||
assert.equal(pickReusableIcloudAlias(aliases)?.email, 'fresh@icloud.com');
|
||||
assert.equal(findIcloudAliasByEmail(aliases, 'FRESH@ICLOUD.COM')?.anonymousId, 'f1');
|
||||
});
|
||||
|
||||
test('normalizeBooleanMap and toNormalizedEmailSet normalize keys and truthy entries', () => {
|
||||
const normalized = normalizeBooleanMap({
|
||||
' Demo@icloud.com ': 1,
|
||||
'skip@icloud.com': 0,
|
||||
});
|
||||
|
||||
assert.deepEqual(normalized, {
|
||||
'demo@icloud.com': true,
|
||||
'skip@icloud.com': false,
|
||||
});
|
||||
assert.deepEqual([...toNormalizedEmailSet(normalized)], ['demo@icloud.com']);
|
||||
});
|
||||
@@ -110,6 +110,11 @@ async function addLog(message) {
|
||||
logMessages.push(message);
|
||||
}
|
||||
|
||||
async function finalizeIcloudAliasAfterSuccessfulFlow() {}
|
||||
function shouldUseCustomRegistrationEmail() {
|
||||
return false;
|
||||
}
|
||||
|
||||
${bundle}
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user