From 4d9e41e4faabe4424636a247815b865b8b6c44d5 Mon Sep 17 00:00:00 2001 From: sameerasw Date: Thu, 27 Feb 2025 11:04:31 +0530 Subject: Added auto fetching and last fetched time. --- background.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ manifest.json | 2 +- popup/popup.css | 2 +- popup/popup.html | 11 +++++++++++ popup/popup.js | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 2 deletions(-) diff --git a/background.js b/background.js index 682441c..77f78f9 100644 --- a/background.js +++ b/background.js @@ -41,6 +41,50 @@ function applyCSSToTab(tab) { }); } +let autoUpdateInterval; + +function startAutoUpdate() { + if (autoUpdateInterval) clearInterval(autoUpdateInterval); + autoUpdateInterval = setInterval(refetchCSS, 2 * 60 * 60 * 1000); +} + +function stopAutoUpdate() { + if (autoUpdateInterval) clearInterval(autoUpdateInterval); +} + +async function refetchCSS() { + try { + const response = await fetch( + "https://sameerasw.github.io/my-internet/styles.json", + { + headers: { "Cache-Control": "no-cache" }, + } + ); + if (!response.ok) throw new Error("Failed to fetch styles.json"); + const styles = await response.json(); + await browser.storage.local.set({ styles }); + await browser.storage.local.set({ lastFetchedTime: Date.now() }); + console.info("All styles refetched and updated from GitHub."); + } catch (error) { + console.error("Error refetching styles:", error); + } +} + +browser.runtime.onMessage.addListener((message) => { + if (message.action === "enableAutoUpdate") { + startAutoUpdate(); + } else if (message.action === "disableAutoUpdate") { + stopAutoUpdate(); + } +}); + +// Initialize auto-update based on stored settings +browser.storage.local.get("transparentZenSettings").then((settings) => { + if (settings.transparentZenSettings?.autoUpdate) { + startAutoUpdate(); + } +}); + browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { if (changeInfo.status === "complete") { applyCSSToTab(tab); diff --git a/manifest.json b/manifest.json index 4fed51a..733eaa6 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Zen Internet", - "version": "1.1.3", + "version": "1.2.0", "description": "Inject custom css from my repository in real time", "browser_specific_settings": { "gecko": { diff --git a/popup/popup.css b/popup/popup.css index 113713e..9b49600 100644 --- a/popup/popup.css +++ b/popup/popup.css @@ -404,6 +404,6 @@ input:checked + .slider:before { margin-right: 4px; } -#addon-version{ +#addon-version, #last-fetched-time{ font-size: 0.75em; } \ No newline at end of file diff --git a/popup/popup.html b/popup/popup.html index cca3def..b53ed87 100644 --- a/popup/popup.html +++ b/popup/popup.html @@ -49,7 +49,18 @@ Refetch latest styles + +
+ + Auto Update Styles +
+
+ +