doFillAllFields function

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

Metrics

LOC: 108 Complexity: 18 Params: 1

Signature

doFillAllFields(options?: { fillEmptyOnly?: boolean; }): : Promise<GenerationResult[]>

Architecture violations

View all

  • [warning] max-cyclomatic-complexity: 'doFillAllFields' has cyclomatic complexity 18 (max 10)
  • [warning] max-lines: 'doFillAllFields' has 108 lines (max 80)

Source Code

async function doFillAllFields(options?: {
  fillEmptyOnly?: boolean;
}): Promise<GenerationResult[]> {
  const url = window.location.href;
  const settings = await getSettings();
  const fillEmptyOnly = options?.fillEmptyOnly ?? settings.fillEmptyOnly;
  const results: GenerationResult[] = [];

  // Determine AI function based on settings
  const aiGenerateFn = await getAiFunction(settings);

  // Load ignored fields for current URL
  const ignoredFields = await getIgnoredFieldsForUrl(url);
  const ignoredSelectors = new Set(ignoredFields.map((f) => f.selector));

  // Create progress notification
  const progress = createProgressNotification();
  progress.show();

  let totalFields = 0;

  // Stream detection + fill progressively (field by field)
  for await (const field of streamAllFields()) {
    totalFields++;

    // Show detecting state
    progress.addDetecting(field);

    // Detection already done by the stream — update badge
    progress.updateDetected(field);

    // Skip ignored fields
    if (ignoredSelectors.has(field.selector)) continue;

    // Skip fields that already have a value when fillEmptyOnly is enabled
    if (fillEmptyOnly && fieldHasValue(field)) continue;

    const fieldLabel =
      field.label ??
      field.name ??
      field.id ??
      field.fieldType ??
      field.selector;
    log.info(`⏳ Preenchendo [${field.fieldType}] "${fieldLabel}"...`);
    const start = Date.now();

    // Show filling state
    progress.addFilling(field);

    try {
      const result = await resolveFieldValue(
        field,
        url,
        aiGenerateFn,
        settings.forceAIFirst,
        settings.aiTimeoutMs,
      );

      await applyValueToField(field, result.value);

      logAuditFill({
        selector: field.selector,
        fieldType: field.fieldType,
        source: result.source,
        value: String(result.value),
      });

      log.info(
        `✅ Preenchido em ${Date.now() - start}ms via ${result.source}: "${String(result.value).slice(0, 40)}"`,
      );

      if (settings.highlightFilled) {
        highlightField(
          field.element,
          field.label ?? field.fieldType ?? undefined,
        );
      }

      if (settings.showAiBadge && result.source === "ai") {
        showAiFieldBadge(field.element);
      }

      // Update progress — filled
      progress.updateFilled(field, result);

      results.push(result);
    } catch (error) {
      log.warn(
        `❌ Falhou em ${Date.now() - start}ms — campo ${field.selector}:`,
        error,
      );
      progress.updateError(
        field,
        error instanceof Error ? error.message : "falhou",
      );
    }
  }

  // Show summary
  progress.done(results.length, totalFields);

  const aiFilledCount = results.filter((r) => r.source === "ai").length;
  if (settings.showFillToast !== false) {
    showFillToast(results.length, totalFields, aiFilledCount);
  }

  return results;
}

Dependencies (Outgoing)

graph LR doFillAllFields["doFillAllFields"] getAiFunction["getAiFunction"] createProgressNotification["createProgressNotification"] fieldHasValue["fieldHasValue"] applyValueToField["applyValueToField"] highlightField["highlightField"] showAiFieldBadge["showAiFieldBadge"] showFillToast["showFillToast"] doFillAllFields -->|calls| getAiFunction doFillAllFields -->|calls| createProgressNotification doFillAllFields -->|calls| fieldHasValue doFillAllFields -->|calls| applyValueToField doFillAllFields -->|calls| highlightField doFillAllFields -->|calls| showAiFieldBadge doFillAllFields -->|calls| showFillToast style doFillAllFields fill:#dbeafe,stroke:#2563eb,stroke-width:2px click doFillAllFields "23fe5c1a0125e335.html" click getAiFunction "da66edfde6d3db9f.html" click createProgressNotification "2f3f7f6d24a2f425.html" click fieldHasValue "5a7659a4d189d652.html" click applyValueToField "59a962012828c5cb.html" click highlightField "d079b946779ebfc9.html" click showAiFieldBadge "91a8abe473f9bccf.html" click showFillToast "f69df9743d44144e.html"

Impact (Incoming)

graph LR doFillAllFields["doFillAllFields"] fillAllFields["fillAllFields"] fillAllFields -->|calls| doFillAllFields style doFillAllFields fill:#dbeafe,stroke:#2563eb,stroke-width:2px click doFillAllFields "23fe5c1a0125e335.html" click fillAllFields "12dbe3fb8e692059.html"
SourceType
fillAllFields calls