diff options
-rw-r--r-- | background.js | 50 | ||||
-rw-r--r-- | manifest.json | 14 |
2 files changed, 62 insertions, 2 deletions
diff --git a/background.js b/background.js new file mode 100644 index 0000000..357e5a0 --- /dev/null +++ b/background.js @@ -0,0 +1,50 @@ +function applyCSSToTab(tab) { + const url = new URL(tab.url); + const hostname = url.hostname; + + browser.storage.local.get("transparentZenSettings").then((settings) => { + if (settings.transparentZenSettings?.enableStyling) { + browser.storage.local.get("styles").then((data) => { + const cssFileName = Object.keys(data.styles?.website || {}).find( + (key) => hostname.includes(key.replace(".css", "")) + ); + + if (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) { + browser.tabs + .insertCSS(tab.id, { code: combinedCSS }) + .then(() => { + console.log(`Injected custom CSS for ${hostname}`); + }) + .catch((error) => { + console.error(`Error applying CSS to ${hostname}:`, error); + }); + } + } + }); + } + }); +} + +browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { + if (changeInfo.status === "complete") { + applyCSSToTab(tab); + } +}); + +browser.tabs.onActivated.addListener(async (activeInfo) => { + const tab = await browser.tabs.get(activeInfo.tabId); + applyCSSToTab(tab); +}); diff --git a/manifest.json b/manifest.json index 9517613..5e3ca93 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Zen Internet", - "version": "1.0.1", + "version": "1.1.0", "description": "Inject custom css from my repository in real time", "browser_specific_settings": { "gecko": { @@ -12,11 +12,21 @@ "48": "assets/images/logo_48.png", "96": "assets/images/logo_96.png" }, - "permissions": ["activeTab", "storage", "tabs", "<all_urls>"], + "permissions": [ + "activeTab", + "storage", + "tabs", + "<all_urls>", + "webNavigation" + ], "browser_action": { "default_popup": "popup/popup.html", "default_title": "Zen Internet" }, + "background": { + "scripts": ["background.js"], + "persistent": false + }, "content_scripts": [ { "matches": ["<all_urls>"], |