interaction.ts

1.2 KB · raw

1import type { DetectedField } from "./detect";2import { isAnyCredentialForm } from "./detect";34export interface AutofillInteractionTargets {5  usernameEl: HTMLInputElement | null;6  passwordEl: HTMLInputElement | null;7  shouldOpen: boolean;8}910/**11 * Explicit icon clicks should still work when passive heuristics are too12 * cautious. Prefer the real detected fields when present, but allow the click13 * path to open Peach even if the current page only exposes a partial form.14 */15export function resolveAutofillInteractionTargets(16  anchorField: HTMLInputElement,17  fields: DetectedField[],18  explicitSummon = false19): AutofillInteractionTargets {20  const userField = fields.find((f) => f.type === "username" || f.type === "email");21  const pwField = fields.find((f) => f.type === "password");22  const anchorType = anchorField.type.toLowerCase();23  const fallbackType = anchorType === "password" ? "password" : "username";2425  return {26    usernameEl: userField?.el ?? (explicitSummon && fallbackType === "username" ? anchorField : null),27    passwordEl: pwField?.el ?? (explicitSummon && fallbackType === "password" ? anchorField : null),28    shouldOpen: explicitSummon || isAnyCredentialForm(fields),29  };30}