|
| 1 | +const MAX_CODE_CONTEXT_LENGTH = 120; |
| 2 | + |
| 3 | +const htmlEntityMap: Record<string, string> = { |
| 4 | + amp: "&", |
| 5 | + lt: "<", |
| 6 | + gt: ">", |
| 7 | + quot: '"', |
| 8 | + apos: "'", |
| 9 | + nbsp: " ", |
| 10 | + ensp: " ", |
| 11 | + emsp: " ", |
| 12 | + thinsp: " ", |
| 13 | + zwnj: "", |
| 14 | + zwj: "", |
| 15 | +}; |
| 16 | + |
| 17 | +export const decodeHtmlEntities = (value: string): string => { |
| 18 | + return value.replace(/&(#x?[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]+);/g, (entity, code: string) => { |
| 19 | + const lowerCode = code.toLowerCase(); |
| 20 | + if (lowerCode.startsWith("#x")) { |
| 21 | + const charCode = parseInt(lowerCode.slice(2), 16); |
| 22 | + return Number.isFinite(charCode) ? String.fromCodePoint(charCode) : entity; |
| 23 | + } |
| 24 | + if (lowerCode.startsWith("#")) { |
| 25 | + const charCode = parseInt(lowerCode.slice(1), 10); |
| 26 | + return Number.isFinite(charCode) ? String.fromCodePoint(charCode) : entity; |
| 27 | + } |
| 28 | + return htmlEntityMap[lowerCode] ?? entity; |
| 29 | + }); |
| 30 | +}; |
| 31 | + |
| 32 | +export const htmlToPlainText = (html: string): string => { |
| 33 | + return decodeHtmlEntities(html) |
| 34 | + .replace(/<\s*(script|style)[^>]*>[\s\S]*?<\s*\/\s*\1\s*>/gi, " ") |
| 35 | + .replace(/<\s*br\s*\/?\s*>/gi, "\n") |
| 36 | + .replace(/<\s*\/\s*(p|div|tr|td|th|li|h[1-6]|table|section|article)\s*>/gi, "\n") |
| 37 | + .replace(/<[^>]+>/g, " ") |
| 38 | + .replace(/[\u200B-\u200D\uFEFF]/g, "") |
| 39 | + .replace(/[ \t\r\f\v]+/g, " ") |
| 40 | + .replace(/\n\s+/g, "\n") |
| 41 | + .replace(/\n{3,}/g, "\n\n") |
| 42 | + .trim(); |
| 43 | +}; |
| 44 | + |
| 45 | +export const buildSearchableMailText = (subject?: string, text?: string, html?: string): string => { |
| 46 | + return [subject || "", text || "", html ? htmlToPlainText(html) : ""] |
| 47 | + .filter(part => part.trim().length > 0) |
| 48 | + .join("\n"); |
| 49 | +}; |
| 50 | + |
| 51 | +const compactCandidateCode = (value: string): string => { |
| 52 | + return value.replace(/[\s\-_.::]/g, ""); |
| 53 | +}; |
| 54 | + |
| 55 | +const isLikelyDateOrTime = (code: string, context: string): boolean => { |
| 56 | + if (/^(19|20)\d{2}$/.test(code)) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + if (/^\d{8}$/.test(code) && /(?:date|日期|时间|time|expires?|过期|有效期)/i.test(context)) { |
| 60 | + return true; |
| 61 | + } |
| 62 | + if (/^\d{4}$/.test(code) && /[::]\s*\d{2}/.test(context)) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + return false; |
| 66 | +}; |
| 67 | + |
| 68 | +const distanceToNearestKeyword = (context: string, codeStartInContext: number): number => { |
| 69 | + const keywordPattern = /(verification|verify|login|sign.?in|security|auth|authentication|one.?time|otp|passcode|code|pin|temporary|确认|验证|验证码|校验码|动态码|登录|登入|安全|口令|一次性|临时)/ig; |
| 70 | + let nearest = Number.POSITIVE_INFINITY; |
| 71 | + let match: RegExpExecArray | null; |
| 72 | + while ((match = keywordPattern.exec(context)) !== null) { |
| 73 | + const keywordStart = match.index; |
| 74 | + const keywordEnd = match.index + match[0].length; |
| 75 | + const distance = codeStartInContext < keywordStart |
| 76 | + ? keywordStart - codeStartInContext |
| 77 | + : Math.max(0, codeStartInContext - keywordEnd); |
| 78 | + nearest = Math.min(nearest, distance); |
| 79 | + } |
| 80 | + return nearest; |
| 81 | +}; |
| 82 | + |
| 83 | +const scoreCandidate = (code: string, context: string, index: number, codeStartInContext: number): number => { |
| 84 | + let score = 0; |
| 85 | + |
| 86 | + if (code.length === 6) score += 30; |
| 87 | + if (code.length === 8) score += 20; |
| 88 | + if (code.length === 4) score += 10; |
| 89 | + if (code.length >= 5 && code.length <= 8) score += 10; |
| 90 | + |
| 91 | + const keywordDistance = distanceToNearestKeyword(context, codeStartInContext); |
| 92 | + if (Number.isFinite(keywordDistance)) { |
| 93 | + score += Math.max(0, 90 - keywordDistance); |
| 94 | + } |
| 95 | + if (/(验证码|校验码|动态码|一次性代码|登录代码|安全代码)/.test(context)) { |
| 96 | + score += 20; |
| 97 | + } |
| 98 | + if (/(openai|chatgpt|google|github|telegram|microsoft|apple|discord|cloudflare)/i.test(context)) { |
| 99 | + score += 15; |
| 100 | + } |
| 101 | + if (/\b(code|pin|otp)\b\s*(?:is|:|:|-)/i.test(context) || /(?:验证码|代码|校验码|动态码)\s*(?:是|为|:|:)/.test(context)) { |
| 102 | + score += 55; |
| 103 | + } |
| 104 | + if (/(?:expires?|expire|valid|有效|过期|分钟|minutes?|mins?)/i.test(context)) { |
| 105 | + score += 5; |
| 106 | + } |
| 107 | + if (isLikelyDateOrTime(code, context)) { |
| 108 | + score -= 100; |
| 109 | + } |
| 110 | + |
| 111 | + score -= Math.min(index / 100, 15); |
| 112 | + return score; |
| 113 | +}; |
| 114 | + |
| 115 | +export const extractVerificationCode = (input: string): string => { |
| 116 | + const normalized = decodeHtmlEntities(input) |
| 117 | + .replace(/[\u200B-\u200D\uFEFF]/g, "") |
| 118 | + .replace(/[0-9]/g, char => String.fromCharCode(char.charCodeAt(0) - 0xFEE0)); |
| 119 | + const candidates: { code: string; score: number; index: number }[] = []; |
| 120 | + const seen = new Set<string>(); |
| 121 | + const codePattern = /(?<!\d)(?:\d[\s\-_.::]?){4,8}\d?(?!\d)/g; |
| 122 | + let match: RegExpExecArray | null; |
| 123 | + |
| 124 | + while ((match = codePattern.exec(normalized)) !== null) { |
| 125 | + const code = compactCandidateCode(match[0]); |
| 126 | + if (!/^\d{4,8}$/.test(code) || seen.has(code)) { |
| 127 | + continue; |
| 128 | + } |
| 129 | + const start = Math.max(0, match.index - MAX_CODE_CONTEXT_LENGTH); |
| 130 | + const end = Math.min(normalized.length, match.index + match[0].length + MAX_CODE_CONTEXT_LENGTH); |
| 131 | + const context = normalized.slice(start, end); |
| 132 | + const score = scoreCandidate(code, context, match.index, match.index - start); |
| 133 | + if (score > 0) { |
| 134 | + candidates.push({ code, score, index: match.index }); |
| 135 | + seen.add(code); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + candidates.sort((a, b) => b.score - a.score || a.index - b.index); |
| 140 | + return candidates[0]?.code || ""; |
| 141 | +}; |
0 commit comments