summaryrefslogtreecommitdiff
path: root/background.js
blob: b67461e81802118e4885568196e09d2a7b3fc140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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();
  }
});