summaryrefslogtreecommitdiff
path: root/popup/popup.js
blob: 7824efb4c663b66647fa38b640a5dcda257afb4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
new class ExtensionPopup {
  BROWSER_STORAGE_KEY = "transparentZenSettings";
  browserStorageSettings = {};
  extensionSettingsForm = document.getElementById("extension-settings");

  constructor() {
    this.loadSettings().then((settings) => {
      if (settings) {
        this.browserStorageSettings = settings;
        this.restoreSettings();
        this.bindEvents();
      }
    });
  }

  bindEvents() {
    this.extensionSettingsForm.querySelectorAll("input").forEach((input) => {
      input.addEventListener("change", () => {
        this.saveSettings();
      })
    });
  }

  restoreSettings() {
    if (this.extensionSettingsForm?.elements) {
      for (const element of this.extensionSettingsForm.elements) {
        if (this.browserStorageSettings[element.name]) {
          element.checked = JSON.parse(this.browserStorageSettings[element.name]);
        }
      }
    }
  }

  async loadSettings() {
    const settings = await browser.storage.local.get(this.BROWSER_STORAGE_KEY);
    console.info("Settings loaded", settings?.[this.BROWSER_STORAGE_KEY]);
    return settings?.[this.BROWSER_STORAGE_KEY] || {};
  }

  saveSettings() {
    if (this.extensionSettingsForm?.elements) {
      for (const element of this.extensionSettingsForm.elements) {
        this.browserStorageSettings[element.name] = element.checked;
      }

      browser.storage.local.set({[this.BROWSER_STORAGE_KEY]: this.browserStorageSettings});
      browser.runtime.sendMessage({ action: "updateSettings" });
      console.info("Settings saved", this.browserStorageSettings);
    }
  }
}