initContentScript function

Last updated: 2026-03-05T10:53:28.853Z

Metrics

LOC: 105 Complexity: 8 Params: 0

Signature

initContentScript(): : Promise<void>

Architecture violations

View all

  • [warning] max-lines: 'initContentScript' has 105 lines (max 80)

Source Code

async function initContentScript(): Promise<void> {
  await initLogger();
  const settings = await getSettings();
  await initI18n(settings.uiLanguage ?? "auto");

  // Configure the detection pipeline from user settings
  if (settings.detectionPipeline?.length) {
    setActiveClassifiers(
      buildClassifiersFromSettings(settings.detectionPipeline),
    );
  }

  // Init the per-field icon only if enabled
  if (settings.showFieldIcon !== false) {
    initFieldIcon(settings.fieldIconPosition ?? "inside");
  }

  // Load pre-trained model artefacts (generated by `npm run train:model`).
  // Falls back silently to runtime keyword classifier if artefacts are absent.
  loadPretrainedModel().catch(() => {});

  // Auto-start watcher if enabled in settings
  if (settings.watcherEnabled) {
    startWatching(
      (newFieldsCount) => {
        if (newFieldsCount > 0) {
          showNotification(
            `🔄 ${newFieldsCount} novo(s) campo(s) detectado(s) — re-preenchendo...`,
          );
        }
      },
      settings.watcherAutoRefill,
      {
        autoRefill: settings.watcherAutoRefill,
        debounceMs: settings.watcherDebounceMs,
        shadowDOM: settings.watcherShadowDOM,
      },
    );
  }

  // Restore a recording session that was persisted before a traditional form submit
  // (non-AJAX GET/POST) caused a full page navigation.
  const restoredSession = tryRestoreRecordingSession();
  if (restoredSession) {
    // Notify the devtools panel about the restored session (with all steps captured
    // before and during the form submit) so it can repopulate the action list.
    chrome.runtime
      .sendMessage({
        type: "RECORDING_RESTORED",
        payload: {
          steps: restoredSession.steps.map((step) => ({
            type: step.type,
            selector: step.selector,
            value: step.value,
            url: step.url,
            label: step.label,
            assertion: step.assertion,
          })),
        },
      })
      .catch(() => {});

    setOnStepAdded((step, index) => {
      chrome.runtime
        .sendMessage({
          type: "RECORDING_STEP_ADDED",
          payload: {
            step: {
              type: step.type,
              selector: step.selector,
              value: step.value,
              url: step.url,
              label: step.label,
              assertion: step.assertion,
            },
            index,
          },
        })
        .catch(() => {});
    });

    setOnStepUpdated((step, index) => {
      chrome.runtime
        .sendMessage({
          type: "RECORDING_STEP_UPDATED",
          payload: {
            step: {
              type: step.type,
              selector: step.selector,
              value: step.value,
              url: step.url,
              label: step.label,
              assertion: step.assertion,
            },
            index,
          },
        })
        .catch(() => {});
    });

    showNotification(
      `🔴 Gravação retomada (${restoredSession.steps.length} passos)`,
    );
  }
}

Dependencies (Outgoing)

graph LR initContentScript["initContentScript"] showNotification["showNotification"] initContentScript -->|calls| showNotification style initContentScript fill:#dbeafe,stroke:#2563eb,stroke-width:2px click initContentScript "9e3cc66103159cb7.html" click showNotification "8c9f4b84bc123ccf.html"
TargetType
showNotification calls

Impact (Incoming)

graph LR initContentScript["initContentScript"] FillableElement["FillableElement"] FillableElement -->|calls| initContentScript style initContentScript fill:#dbeafe,stroke:#2563eb,stroke-width:2px click initContentScript "9e3cc66103159cb7.html" click FillableElement "2ecf5aaac3f668a8.html"
SourceType
FillableElement calls