| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- const ICLOUD_MAIL_PREFIX = '[MultiPage:icloud-mail]';
- const isTopFrame = window === window.top;
- console.log(ICLOUD_MAIL_PREFIX, 'Content script loaded on', location.href, 'frame:', isTopFrame ? 'top' : 'child');
- function isMailApplicationFrame() {
- if (/\/applications\/mail2\//.test(location.pathname)) {
- return true;
- }
- return Boolean(document.querySelector('.content-container, .mail-message-defaults, .thread-participants'));
- }
- if (isTopFrame) {
- console.log(ICLOUD_MAIL_PREFIX, 'Top frame detected; waiting for mail iframe.');
- } else {
- chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
- if (message.type === 'POLL_EMAIL') {
- if (!isMailApplicationFrame()) {
- sendResponse({ ok: false, reason: 'wrong-frame' });
- return;
- }
- resetStopState();
- handlePollEmail(message.step, message.payload).then((result) => {
- sendResponse(result);
- }).catch((err) => {
- if (isStopError(err)) {
- log(`步骤 ${message.step}:已被用户停止。`, 'warn');
- sendResponse({ stopped: true, error: err.message });
- return;
- }
- log(`步骤 ${message.step}:iCloud 邮箱轮询失败:${err.message}`, 'warn');
- sendResponse({ error: err.message });
- });
- return true;
- }
- });
- function normalizeText(value) {
- return String(value || '').replace(/\s+/g, ' ').trim();
- }
- function isVisibleElement(node) {
- return Boolean(node instanceof HTMLElement)
- && (Boolean(node.offsetParent) || getComputedStyle(node).position === 'fixed');
- }
- function collectThreadItems() {
- return Array.from(document.querySelectorAll('.content-container')).filter((item) => {
- if (!isVisibleElement(item)) return false;
- return item.querySelector('.thread-participants')
- && item.querySelector('.thread-subject')
- && item.querySelector('.thread-preview');
- });
- }
- function getThreadItemMetadata(item) {
- const sender = normalizeText(item.querySelector('.thread-participants')?.textContent || '');
- const subject = normalizeText(item.querySelector('.thread-subject')?.textContent || '');
- const preview = normalizeText(item.querySelector('.thread-preview')?.textContent || '');
- const timestamp = normalizeText(item.querySelector('.thread-timestamp')?.textContent || '');
- return {
- sender,
- subject,
- preview,
- timestamp,
- combinedText: normalizeText([sender, subject, preview, timestamp].filter(Boolean).join(' ')),
- };
- }
- function buildItemSignature(item) {
- const meta = getThreadItemMetadata(item);
- return normalizeText([
- item.getAttribute('aria-label') || '',
- meta.sender,
- meta.subject,
- meta.preview,
- meta.timestamp,
- ].join('::')).slice(0, 240);
- }
- function extractVerificationCode(text) {
- const matchCn = text.match(/(?:代码为|验证码[^0-9]*?)[\s::]*(\d{6})/);
- if (matchCn) return matchCn[1];
- const matchEn = text.match(/code[:\s]+is[:\s]+(\d{6})|code[:\s]+(\d{6})/i);
- if (matchEn) return matchEn[1] || matchEn[2];
- const match6 = text.match(/\b(\d{6})\b/);
- if (match6) return match6[1];
- return null;
- }
- function readOpenedMailHeader() {
- const headerRoot = document.querySelector('.ic-efwqa7');
- if (!headerRoot) {
- return { sender: '', recipients: '', timestamp: '' };
- }
- const contactValues = Array.from(headerRoot.querySelectorAll('.contact-token .ic-x1z554'))
- .map((node) => normalizeText(node.textContent))
- .filter(Boolean);
- const sender = contactValues[0] || '';
- const recipients = contactValues.slice(1).join(' ');
- const timestamp = normalizeText(headerRoot.querySelector('.ic-rffsj8')?.textContent || '');
- return { sender, recipients, timestamp };
- }
- function getOpenedMailBodyRoot() {
- return document.querySelector('.mail-message-defaults, .pane.thread-detail-pane');
- }
- function readOpenedMailBody() {
- const bodyRoot = getOpenedMailBodyRoot();
- return normalizeText(bodyRoot?.innerText || bodyRoot?.textContent || '');
- }
- function getThreadListItemRoot(item) {
- return item?.closest?.('.thread-list-item, [role="treeitem"]') || null;
- }
- function isThreadItemSelected(item, expectedSignature = '') {
- const expected = normalizeText(expectedSignature);
- const candidates = collectThreadItems();
- const matchedItem = expected
- ? candidates.find((candidate) => buildItemSignature(candidate) === expected)
- : item;
- const root = getThreadListItemRoot(matchedItem || item);
- if (!root) {
- return false;
- }
- if (root.getAttribute('aria-selected') === 'true') {
- return true;
- }
- const className = String(root.className || '').toLowerCase();
- return /\b(selected|current|active)\b/.test(className);
- }
- function openedMailMatchesExpectedContent(expectedMeta = {}, header = null, bodyText = '') {
- const expectedSender = normalizeText(expectedMeta.sender || '').toLowerCase();
- const expectedSubject = normalizeText(expectedMeta.subject || '').toLowerCase();
- const combined = normalizeText([
- header?.sender || '',
- header?.recipients || '',
- header?.timestamp || '',
- bodyText || '',
- ].join(' ')).toLowerCase();
- if (expectedSender && combined.includes(expectedSender)) {
- return true;
- }
- if (expectedSubject && combined.includes(expectedSubject)) {
- return true;
- }
- return false;
- }
- async function waitForOpenedMailContent(item, expectedMeta = {}, timeout = 10000) {
- const expectedSignature = buildItemSignature(item);
- const start = Date.now();
- while (Date.now() - start < timeout) {
- throwIfStopped();
- const headerRoot = document.querySelector('.ic-efwqa7');
- const bodyRoot = getOpenedMailBodyRoot();
- const selected = isThreadItemSelected(item, expectedSignature);
- if (selected && (headerRoot || bodyRoot)) {
- const header = readOpenedMailHeader();
- const bodyText = normalizeText(bodyRoot?.innerText || bodyRoot?.textContent || '');
- if (openedMailMatchesExpectedContent(expectedMeta, header, bodyText)) {
- return { headerRoot, bodyRoot };
- }
- }
- await sleep(100);
- }
- throw new Error('打开邮件后未找到详情区域,请确认邮件内容已加载。');
- }
- async function openMailItemAndRead(item) {
- const expectedMeta = getThreadItemMetadata(item);
- simulateClick(item);
- const { bodyRoot } = await waitForOpenedMailContent(item, expectedMeta, 10000);
- await sleep(300);
- const header = readOpenedMailHeader();
- const bodyText = normalizeText(
- bodyRoot?.innerText || bodyRoot?.textContent || readOpenedMailBody()
- );
- return {
- ...header,
- bodyText,
- combinedText: normalizeText([header.sender, header.recipients, header.timestamp, bodyText].filter(Boolean).join(' ')),
- };
- }
- async function refreshInbox() {
- const refreshPatterns = [/刷新/i, /refresh/i, /重新载入/i];
- const candidates = document.querySelectorAll('button, [role="button"], a');
- for (const node of candidates) {
- const text = normalizeText(node.innerText || node.textContent || '');
- const label = normalizeText(node.getAttribute('aria-label') || node.getAttribute('title') || '');
- if (refreshPatterns.some((pattern) => pattern.test(text) || pattern.test(label))) {
- simulateClick(node);
- await sleep(1000);
- return;
- }
- }
- const inboxPatterns = [/收件箱/, /inbox/i];
- for (const node of candidates) {
- const text = normalizeText(node.innerText || node.textContent || '');
- const label = normalizeText(node.getAttribute('aria-label') || node.getAttribute('title') || '');
- if (inboxPatterns.some((pattern) => pattern.test(text) || pattern.test(label))) {
- simulateClick(node);
- await sleep(1000);
- return;
- }
- }
- }
- async function handlePollEmail(step, payload) {
- const { senderFilters, subjectFilters, maxAttempts, intervalMs, excludeCodes = [] } = payload;
- const excludedCodeSet = new Set(excludeCodes.filter(Boolean));
- const FALLBACK_AFTER = 3;
- const normalizedSenderFilters = senderFilters.map((filter) => String(filter || '').toLowerCase()).filter(Boolean);
- const normalizedSubjectFilters = subjectFilters.map((filter) => String(filter || '').toLowerCase()).filter(Boolean);
- log(`步骤 ${step}:开始轮询 iCloud 邮箱(最多 ${maxAttempts} 次)`);
- await waitForElement('.content-container', 10000);
- await sleep(1500);
- const existingSignatures = new Set(collectThreadItems().map(buildItemSignature));
- log(`步骤 ${step}:已记录当前 ${existingSignatures.size} 封旧邮件快照`);
- for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
- log(`步骤 ${step}:正在轮询 iCloud 邮箱,第 ${attempt}/${maxAttempts} 次`);
- if (attempt > 1) {
- await refreshInbox();
- await sleep(1200);
- }
- const items = collectThreadItems();
- const useFallback = attempt > FALLBACK_AFTER;
- for (const item of items) {
- const signature = buildItemSignature(item);
- if (!useFallback && existingSignatures.has(signature)) {
- continue;
- }
- const meta = getThreadItemMetadata(item);
- const lowerSender = meta.sender.toLowerCase();
- const lowerSubject = normalizeText([meta.subject, meta.preview].join(' ')).toLowerCase();
- const senderMatch = normalizedSenderFilters.some((filter) => lowerSender.includes(filter));
- const subjectMatch = normalizedSubjectFilters.some((filter) => lowerSubject.includes(filter));
- if (!senderMatch && !subjectMatch) {
- continue;
- }
- let code = extractVerificationCode(meta.combinedText);
- let opened = null;
- if (!code) {
- opened = await openMailItemAndRead(item);
- const openedSender = opened.sender.toLowerCase();
- const openedBody = opened.bodyText.toLowerCase();
- const openedSenderMatch = normalizedSenderFilters.some((filter) => openedSender.includes(filter));
- const openedSubjectMatch = normalizedSubjectFilters.some((filter) => openedBody.includes(filter));
- if (!openedSenderMatch && !openedSubjectMatch && !senderMatch && !subjectMatch) {
- continue;
- }
- code = extractVerificationCode(opened.combinedText);
- }
- if (!code) {
- continue;
- }
- if (excludedCodeSet.has(code)) {
- log(`步骤 ${step}:跳过排除的验证码:${code}`, 'info');
- continue;
- }
- const source = useFallback && existingSignatures.has(signature) ? '回退匹配邮件' : '新邮件';
- log(`步骤 ${step}:已找到验证码:${code}(来源:${source})`, 'ok');
- return {
- ok: true,
- code,
- emailTimestamp: Date.now(),
- preview: (opened?.combinedText || meta.combinedText).slice(0, 160),
- };
- }
- if (attempt === FALLBACK_AFTER + 1) {
- log(`步骤 ${step}:连续 ${FALLBACK_AFTER} 次未发现新邮件,开始回退到首封匹配邮件`, 'warn');
- }
- if (attempt < maxAttempts) {
- await sleep(intervalMs);
- }
- }
- throw new Error(
- `${Math.round((maxAttempts * intervalMs) / 1000)} 秒后仍未在 iCloud 邮箱中找到新的匹配邮件。请手动检查收件箱。`
- );
- }
- }
|