detectAllFieldsAsync function exported βœ“ 100.0%

Last updated: 2026-03-04T23:21:38.397Z

Metrics

LOC: 80 Complexity: 18 Params: 0 Coverage: 100.0% (35/35 lines, 0x executed)

Signature

detectAllFieldsAsync(): : Promise<DetectionResult>

Summary

Async detection β€” runs the full DEFAULT_COLLECTION_PIPELINE and adds per-detector summary logging.

Architecture violations

View all

  • [warning] max-cyclomatic-complexity: 'detectAllFieldsAsync' has cyclomatic complexity 18 (max 10)

Source Code

export async function detectAllFieldsAsync(): Promise<DetectionResult> {
  const url = window.location.href;
  const t0 = performance.now();

  log.groupCollapsed(`πŸš€ DetecΓ§Γ£o iniciada β€” ${new URL(url).hostname}`);
  log.debug(`πŸ“„ URL: ${url}`);

  // Use the async pipeline so the Chrome AI classifier (detectAsync) is active
  // for native inputs. Custom selects also run the full async pipeline so TF.js
  // and Chrome AI can classify fields the adapter left as "unknown".
  const nativeFields = await detectNativeFieldsAsync();
  const customFields = await classifyCustomFieldsAsync(
    detectCustomComponents(),
  );
  const fields = deduplicateFields(nativeFields, customFields);

  const byMethod: Record<DetectionMethod, number> = {
    "html-type": 0,
    keyword: 0,
    tensorflow: 0,
    "chrome-ai": 0,
    "html-fallback": 0,
    "custom-select": 0,
    interactive: 0,
    "user-override": 0,
  };

  fields.forEach((field, idx) => {
    log.debug(`πŸ” Campo #${idx + 1} detectado:`, field);
    const method = field.detectionMethod ?? "html-fallback";
    byMethod[method as DetectionMethod]++;

    const tag = field.element.tagName.toLowerCase();
    const htmlType =
      field.element instanceof HTMLInputElement ? field.element.type : "β€”";

    log.groupCollapsed(
      `#${idx + 1} <${tag} type="${htmlType}"> β”‚ id="${field.id ?? ""}" name="${field.name ?? ""}"`,
    );
    log.debug(field);
    log.debug(`πŸ“Œ Label: "${field.label ?? "(nenhum)"}"`);
    log.debug(`πŸ“‘ Sinais: "${field.contextSignals || "(nenhum)"}"`);
    const fieldMs = field.detectionDurationMs ?? 0;
    const fieldMsStr =
      fieldMs >= 1
        ? `${fieldMs.toFixed(1)}ms`
        : `${(fieldMs * 1000).toFixed(0)}Β΅s`;
    log.debug(
      `βœ… Tipo final: "${field.fieldType}" [${method} | ${((field.detectionConfidence ?? 0) * 100).toFixed(0)}%] ⚑ ${fieldMsStr}`,
    );
    log.groupEnd();
  });

  const summary = (Object.entries(byMethod) as [DetectionMethod, number][])
    .filter(([, n]) => n > 0)
    .map(([m, n]) => `${m}: ${n}`)
    .join(" Β· ");

  log.info(`βœ… ${fields.length} campo(s)  Β·  ${summary}`);

  // ── Performance summary ────────────────────────────────────────────────────
  const totalMs = performance.now() - t0;
  const perfSorted = [...fields]
    .filter((f) => (f.detectionDurationMs ?? 0) > 0)
    .sort(
      (a, b) => (b.detectionDurationMs ?? 0) - (a.detectionDurationMs ?? 0),
    );
  const slowTop = perfSorted.slice(0, 3).map((f) => {
    const fIdx = fields.indexOf(f) + 1;
    const ms = (f.detectionDurationMs ?? 0).toFixed(1);
    const label = f.label ?? f.id ?? f.name ?? "?";
    return `#${fIdx} "${label}" ${ms}ms [${f.detectionMethod}]`;
  });
  log.debug(
    `⏱ ${totalMs.toFixed(0)}ms total${slowTop.length ? ` · 🐒 ${slowTop.join(" · ")}` : ""}`,
  );
  log.groupEnd();

  return { fields };
}

Dependencies (Outgoing)

graph LR detectAllFieldsAsync["detectAllFieldsAsync"] detectNativeFieldsAsync["detectNativeFieldsAsync"] classifyCustomFieldsAsync["classifyCustomFieldsAsync"] detectCustomComponents["detectCustomComponents"] deduplicateFields["deduplicateFields"] detectAllFieldsAsync -->|calls| detectNativeFieldsAsync detectAllFieldsAsync -->|calls| classifyCustomFieldsAsync detectAllFieldsAsync -->|calls| detectCustomComponents detectAllFieldsAsync -->|calls| deduplicateFields style detectAllFieldsAsync fill:#dbeafe,stroke:#2563eb,stroke-width:2px click detectAllFieldsAsync "1b422b3353cdbe22.html" click detectNativeFieldsAsync "5423f52b3a4d371e.html" click classifyCustomFieldsAsync "d6918a7c3b58c267.html" click detectCustomComponents "e93e9888463f9367.html" click deduplicateFields "10d773974adfc3cb.html"

Impact (Incoming)

graph LR detectAllFieldsAsync["detectAllFieldsAsync"] FillableElement["FillableElement"] DomWatcherCallback["DomWatcherCallback"] refillNewFields["refillNewFields"] setNativeValue["setNativeValue"] fillContextualAI["fillContextualAI"] captureFormValues["captureFormValues"] applyTemplate["applyTemplate"] makeField["makeField"] FillableElement -->|uses| detectAllFieldsAsync DomWatcherCallback -->|uses| detectAllFieldsAsync refillNewFields -->|calls| detectAllFieldsAsync setNativeValue -->|uses| detectAllFieldsAsync fillContextualAI -->|calls| detectAllFieldsAsync captureFormValues -->|calls| detectAllFieldsAsync applyTemplate -->|calls| detectAllFieldsAsync makeField -->|uses| detectAllFieldsAsync style detectAllFieldsAsync fill:#dbeafe,stroke:#2563eb,stroke-width:2px click detectAllFieldsAsync "1b422b3353cdbe22.html" click FillableElement "2ecf5aaac3f668a8.html" click DomWatcherCallback "3bef99a9ca0785db.html" click refillNewFields "07b7f93b13ced4af.html" click setNativeValue "334bd99609d7c37c.html" click fillContextualAI "854e1a4562eb49e4.html" click captureFormValues "6a3156502a77636f.html" click applyTemplate "2ec007fc3b6a3513.html" click makeField "a5977875ae7cda10.html"