bindSettingsEvents function domain

Last updated: 2026-03-03T18:32:34.140Z

Metrics

LOC: 111 Complexity: 11 Params: 0

Signature

bindSettingsEvents(): : void

Architecture violations

View all

  • [warning] max-cyclomatic-complexity: 'bindSettingsEvents' has cyclomatic complexity 11 (max 10)
  • [warning] max-lines: 'bindSettingsEvents' has 111 lines (max 80)

Source Code

function bindSettingsEvents(): void {
  const debouncedSaveGeneral = debounce(() => {
    void saveGeneralSettings();
  }, 300);
  const debouncedSaveFieldIcon = debounce(() => {
    void saveFieldIconSettings();
  }, 300);

  // General settings — auto-save on any change
  for (const id of [
    "setting-auto-fill",
    "setting-highlight",
    "setting-cache-enabled",
    "setting-show-panel",
    "setting-fill-empty-only",
    "setting-debug-log",
    "setting-log-level",
    "setting-log-max-entries",
    "setting-strategy",
    "setting-locale",
  ]) {
    document
      .getElementById(id)
      ?.addEventListener("change", debouncedSaveGeneral);
  }

  // Field icon — auto-save on any change
  document
    .getElementById("setting-show-field-icon")
    ?.addEventListener("change", debouncedSaveFieldIcon);
  document
    .getElementById("setting-field-icon-position")
    ?.addEventListener("change", debouncedSaveFieldIcon);

  // Watcher — auto-save on any change
  const debouncedSaveWatcher = debounce(() => {
    void saveWatcherSettings();
  }, 300);
  for (const id of [
    "setting-watcher-enabled",
    "setting-watcher-auto-refill",
    "setting-watcher-shadow-dom",
    "setting-watcher-debounce",
  ]) {
    const el = document.getElementById(id);
    el?.addEventListener("change", debouncedSaveWatcher);
    if (el?.tagName === "INPUT" && (el as HTMLInputElement).type === "number") {
      el.addEventListener("input", debouncedSaveWatcher);
    }
  }

  // AI feedback — auto-save on any change
  const debouncedSaveAi = debounce(() => {
    void saveAiSettings();
  }, 300);
  for (const id of [
    "setting-show-fill-toast",
    "setting-show-ai-badge",
    "setting-ai-timeout",
  ]) {
    const el = document.getElementById(id);
    el?.addEventListener("change", debouncedSaveAi);
    if (el?.tagName === "INPUT" && (el as HTMLInputElement).type === "number") {
      el.addEventListener("input", debouncedSaveAi);
    }
  }

  // UI language — dedicated handler that re-localises the page
  document
    .getElementById("setting-ui-language")
    ?.addEventListener("change", async (e) => {
      const lang = (e.target as HTMLSelectElement)
        .value as Settings["uiLanguage"];
      await chrome.runtime.sendMessage({
        type: "SAVE_SETTINGS",
        payload: { uiLanguage: lang } as Partial<Settings>,
      });
      await initI18n(lang);
      localizeHTML();
      void loadSettings(); // re-render strategy list and other dynamic content
      showToast(t("uiLanguageChanged"));
    });

  document
    .getElementById("btn-download-chrome-ai")
    ?.addEventListener("click", async () => {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      const LanguageModel = (globalThis as any).LanguageModel as
        | {
            create?: (opts?: {
              expectedOutputs?: { type: string; languages?: string[] }[];
            }) => Promise<unknown>;
          }
        | undefined;
      if (!LanguageModel?.create) return;
      try {
        await LanguageModel.create({
          expectedOutputs: [{ type: "text", languages: ["en"] }],
        });
        void checkChromeAiStatus();
        showToast(t("chromeAiDownloadStart"));
      } catch (err) {
        showToast(
          t("chromeAiDownloadError", [
            err instanceof Error ? err.message : String(err),
          ]),
          "error",
        );
      }
    });
}

Dependencies (Outgoing)

graph LR bindSettingsEvents["bindSettingsEvents"] debounce["debounce"] saveGeneralSettings["saveGeneralSettings"] saveFieldIconSettings["saveFieldIconSettings"] saveWatcherSettings["saveWatcherSettings"] saveAiSettings["saveAiSettings"] loadSettings["loadSettings"] checkChromeAiStatus["checkChromeAiStatus"] bindSettingsEvents -->|calls| debounce bindSettingsEvents -->|calls| saveGeneralSettings bindSettingsEvents -->|calls| saveFieldIconSettings bindSettingsEvents -->|calls| saveWatcherSettings bindSettingsEvents -->|calls| saveAiSettings bindSettingsEvents -->|calls| loadSettings bindSettingsEvents -->|calls| checkChromeAiStatus style bindSettingsEvents fill:#dbeafe,stroke:#2563eb,stroke-width:2px click bindSettingsEvents "29719c6741ea9340.html" click debounce "8b7d4e236272ffb6.html" click saveGeneralSettings "544dc3be82c02f65.html" click saveFieldIconSettings "c498ea644ec70b0b.html" click saveWatcherSettings "2ae10f14691640a6.html" click saveAiSettings "b59249272b7a5fcc.html" click loadSettings "6569479f3c7ea49f.html" click checkChromeAiStatus "f0bcc646e70f0282.html"
TargetType
debounce calls
saveGeneralSettings calls
saveFieldIconSettings calls
saveWatcherSettings calls
saveAiSettings calls
loadSettings calls
checkChromeAiStatus calls
change dynamic_call
input dynamic_call
click dynamic_call

Impact (Incoming)

graph LR bindSettingsEvents["bindSettingsEvents"] initSettingsTab["initSettingsTab"] initSettingsTab -->|calls| bindSettingsEvents style bindSettingsEvents fill:#dbeafe,stroke:#2563eb,stroke-width:2px click bindSettingsEvents "29719c6741ea9340.html" click initSettingsTab "340c0f2f1d3314fd.html"
SourceType
initSettingsTab calls