summaryrefslogtreecommitdiff
path: root/inject-css.js
blob: db19b3d70898968e23da37d415085533b3c399a6 (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
42
43
44
45
46
47
48
49
50
51
52
let logging = true;

if (logging) console.log("inject-css.js script loaded");

browser.storage.local.get("transparentZenSettings").then((settings) => {
  if (logging) console.log("Settings loaded", settings);

  if (settings.transparentZenSettings?.enableStyling) {
    if (logging) console.log("Styling is enabled");

    browser.storage.local.get("styles").then((data) => {
      if (logging) console.log("Styles data loaded", data);

      const currentUrl = window.location.hostname;
      if (logging) console.log("Current URL hostname", currentUrl);

      const cssFileName = Object.keys(data.styles?.website || {}).find(
        (key) => {
          const siteName = key.replace(".css", "");
          return currentUrl === siteName || currentUrl === `www.${siteName}`;
        }
      );

      if (cssFileName) {
        if (logging)
          console.log("CSS file found for current site", cssFileName);

        const features = data.styles.website[cssFileName];
        const featureSettings =
          settings.transparentZenSettings.featureSettings?.[cssFileName] || {};

        let combinedCSS = "";
        for (const [feature, css] of Object.entries(features)) {
          if (featureSettings[feature] !== false) {
            combinedCSS += css + "\n";
          }
        }

        if (combinedCSS) {
          let style = document.createElement("style");
          style.textContent = combinedCSS;
          document.head.appendChild(style);
          if (logging) console.log(`Injected custom CSS for ${currentUrl}`);
        }
      } else {
        if (logging) console.log("No CSS file found for current site");
      }
    });
  } else {
    if (logging) console.log("Styling is disabled");
  }
});