fill.ts

4.1 KB · raw

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  // Fill username regardless of visibility — on SPA step-by-step login flows32  // the username input may be visually hidden but still tracked by the form.33  if (usernameEl && username) {34    setNativeValue(usernameEl, username);35  }36  if (passwordEl && isVisible(passwordEl)) {37    setNativeValue(passwordEl, password);38  }39}4041export function fillTOTPCode(42  totpEl: HTMLInputElement | null,43  code: string44): boolean {45  if (!totpEl || !code || !isVisible(totpEl)) return false;46  if (totpEl.value.trim() !== "") return false;47  setNativeValue(totpEl, code);48  return true;49}5051// React tracks input values via a synthetic event system that does not see52// direct `.value =` assignment. We must call the native HTMLInputElement53// value setter (pulled off the prototype) and then dispatch the real input54// and change events so React re-reads the new value. Without this, the55// field reads as empty to the form's controlled-input state.56export function setNativeValue(57  el: HTMLInputElement | HTMLTextAreaElement,58  value: string59): void {60  const proto =61    el instanceof HTMLTextAreaElement62      ? window.HTMLTextAreaElement.prototype63      : window.HTMLInputElement.prototype;64  const nativeValueSetter = Object.getOwnPropertyDescriptor(proto, "value")?.set;65  nativeValueSetter?.call(el, value);66  el.dispatchEvent(new Event("input", { bubbles: true }));67  el.dispatchEvent(new Event("change", { bubbles: true }));68}6970export interface CardFillFields {71  number: HTMLInputElement | null;72  expMonth: HTMLInputElement | null;73  expYear: HTMLInputElement | null;74  expiryCombo: HTMLInputElement | null;75  cvc: HTMLInputElement | null;76  name: HTMLInputElement | null;77}7879export function fillCardForm(fields: CardFillFields, card: { cardNumber: string; expMonth: string; expYear: string; cvc: string; cardholderName: string }): void {80  const raw = card.cardNumber.replace(/\s/g, '');81  if (fields.number) setNativeValue(fields.number, formatCardNumber(raw));82  if (fields.expiryCombo) {83    setNativeValue(fields.expiryCombo, `${card.expMonth}/${card.expYear.slice(-2)}`);84  } else {85    if (fields.expMonth) setNativeValue(fields.expMonth, card.expMonth);86    if (fields.expYear) setNativeValue(fields.expYear, card.expYear);87  }88  if (fields.cvc) setNativeValue(fields.cvc, card.cvc);89  if (fields.name) setNativeValue(fields.name, card.cardholderName);90}9192export function formatCardNumber(raw: string): string {93  if (/^3[47]/.test(raw)) return raw.replace(/(\d{4})(\d{6})(\d{5})/, '$1 $2 $3');94  return raw.replace(/(\d{4})(?=\d)/g, '$1 ').trim();95}9697export function detectCardNetwork(number: string): import('../shared/types').CardNetwork {98  const n = number.replace(/\s/g, '');99  if (/^4/.test(n)) return 'visa';100  if (/^5[1-5]|^2[2-7]/.test(n)) return 'mastercard';101  if (/^3[47]/.test(n)) return 'amex';102  if (/^6(?:011|5)/.test(n)) return 'discover';103  return 'unknown';104}105106export function passesLuhn(number: string): boolean {107  const digits = number.replace(/\s/g, '').split('').map(Number);108  let sum = 0, isEven = false;109  for (let i = digits.length - 1; i >= 0; i--) {110    let d = digits[i]!;111    if (isEven) { d *= 2; if (d > 9) d -= 9; }112    sum += d;113    isEven = !isEven;114  }115  return sum % 10 === 0;116}117118export function extractLast4(number: string): string {119  return number.replace(/\s/g, '').slice(-4);120}