diff options
author | sameerasw <[email protected]> | 2025-02-25 15:20:19 +0530 |
---|---|---|
committer | sameerasw <[email protected]> | 2025-02-25 15:20:19 +0530 |
commit | 36cb8d72865b1ab5cbcb008f939d916dfd1d206d (patch) | |
tree | cbf9efffce76a34b2230826c50e4612aea482d7f /background.js | |
parent | e36e4ce96399db97f80c8833fa6ef2982a249e51 (diff) |
Added the website mapper and automatic loading and fetching of css styles with the ability to disable as well
Diffstat (limited to 'background.js')
-rw-r--r-- | background.js | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/background.js b/background.js index 3c25ff3..b67461e 100644 --- a/background.js +++ b/background.js @@ -1,29 +1,41 @@ -const CSS_URL = - "https://sameerasw.github.io/my-internet/github.com.css"; +const CSS_URL_BASE = "https://sameerasw.github.io/my-internet/"; -async function updateCSS() { +async function updateCSS(url, cssFileName) { try { - let response = await fetch(CSS_URL, { + let response = await fetch(url, { headers: { - 'Cache-Control': 'no-cache' - } + "Cache-Control": "no-cache", + }, }); if (!response.ok) throw new Error("Failed to fetch CSS"); let cssText = await response.text(); - await browser.storage.local.set({ githubCSS: cssText }); - await browser.storage.sync.set({ githubCSS: cssText }); - console.log("Updated GitHub CSS from remote source." + cssText); + await browser.storage.local.set({ [cssFileName]: cssText }); + await browser.storage.sync.set({ [cssFileName]: cssText }); + console.log(`Updated CSS for ${cssFileName} from remote source.`); } catch (error) { - console.error("Error fetching CSS:", error); + console.error(`Error fetching CSS for ${cssFileName}:`, error); } } +async function updateAllCSS(mapping) { + for (const [site, cssFileName] of Object.entries(mapping)) { + const url = `${CSS_URL_BASE}${cssFileName}`; + await updateCSS(url, cssFileName); + } + console.log("All CSS files updated."); +} + // Fetch CSS on startup and then every hour -updateCSS(); +fetch("/mapper.json") + .then((response) => response.json()) + .then((mapping) => updateAllCSS(mapping)); -// Listen for messages to restart the background script browser.runtime.onMessage.addListener((message) => { - if (message.action === "restartBackground") { + if (message.action === "updateCSS") { + fetch("/mapper.json") + .then((response) => response.json()) + .then((mapping) => updateAllCSS(mapping)); + } else if (message.action === "restartBackground") { browser.runtime.reload(); } -});
\ No newline at end of file +}); |