new(class ExtensionPopup { BROWSER_STORAGE_KEY = "transparentZenSettings"; browserStorageSettings = {}; enableStylingSwitch = document.getElementById("enable-styling"); refetchCSSButton = document.getElementById("refetch-css"); websitesList = document.getElementById("websites-list"); currentSiteHostname = ""; constructor() { this.loadSettings().then((settings) => { if (settings) { this.browserStorageSettings = settings; this.getCurrentTabInfo().then(() => { this.restoreSettings(); this.bindEvents(); }); } }); this.refetchCSSButton.addEventListener("click", this.refetchCSS.bind(this)); this.setupContentScriptInjection(); } async getCurrentTabInfo() { 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() { this.enableStylingSwitch.addEventListener("change", () => { this.saveSettings(); this.updateActiveTabStyling(); }); this.websitesList.addEventListener("change", (event) => { this.saveSettings(); // Update styling immediately when a checkbox changes if (event.target.type === 'checkbox') { this.updateActiveTabStyling(); } }); } restoreSettings() { if (this.browserStorageSettings.enableStyling !== undefined) { this.enableStylingSwitch.checked = this.browserStorageSettings.enableStyling; } this.loadWebsitesList(); } 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() { this.browserStorageSettings.enableStyling = this.enableStylingSwitch.checked; const websiteSettings = {}; this.websitesList .querySelectorAll("input[type=checkbox]") .forEach((checkbox) => { websiteSettings[checkbox.name] = checkbox.checked; }); this.browserStorageSettings.websiteSettings = websiteSettings; browser.storage.local.set({ [this.BROWSER_STORAGE_KEY]: this.browserStorageSettings, }); console.info("Settings saved", this.browserStorageSettings); } async loadWebsitesList() { try { // Get the styles from storage that were fetched using refetchCSS const stylesData = await browser.storage.local.get("styles"); const styles = stylesData.styles || {}; this.websitesList.innerHTML = ""; // Use the keys from styles object const websites = Object.keys(styles); if (websites.length === 0) { const listItem = document.createElement("li"); listItem.textContent = "No styles available. Click 'Refetch latest styles' to update."; this.websitesList.appendChild(listItem); return; } // Create array to hold all website items const websiteItems = []; let currentSiteItem = null; for (const site of websites) { // Remove the .css extension if present const displayName = site.replace(/\.css$/, ""); const isChecked = this.browserStorageSettings.websiteSettings?.[displayName] ?? true; const listItem = document.createElement("li"); // Check if this site matches the current site const isCurrent = this.isCurrentSite(displayName); if (isCurrent) { listItem.classList.add("current-site"); currentSiteItem = listItem; // Store the current site item separately } listItem.innerHTML = ` `; // Add to array if not current site if (!isCurrent) { websiteItems.push(listItem); } } // Add current site at the top if it exists if (currentSiteItem) { this.websitesList.appendChild(currentSiteItem); } // Add all other sites websiteItems.forEach(item => { this.websitesList.appendChild(item); }); } catch (error) { console.error("Error loading websites list:", error); this.websitesList.innerHTML = "