diff options
author | sameerasw <[email protected]> | 2025-02-25 15:46:34 +0530 |
---|---|---|
committer | sameerasw <[email protected]> | 2025-02-25 15:46:34 +0530 |
commit | 3e8b7b3cfc273ebe348b4c40d869a456db895c7c (patch) | |
tree | c53af64bb4ed9c895922c31d61a5743b2d16e288 | |
parent | 36cb8d72865b1ab5cbcb008f939d916dfd1d206d (diff) |
no background scripts
-rw-r--r-- | background.js | 41 | ||||
-rw-r--r-- | manifest.json | 4 | ||||
-rw-r--r-- | popup/popup.js | 18 |
3 files changed, 12 insertions, 51 deletions
diff --git a/background.js b/background.js deleted file mode 100644 index b67461e..0000000 --- a/background.js +++ /dev/null @@ -1,41 +0,0 @@ -const CSS_URL_BASE = "https://sameerasw.github.io/my-internet/"; - -async function updateCSS(url, cssFileName) { - try { - let response = await fetch(url, { - headers: { - "Cache-Control": "no-cache", - }, - }); - if (!response.ok) throw new Error("Failed to fetch CSS"); - let cssText = await response.text(); - 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 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 -fetch("/mapper.json") - .then((response) => response.json()) - .then((mapping) => updateAllCSS(mapping)); - -browser.runtime.onMessage.addListener((message) => { - if (message.action === "updateCSS") { - fetch("/mapper.json") - .then((response) => response.json()) - .then((mapping) => updateAllCSS(mapping)); - } else if (message.action === "restartBackground") { - browser.runtime.reload(); - } -}); diff --git a/manifest.json b/manifest.json index e4de56c..d3172ce 100644 --- a/manifest.json +++ b/manifest.json @@ -17,10 +17,6 @@ "default_popup": "popup/popup.html", "default_title": "Zen Internet" }, - "background": { - "scripts": ["background.js"], - "persistent": false - }, "content_scripts": [ { "matches": ["<all_urls>"], diff --git a/popup/popup.js b/popup/popup.js index 494f170..6a28e31 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -2,6 +2,7 @@ new (class ExtensionPopup { BROWSER_STORAGE_KEY = "transparentZenSettings"; browserStorageSettings = {}; enableStylingSwitch = document.getElementById("enable-styling"); + refetchCSSButton = document.getElementById("refetch-css"); constructor() { this.loadSettings().then((settings) => { @@ -11,9 +12,7 @@ new (class ExtensionPopup { this.bindEvents(); } }); - document - .getElementById("refetch-css") - .addEventListener("click", this.refetchCSS.bind(this)); + this.refetchCSSButton.addEventListener("click", this.refetchCSS.bind(this)); document .getElementById("restart-background") .addEventListener("click", this.restartBackground); @@ -48,11 +47,11 @@ new (class ExtensionPopup { browser.storage.sync.set({ [this.BROWSER_STORAGE_KEY]: this.browserStorageSettings, }); - browser.runtime.sendMessage({ action: "updateSettings" }); console.info("Settings saved", this.browserStorageSettings); } async refetchCSS() { + this.refetchCSSButton.textContent = "Fetching..."; try { const response = await fetch("/mapper.json", { headers: { @@ -75,15 +74,22 @@ new (class ExtensionPopup { await browser.storage.local.set({ [cssFileName]: cssText }); await browser.storage.sync.set({ [cssFileName]: cssText }); } - browser.runtime.sendMessage({ action: "updateCSS" }); + this.refetchCSSButton.textContent = "Done!"; + setTimeout(() => { + this.refetchCSSButton.textContent = "Refetch latest styles"; + }, 2000); console.info("All CSS files refetched and updated from GitHub."); } catch (error) { + this.refetchCSSButton.textContent = "Error!"; + setTimeout(() => { + this.refetchCSSButton.textContent = "Refetch latest styles"; + }, 2000); console.error("Error refetching CSS:", error); } } async restartBackground() { - browser.runtime.sendMessage({ action: "restartBackground" }); + browser.runtime.reload(); console.info("Background script restart requested."); } })(); |