summaryrefslogtreecommitdiff
path: root/popup/popup.js
blob: 77c5391efef49f1e96378f045685429d6eb7788b (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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();
      }
    });
    document
      .getElementById("refetch-css")
      .addEventListener("click", this.refetchCSS);
    document
      .getElementById("restart-background")
      .addEventListener("click", this.restartBackground);
  }

  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);
    }
  }

  async refetchCSS() {
    try {
      const response = await fetch(
        "https://sameerasw.github.io/my-internet/github.com.css",
        {
          headers: {
            'Cache-Control': 'no-cache'
          }
        }
      );
      if (!response.ok) throw new Error("Failed to fetch CSS");
      const cssText = await response.text();
      await browser.storage.local.set({ githubCSS: cssText });
      await browser.storage.sync.set({ githubCSS: cssText });
      browser.runtime.sendMessage({ action: "updateCSS" });
      console.info("CSS refetched and updated from GitHub." + cssText);
    } catch (error) {
      console.error("Error refetching CSS:", error);
    }
  }

  async restartBackground() {
    browser.runtime.sendMessage({ action: "restartBackground" });
    console.info("Background script restart requested.");
  }
})();