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 13 14 15 if (actionUrl.protocol !== "https:") return false;16 const host = actionUrl.hostname;17 if (getRegistrableDomain(host) !== expectedDomain) return false;18 if (host !== expectedDomain && !host.endsWith(`.${expectedDomain}`)) {19 return false;20 }21 } catch {}22 return true;23}2425export function fillFields(26 usernameEl: HTMLInputElement | null,27 passwordEl: HTMLInputElement | null,28 username: string,29 password: string,30 form?: HTMLFormElement | null,31 expectedDomain?: string,32 emailEl?: HTMLInputElement | null,33 email?: string34): void {35 if (expectedDomain && form && !checkFormAction(form, expectedDomain)) {36 return;37 }38 39 40 if (usernameEl && username) {41 setNativeValue(usernameEl, username);42 }43 if (passwordEl && isVisible(passwordEl)) {44 setNativeValue(passwordEl, password);45 }46 if (emailEl && email && isVisible(emailEl)) {47 setNativeValue(emailEl, email);48 }49}505152535455export function fillOTPDigitBoxes(boxes: HTMLInputElement[], code: string): void {56 const digits = code.replace(/\s/g, "").split("");57 boxes.forEach((box, i) => {58 if (i < digits.length) setNativeValue(box, digits[i]!);59 });60}6162export function fillTOTPCode(63 totpEl: HTMLInputElement | null,64 code: string65): boolean {66 if (!totpEl || !code || !isVisible(totpEl)) return false;67 if (totpEl.value.trim() !== "") return false;68 setNativeValue(totpEl, code);69 return true;70}71727374757677export function setNativeValue(78 el: HTMLInputElement | HTMLTextAreaElement,79 value: string80): void {81 const proto =82 el instanceof HTMLTextAreaElement83 ? window.HTMLTextAreaElement.prototype84 : window.HTMLInputElement.prototype;85 const nativeValueSetter = Object.getOwnPropertyDescriptor(proto, "value")?.set;86 nativeValueSetter?.call(el, value);87 el.dispatchEvent(new Event("input", { bubbles: true }));88 el.dispatchEvent(new Event("change", { bubbles: true }));89}9091export interface CardFillFields {92 number: HTMLInputElement | null;93 expMonth: HTMLInputElement | null;94 expYear: HTMLInputElement | null;95 expiryCombo: HTMLInputElement | null;96 cvc: HTMLInputElement | null;97 name: HTMLInputElement | null;98}99100export function fillCardForm(fields: CardFillFields, card: { cardNumber: string; expMonth: string; expYear: string; cvc: string; cardholderName: string }): void {101 102 103 const raw = card.cardNumber.replace(/\s/g, '');104 if (fields.number && isVisible(fields.number)) setNativeValue(fields.number, formatCardNumber(raw));105 if (fields.expiryCombo && isVisible(fields.expiryCombo)) {106 setNativeValue(fields.expiryCombo, `${card.expMonth}/${card.expYear.slice(-2)}`);107 } else {108 if (fields.expMonth && isVisible(fields.expMonth)) setNativeValue(fields.expMonth, card.expMonth);109 if (fields.expYear && isVisible(fields.expYear)) setNativeValue(fields.expYear, card.expYear);110 }111 if (fields.cvc && isVisible(fields.cvc)) setNativeValue(fields.cvc, card.cvc);112 if (fields.name && isVisible(fields.name)) setNativeValue(fields.name, card.cardholderName);113}114115export function formatCardNumber(raw: string): string {116 if (/^3[47]/.test(raw)) return raw.replace(/(\d{4})(\d{6})(\d{5})/, '$1 $2 $3');117 return raw.replace(/(\d{4})(?=\d)/g, '$1 ').trim();118}119120export function detectCardNetwork(number: string): import('../shared/types').CardNetwork {121 const n = number.replace(/\s/g, '');122 if (/^4/.test(n)) return 'visa';123 if (/^5[1-5]|^2[2-7]/.test(n)) return 'mastercard';124 if (/^3[47]/.test(n)) return 'amex';125 if (/^6(?:011|5)/.test(n)) return 'discover';126 return 'unknown';127}128129export function passesLuhn(number: string): boolean {130 const digits = number.replace(/\s/g, '').split('').map(Number);131 let sum = 0, isEven = false;132 for (let i = digits.length - 1; i >= 0; i--) {133 let d = digits[i]!;134 if (isEven) { d *= 2; if (d > 9) d -= 9; }135 sum += d;136 isEven = !isEven;137 }138 return sum % 10 === 0;139}140141export function extractLast4(number: string): string {142 return number.replace(/\s/g, '').slice(-4);143}