summaryrefslogtreecommitdiff
path: root/inject-css.js
blob: d53a9159625a310955a8728718fb89081e5d4d1e (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { SKIP_FORCE_THEMING_KEY } from "./shared/constants.js";

let logging = false;

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

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

    if (!settings.transparentZenSettings?.enableStyling) {
      if (logging) console.log("Styling is disabled");
      return;
    }

    if (logging) console.log("Styling is enabled");
    const data = await browser.storage.local.get("styles");
    if (logging) console.log("Styles data loaded", data);

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

    // Find the best matching CSS file
    let bestMatch = null;
    let bestMatchLength = 0;

    for (const key of Object.keys(data.styles?.website || {})) {
      const siteName = key.replace(".css", "");
      if (siteName.startsWith("+")) {
        const baseSiteName = siteName.slice(1);
        if (
          currentUrl.endsWith(baseSiteName) &&
          baseSiteName.length > bestMatchLength
        ) {
          bestMatch = key;
          bestMatchLength = baseSiteName.length;
        }
      } else if (currentUrl === siteName || currentUrl === `www.${siteName}`) {
        // Exact match has priority
        bestMatch = key;
        break;
      } else if (
        currentUrl.endsWith(siteName) &&
        siteName.length > bestMatchLength
      ) {
        bestMatch = key;
        bestMatchLength = siteName.length;
      }
    }

    // If a direct match was found, use it
    if (bestMatch) {
      await injectCSS(currentUrl, data.styles.website[bestMatch]);
      return;
    }

    // If no direct match was found and force styling is enabled, check whitelist/blacklist mode
    if (settings.transparentZenSettings?.forceStyling) {
      const skipListData = await browser.storage.local.get(
        SKIP_FORCE_THEMING_KEY
      );
      const siteList = skipListData[SKIP_FORCE_THEMING_KEY] || [];
      const isWhitelistMode =
        settings.transparentZenSettings?.whitelistMode || false;
      const siteInList = siteList.includes(currentUrl);

      // In whitelist mode: apply only if site is in the list
      // In blacklist mode: apply only if site is NOT in the list
      if (
        (isWhitelistMode && siteInList) ||
        (!isWhitelistMode && !siteInList)
      ) {
        await injectCSS(currentUrl, data.styles.website["example.com.css"]);
      } else {
        if (logging)
          console.log(
            `Styling skipped due to ${
              isWhitelistMode ? "whitelist" : "blacklist"
            } mode settings`
          );
      }
    } else {
      if (logging) console.log("No CSS file found for current site");
    }
  } catch (error) {
    console.error("Error injecting CSS:", error);
  }
})();

async function injectCSS(hostname, features) {
  if (!features) return;

  const siteKey = `transparentZenSettings.${hostname}`;
  const siteData = await browser.storage.local.get(siteKey);
  const featureSettings = siteData[siteKey] || {};

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

  if (combinedCSS) {
    const style = document.createElement("style");
    style.textContent = combinedCSS;
    document.head.appendChild(style);
    if (logging) console.log(`Injected custom CSS for ${hostname}`);
  }
}