1import { isVisible } from "./detect";2import { extractDomain } from "../shared/domain";34function getRegistrableDomain(hostname: string): string {5 return extractDomain(hostname) ?? hostname;6}78function checkFormAction(form: HTMLFormElement | null, expectedDomain: string): boolean {9 if (!form?.action) return true;10 try {11 const actionUrl = new URL(form.action);12 const actionDomain = getRegistrableDomain(actionUrl.hostname);13 if (actionDomain !== expectedDomain) {14 return false;15 }16 } catch {}17 return true;18}1920export function fillFields(21 usernameEl: HTMLInputElement | null,22 passwordEl: HTMLInputElement | null,23 username: string,24 password: string,25 form?: HTMLFormElement | null,26 expectedDomain?: string27): void {28 if (expectedDomain && form && !checkFormAction(form, expectedDomain)) {29 return;30 }31 32 33 if (usernameEl && username) {34 setNativeValue(usernameEl, username);35 }36 if (passwordEl && isVisible(passwordEl)) {37 setNativeValue(passwordEl, password);38 }39}40414243444546export function setNativeValue(47 el: HTMLInputElement | HTMLTextAreaElement,48 value: string49): void {50 const proto =51 el instanceof HTMLTextAreaElement52 ? window.HTMLTextAreaElement.prototype53 : window.HTMLInputElement.prototype;54 const nativeValueSetter = Object.getOwnPropertyDescriptor(proto, "value")?.set;55 nativeValueSetter?.call(el, value);56 el.dispatchEvent(new Event("input", { bubbles: true }));57 el.dispatchEvent(new Event("change", { bubbles: true }));58}5960export interface CardFillFields {61 number: HTMLInputElement | null;62 expMonth: HTMLInputElement | null;63 expYear: HTMLInputElement | null;64 expiryCombo: HTMLInputElement | null;65 cvc: HTMLInputElement | null;66 name: HTMLInputElement | null;67}6869export function fillCardForm(fields: CardFillFields, card: { cardNumber: string; expMonth: string; expYear: string; cvc: string; cardholderName: string }): void {70 const raw = card.cardNumber.replace(/\s/g, '');71 if (fields.number) setNativeValue(fields.number, formatCardNumber(raw));72 if (fields.expiryCombo) {73 setNativeValue(fields.expiryCombo, `${card.expMonth}/${card.expYear.slice(-2)}`);74 } else {75 if (fields.expMonth) setNativeValue(fields.expMonth, card.expMonth);76 if (fields.expYear) setNativeValue(fields.expYear, card.expYear);77 }78 if (fields.cvc) setNativeValue(fields.cvc, card.cvc);79 if (fields.name) setNativeValue(fields.name, card.cardholderName);80}8182export function formatCardNumber(raw: string): string {83 if (/^3[47]/.test(raw)) return raw.replace(/(\d{4})(\d{6})(\d{5})/, '$1 $2 $3');84 return raw.replace(/(\d{4})(?=\d)/g, '$1 ').trim();85}8687export function detectCardNetwork(number: string): import('../shared/types').CardNetwork {88 const n = number.replace(/\s/g, '');89 if (/^4/.test(n)) return 'visa';90 if (/^5[1-5]|^2[2-7]/.test(n)) return 'mastercard';91 if (/^3[47]/.test(n)) return 'amex';92 if (/^6(?:011|5)/.test(n)) return 'discover';93 return 'unknown';94}9596export function passesLuhn(number: string): boolean {97 const digits = number.replace(/\s/g, '').split('').map(Number);98 let sum = 0, isEven = false;99 for (let i = digits.length - 1; i >= 0; i--) {100 let d = digits[i]!;101 if (isEven) { d *= 2; if (d > 9) d -= 9; }102 sum += d;103 isEven = !isEven;104 }105 return sum % 10 === 0;106}107108export function extractLast4(number: string): string {109 return number.replace(/\s/g, '').slice(-4);110}