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}404142434445export function fillOTPDigitBoxes(boxes: HTMLInputElement[], code: string): void {46 const digits = code.replace(/\s/g, "").split("");47 boxes.forEach((box, i) => {48 if (i < digits.length) setNativeValue(box, digits[i]!);49 });50}5152export function fillTOTPCode(53 totpEl: HTMLInputElement | null,54 code: string55): boolean {56 if (!totpEl || !code || !isVisible(totpEl)) return false;57 if (totpEl.value.trim() !== "") return false;58 setNativeValue(totpEl, code);59 return true;60}61626364656667export function setNativeValue(68 el: HTMLInputElement | HTMLTextAreaElement,69 value: string70): void {71 const proto =72 el instanceof HTMLTextAreaElement73 ? window.HTMLTextAreaElement.prototype74 : window.HTMLInputElement.prototype;75 const nativeValueSetter = Object.getOwnPropertyDescriptor(proto, "value")?.set;76 nativeValueSetter?.call(el, value);77 el.dispatchEvent(new Event("input", { bubbles: true }));78 el.dispatchEvent(new Event("change", { bubbles: true }));79}8081export interface CardFillFields {82 number: HTMLInputElement | null;83 expMonth: HTMLInputElement | null;84 expYear: HTMLInputElement | null;85 expiryCombo: HTMLInputElement | null;86 cvc: HTMLInputElement | null;87 name: HTMLInputElement | null;88}8990export function fillCardForm(fields: CardFillFields, card: { cardNumber: string; expMonth: string; expYear: string; cvc: string; cardholderName: string }): void {91 const raw = card.cardNumber.replace(/\s/g, '');92 if (fields.number) setNativeValue(fields.number, formatCardNumber(raw));93 if (fields.expiryCombo) {94 setNativeValue(fields.expiryCombo, `${card.expMonth}/${card.expYear.slice(-2)}`);95 } else {96 if (fields.expMonth) setNativeValue(fields.expMonth, card.expMonth);97 if (fields.expYear) setNativeValue(fields.expYear, card.expYear);98 }99 if (fields.cvc) setNativeValue(fields.cvc, card.cvc);100 if (fields.name) setNativeValue(fields.name, card.cardholderName);101}102103export function formatCardNumber(raw: string): string {104 if (/^3[47]/.test(raw)) return raw.replace(/(\d{4})(\d{6})(\d{5})/, '$1 $2 $3');105 return raw.replace(/(\d{4})(?=\d)/g, '$1 ').trim();106}107108export function detectCardNetwork(number: string): import('../shared/types').CardNetwork {109 const n = number.replace(/\s/g, '');110 if (/^4/.test(n)) return 'visa';111 if (/^5[1-5]|^2[2-7]/.test(n)) return 'mastercard';112 if (/^3[47]/.test(n)) return 'amex';113 if (/^6(?:011|5)/.test(n)) return 'discover';114 return 'unknown';115}116117export function passesLuhn(number: string): boolean {118 const digits = number.replace(/\s/g, '').split('').map(Number);119 let sum = 0, isEven = false;120 for (let i = digits.length - 1; i >= 0; i--) {121 let d = digits[i]!;122 if (isEven) { d *= 2; if (d > 9) d -= 9; }123 sum += d;124 isEven = !isEven;125 }126 return sum % 10 === 0;127}128129export function extractLast4(number: string): string {130 return number.replace(/\s/g, '').slice(-4);131}