diff options
Diffstat (limited to 'popup/popup.js')
-rw-r--r-- | popup/popup.js | 76 |
1 files changed, 39 insertions, 37 deletions
diff --git a/popup/popup.js b/popup/popup.js index 77c5391..494f170 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -1,7 +1,7 @@ new (class ExtensionPopup { BROWSER_STORAGE_KEY = "transparentZenSettings"; browserStorageSettings = {}; - extensionSettingsForm = document.getElementById("extension-settings"); + enableStylingSwitch = document.getElementById("enable-styling"); constructor() { this.loadSettings().then((settings) => { @@ -13,29 +13,22 @@ new (class ExtensionPopup { }); document .getElementById("refetch-css") - .addEventListener("click", this.refetchCSS); + .addEventListener("click", this.refetchCSS.bind(this)); document .getElementById("restart-background") .addEventListener("click", this.restartBackground); } bindEvents() { - this.extensionSettingsForm.querySelectorAll("input").forEach((input) => { - input.addEventListener("change", () => { - this.saveSettings(); - }); + this.enableStylingSwitch.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] - ); - } - } + if (this.browserStorageSettings.enableStyling !== undefined) { + this.enableStylingSwitch.checked = + this.browserStorageSettings.enableStyling; } } @@ -46,35 +39,44 @@ new (class ExtensionPopup { } saveSettings() { - if (this.extensionSettingsForm?.elements) { - for (const element of this.extensionSettingsForm.elements) { - this.browserStorageSettings[element.name] = element.checked; - } + this.browserStorageSettings.enableStyling = + this.enableStylingSwitch.checked; - browser.storage.local.set({ - [this.BROWSER_STORAGE_KEY]: this.browserStorageSettings, - }); - browser.runtime.sendMessage({ action: "updateSettings" }); - console.info("Settings saved", this.browserStorageSettings); - } + browser.storage.local.set({ + [this.BROWSER_STORAGE_KEY]: this.browserStorageSettings, + }); + browser.storage.sync.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' + const response = await fetch("/mapper.json", { + headers: { + "Cache-Control": "no-cache", + }, + }); + if (!response.ok) throw new Error("Failed to fetch mapper.json"); + const mapping = await response.json(); + for (const [site, cssFileName] of Object.entries(mapping)) { + const cssResponse = await fetch( + `https://sameerasw.github.io/my-internet/${cssFileName}`, + { + 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 }); + ); + if (!cssResponse.ok) throw new Error(`Failed to fetch CSS for ${site}`); + const cssText = await cssResponse.text(); + await browser.storage.local.set({ [cssFileName]: cssText }); + await browser.storage.sync.set({ [cssFileName]: cssText }); + } browser.runtime.sendMessage({ action: "updateCSS" }); - console.info("CSS refetched and updated from GitHub." + cssText); + console.info("All CSS files refetched and updated from GitHub."); } catch (error) { console.error("Error refetching CSS:", error); } @@ -84,4 +86,4 @@ new (class ExtensionPopup { browser.runtime.sendMessage({ action: "restartBackground" }); console.info("Background script restart requested."); } -})();
\ No newline at end of file +})(); |