diff options
author | sameerasw <[email protected]> | 2025-02-27 23:03:28 +0530 |
---|---|---|
committer | sameerasw <[email protected]> | 2025-02-27 23:03:28 +0530 |
commit | d020d729d338ee71b59130ff1a820363dc0053e7 (patch) | |
tree | 2995d979a0998e0dbc27b7a23bb44a36c0c49000 /inject-css.js | |
parent | 9e8de1c7b8ab58b12924cedfb7284af58f5b8181 (diff) |
FIXED THE DATA PERSISTANCE ISSUE! #2
Diffstat (limited to 'inject-css.js')
-rw-r--r-- | inject-css.js | 91 |
1 files changed, 48 insertions, 43 deletions
diff --git a/inject-css.js b/inject-css.js index db19b3d..7d009cc 100644 --- a/inject-css.js +++ b/inject-css.js @@ -2,51 +2,56 @@ let logging = true; if (logging) console.log("inject-css.js script loaded"); -browser.storage.local.get("transparentZenSettings").then((settings) => { - if (logging) console.log("Settings loaded", settings); +(async () => { + try { + const settings = await browser.storage.local.get("transparentZenSettings"); + if (logging) console.log("Settings loaded", settings); + + if (!settings.transparentZenSettings?.enableStyling) { + if (logging) console.log("Styling is disabled"); + return; + } - if (settings.transparentZenSettings?.enableStyling) { if (logging) console.log("Styling is enabled"); + const data = await browser.storage.local.get("styles"); + if (logging) console.log("Styles data loaded", data); + + const currentUrl = window.location.hostname; + if (logging) console.log("Current URL hostname", currentUrl); + + const cssFileName = Object.keys(data.styles?.website || {}).find( + (key) => { + const siteName = key.replace(".css", ""); + return currentUrl === siteName || currentUrl === `www.${siteName}`; + } + ); + + if (!cssFileName) { + if (logging) console.log("No CSS file found for current site"); + return; + } + + if (logging) console.log("CSS file found for current site", cssFileName); + + const features = data.styles.website[cssFileName]; + const siteKey = `transparentZenSettings.${currentUrl}`; + const siteData = await browser.storage.local.get(siteKey); + const featureSettings = siteData[siteKey] || {}; - browser.storage.local.get("styles").then((data) => { - if (logging) console.log("Styles data loaded", data); - - const currentUrl = window.location.hostname; - if (logging) console.log("Current URL hostname", currentUrl); - - const cssFileName = Object.keys(data.styles?.website || {}).find( - (key) => { - const siteName = key.replace(".css", ""); - return currentUrl === siteName || currentUrl === `www.${siteName}`; - } - ); - - if (cssFileName) { - if (logging) - console.log("CSS file found for current site", cssFileName); - - const features = data.styles.website[cssFileName]; - const featureSettings = - settings.transparentZenSettings.featureSettings?.[cssFileName] || {}; - - let combinedCSS = ""; - for (const [feature, css] of Object.entries(features)) { - if (featureSettings[feature] !== false) { - combinedCSS += css + "\n"; - } - } - - if (combinedCSS) { - let style = document.createElement("style"); - style.textContent = combinedCSS; - document.head.appendChild(style); - if (logging) console.log(`Injected custom CSS for ${currentUrl}`); - } - } else { - if (logging) console.log("No CSS file found for current site"); + let combinedCSS = ""; + for (const [feature, css] of Object.entries(features)) { + if (featureSettings[feature] !== false) { + combinedCSS += css + "\n"; } - }); - } else { - if (logging) console.log("Styling is disabled"); + } + + if (combinedCSS) { + const style = document.createElement("style"); + style.textContent = combinedCSS; + document.head.appendChild(style); + if (logging) console.log(`Injected custom CSS for ${currentUrl}`); + } + } catch (error) { + console.error("Error injecting CSS:", error); } -}); +})(); |