let logging = true; new (class ExtensionPopup { BROWSER_STORAGE_KEY = "transparentZenSettings"; browserStorageSettings = {}; enableStylingSwitch = document.getElementById("enable-styling"); refetchCSSButton = document.getElementById("refetch-css"); websitesList = document.getElementById("websites-list"); currentSiteFeatures = document.getElementById("current-site-toggles"); currentSiteHostname = ""; autoUpdateSwitch = document.getElementById("auto-update"); lastFetchedTime = document.getElementById("last-fetched-time"); constructor() { if (logging) console.log("Initializing ExtensionPopup"); // Load settings and initialize the popup this.loadSettings().then((settings) => { if (settings) { this.browserStorageSettings = settings; this.getCurrentTabInfo().then(() => { this.restoreSettings(); this.bindEvents(); }); } }); // Bind event listeners this.refetchCSSButton.addEventListener("click", this.refetchCSS.bind(this)); this.autoUpdateSwitch.addEventListener( "change", this.saveSettings.bind(this) ); // Setup auto-update and display last fetched time this.setupAutoUpdate(); this.displayLastFetchedTime(); this.displayAddonVersion(); } async getCurrentTabInfo() { if (logging) console.log("getCurrentTabInfo called"); try { const tabs = await browser.tabs.query({ active: true, currentWindow: true, }); if (tabs.length > 0) { const url = new URL(tabs[0].url); this.currentSiteHostname = url.hostname; console.info("Current site hostname:", this.currentSiteHostname); } } catch (error) { console.error("Error getting current tab info:", error); } } bindEvents() { if (logging) console.log("bindEvents called"); // Bind event listeners for settings changes this.enableStylingSwitch.addEventListener("change", () => { this.saveSettings(); this.updateActiveTabStyling(); }); this.currentSiteFeatures.addEventListener("change", (event) => { if (event.target.type === "checkbox") { this.saveSettings(); this.updateActiveTabStyling(); } }); } restoreSettings() { if (logging) console.log("restoreSettings called"); // Restore settings from storage if (this.browserStorageSettings.enableStyling !== undefined) { this.enableStylingSwitch.checked = this.browserStorageSettings.enableStyling; } if (this.browserStorageSettings.autoUpdate !== undefined) { this.autoUpdateSwitch.checked = this.browserStorageSettings.autoUpdate; } this.loadCurrentSiteFeatures(); } async loadSettings() { if (logging) console.log("loadSettings called"); // Load settings from browser storage 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 (logging) console.log("saveSettings called"); // Save settings to browser storage this.browserStorageSettings.enableStyling = this.enableStylingSwitch.checked; this.browserStorageSettings.autoUpdate = this.autoUpdateSwitch.checked; const featureSettings = {}; this.currentSiteFeatures .querySelectorAll("input[type=checkbox]") .forEach((checkbox) => { const [site, feature] = checkbox.name.split("|"); if (!featureSettings[site]) { featureSettings[site] = {}; } featureSettings[site][feature] = checkbox.checked; }); this.browserStorageSettings.featureSettings = featureSettings; browser.storage.local.set({ [this.BROWSER_STORAGE_KEY]: this.browserStorageSettings, }); console.info("Settings saved", this.browserStorageSettings); } async loadCurrentSiteFeatures() { if (logging) console.log("loadCurrentSiteFeatures called"); // Load features for the current site try { const stylesData = await browser.storage.local.get("styles"); const styles = stylesData.styles?.website || {}; this.currentSiteFeatures.innerHTML = ""; const currentSiteKey = Object.keys(styles).find((site) => this.isCurrentSite(site.replace(".css", "")) ); if (!currentSiteKey) { const requestThemeButton = document.createElement("button"); requestThemeButton.className = "action-button primary"; requestThemeButton.innerHTML = `Request Theme for ${this.currentSiteHostname}`; requestThemeButton.addEventListener("click", () => { const issueUrl = `https://github.com/sameerasw/my-internet/issues/new?template=website-theme-request.md&title=[THEME] ${this.currentSiteHostname}&body=Please add a theme for ${this.currentSiteHostname}`; window.open(issueUrl, "_blank"); }); this.currentSiteFeatures.appendChild(requestThemeButton); return; } const features = styles[currentSiteKey]; for (const [feature, css] of Object.entries(features)) { const displayFeatureName = feature.includes("-") ? feature.split("-")[1] : feature; const isChecked = this.browserStorageSettings.featureSettings?.[currentSiteKey]?.[ feature ] ?? true; const featureToggle = document.createElement("div"); featureToggle.className = "feature-toggle"; featureToggle.innerHTML = ` ${displayFeatureName} `; this.currentSiteFeatures.appendChild(featureToggle); } } catch (error) { console.error("Error loading current site features:", error); this.currentSiteFeatures.innerHTML = "