diff options
author | sameerasw <[email protected]> | 2025-02-27 11:04:31 +0530 |
---|---|---|
committer | sameerasw <[email protected]> | 2025-02-27 11:04:31 +0530 |
commit | 4d9e41e4faabe4424636a247815b865b8b6c44d5 (patch) | |
tree | 3c58131f85e72969bd6b83824d69482f586e69bb /background.js | |
parent | e3b7b6b485dd94a4b9308d69944320069d69740e (diff) |
Added auto fetching and last fetched time.
Diffstat (limited to 'background.js')
-rw-r--r-- | background.js | 44 |
1 files changed, 44 insertions, 0 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); |