MCPcopy
hub / github.com/darkreader/darkreader / createStaticStylesheet

Function createStaticStylesheet

src/generators/static-theme.ts:62–102  ·  view source on GitHub ↗
(config: Theme, url: string, isTopFrame: boolean, staticThemes: string, staticThemesIndex: SiteFixesIndex)

Source from the content-addressed store, hash-verified

60}
61
62export default function createStaticStylesheet(config: Theme, url: string, isTopFrame: boolean, staticThemes: string, staticThemesIndex: SiteFixesIndex): string {
63 const srcTheme = config.mode === 1 ? darkTheme : lightTheme;
64 const theme = Object.entries(srcTheme).reduce((t, [prop, color]) => {
65 const [r, g, b, a] = color;
66 t[prop] = applyColorMatrix([r, g, b], createFilterMatrix({...config, mode: 0}));
67 if (a !== undefined) {
68 t[prop].push(a);
69 }
70 return t;
71 }, {} as ThemeColors);
72
73 const themes = getSitesFixesFor(url, staticThemes, staticThemesIndex, parseStaticThemes);
74
75 const commonTheme = themes.find((t) => t.url[0] === '*');
76 const siteTheme = themes.find((t) => t.url[0] !== '*');
77
78 if (!commonTheme) {
79 return '';
80 }
81
82 const lines: string[] = [];
83
84 if (!siteTheme || !siteTheme.noCommon) {
85 lines.push('/* Common theme */');
86 lines.push(...ruleGenerators.map((gen) => gen(commonTheme, theme)!));
87 }
88
89 if (siteTheme) {
90 lines.push(`/* Theme for ${siteTheme.url.join(' ')} */`);
91 lines.push(...ruleGenerators.map((gen) => gen(siteTheme, theme)!));
92 }
93
94 if (config.useFont || config.textStroke > 0) {
95 lines.push('/* Font */');
96 lines.push(createTextStyle(config));
97 }
98
99 return lines
100 .filter((ln) => ln)
101 .join('\n');
102}
103
104function createRuleGen(getSelectors: (siteTheme: StaticTheme) => string[] | undefined, generateDeclarations: (theme: ThemeColors) => string[], modifySelector: ((s: string) => string) = (s) => s) {
105 return (siteTheme: StaticTheme, themeColors: ThemeColors) => {

Callers 1

ExtensionClass · 0.85

Calls 4

applyColorMatrixFunction · 0.90
createFilterMatrixFunction · 0.90
getSitesFixesForFunction · 0.90
createTextStyleFunction · 0.90

Tested by

no test coverage detected