forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcss-filter.ts
More file actions
272 lines (237 loc) · 9.12 KB
/
Copy pathcss-filter.ts
File metadata and controls
272 lines (237 loc) · 9.12 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import {formatSitesFixesConfig} from './utils/format';
import {applyColorMatrix, createFilterMatrix} from './utils/matrix';
import {parseSitesFixesConfig, getSitesFixesFor} from './utils/parse';
import type {SitePropsIndex} from './utils/parse';
import {parseArray, formatArray} from '../utils/text';
import {compareURLPatterns, isURLInList} from '../utils/url';
import {createTextStyle} from './text-style';
import type {Theme, InversionFix} from '../definitions';
import {compareChromeVersions, chromiumVersion, isFirefox, firefoxVersion} from '../utils/platform';
declare const __CHROMIUM_MV2__: boolean;
declare const __CHROMIUM_MV3__: boolean;
export enum FilterMode {
light = 0,
dark = 1
}
/**
* This checks if the current chromium version has the patch in it.
* As of Chromium v81.0.4035.0 this has been the situation
*
* Bug report: https://bugs.chromium.org/p/chromium/issues/detail?id=501582
* Patch: https://chromium-review.googlesource.com/c/chromium/src/+/1979258
*/
export function hasPatchForChromiumIssue501582(): boolean {
return __CHROMIUM_MV3__ || Boolean(
__CHROMIUM_MV2__ &&
compareChromeVersions(chromiumVersion, '81.0.4035.0') >= 0
);
}
/**
* Since Firefox v102.0, they have changed to the new root behavior.
* This was already the case for Chromium v81.0.4035.0 and Firefox now
* switched over as well.
*/
export function hasFirefoxNewRootBehavior(): boolean {
return Boolean(
isFirefox &&
compareChromeVersions(firefoxVersion, '102.0') >= 0
);
}
export default function createCSSFilterStyleSheet(config: Theme, url: string, isTopFrame: boolean, fixes: string, index: SitePropsIndex<InversionFix>): string {
const filterValue = getCSSFilterValue(config)!;
const reverseFilterValue = 'invert(100%) hue-rotate(180deg)';
return cssFilterStyleSheetTemplate('html', filterValue, reverseFilterValue, config, url, isTopFrame, fixes, index);
}
export function cssFilterStyleSheetTemplate(filterRoot: string, filterValue: string, reverseFilterValue: string, config: Theme, url: string, isTopFrame: boolean, fixes: string, index: SitePropsIndex<InversionFix>): string {
const fix = getInversionFixesFor(url, fixes, index);
const lines: string[] = [];
lines.push('@media screen {');
// Add leading rule
if (filterValue && isTopFrame) {
lines.push('');
lines.push('/* Leading rule */');
lines.push(createLeadingRule(filterRoot, filterValue));
}
if (config.mode === FilterMode.dark) {
// Add reverse rule
lines.push('');
lines.push('/* Reverse rule */');
lines.push(createReverseRule(reverseFilterValue, fix));
}
if (config.useFont || config.textStroke > 0) {
// Add text rule
lines.push('');
lines.push('/* Font */');
lines.push(createTextStyle(config));
}
// Fix bad font hinting after inversion
lines.push('');
lines.push('/* Text contrast */');
lines.push('html {');
lines.push(' text-shadow: 0 0 0 !important;');
lines.push('}');
// Full screen fix
lines.push('');
lines.push('/* Full screen */');
[':-webkit-full-screen', ':-moz-full-screen', ':fullscreen'].forEach((fullScreen) => {
lines.push(`${fullScreen}, ${fullScreen} * {`);
lines.push(' -webkit-filter: none !important;');
lines.push(' filter: none !important;');
lines.push('}');
});
if (isTopFrame) {
const light: [number, number, number] = [255, 255, 255];
// If browser affected by Chromium Issue 501582, set dark background on html
// Or if browser is Firefox v102+
const bgColor = (!hasPatchForChromiumIssue501582() && !hasFirefoxNewRootBehavior()) && config.mode === FilterMode.dark ?
applyColorMatrix(light, createFilterMatrix(config)).map(Math.round) :
light;
lines.push('');
lines.push('/* Page background */');
lines.push('html {');
lines.push(` background: rgb(${bgColor.join(',')}) !important;`);
lines.push('}');
}
if (fix.css && fix.css.length > 0 && config.mode === FilterMode.dark) {
lines.push('');
lines.push('/* Custom rules */');
lines.push(fix.css);
}
lines.push('');
lines.push('}');
return lines.join('\n');
}
export function getCSSFilterValue(config: Theme): string | null {
const filters: string[] = [];
if (config.mode === FilterMode.dark) {
filters.push('invert(100%) hue-rotate(180deg)');
}
if (config.brightness !== 100) {
filters.push(`brightness(${config.brightness}%)`);
}
if (config.contrast !== 100) {
filters.push(`contrast(${config.contrast}%)`);
}
if (config.grayscale !== 0) {
filters.push(`grayscale(${config.grayscale}%)`);
}
if (config.sepia !== 0) {
filters.push(`sepia(${config.sepia}%)`);
}
if (filters.length === 0) {
return null;
}
return filters.join(' ');
}
function createLeadingRule(filterRoot: string, filterValue: string): string {
return [
`${filterRoot} {`,
` -webkit-filter: ${filterValue} !important;`,
` filter: ${filterValue} !important;`,
'}',
].join('\n');
}
function joinSelectors(selectors: string[]): string {
return selectors.map((s) => s.replace(/\,$/, '')).join(',\n');
}
function createReverseRule(reverseFilterValue: string, fix: InversionFix): string {
const lines: string[] = [];
if (fix.invert.length > 0) {
lines.push(`${joinSelectors(fix.invert)} {`);
lines.push(` -webkit-filter: ${reverseFilterValue} !important;`);
lines.push(` filter: ${reverseFilterValue} !important;`);
lines.push('}');
}
if (fix.noinvert.length > 0) {
lines.push(`${joinSelectors(fix.noinvert)} {`);
lines.push(' -webkit-filter: none !important;');
lines.push(' filter: none !important;');
lines.push('}');
}
if (fix.removebg.length > 0) {
lines.push(`${joinSelectors(fix.removebg)} {`);
lines.push(' background: white !important;');
lines.push('}');
}
return lines.join('\n');
}
/**
* Returns fixes for a given URL.
* If no matches found, common fixes will be returned.
* @param url Site URL.
* @param inversionFixes List of inversion fixes.
*/
export function getInversionFixesFor(url: string, fixes: string, index: SitePropsIndex<InversionFix>): InversionFix {
const inversionFixes = getSitesFixesFor<InversionFix>(url, fixes, index, {
commands: Object.keys(inversionFixesCommands),
getCommandPropName: (command) => inversionFixesCommands[command],
parseCommandValue: (command, value) => {
if (command === 'CSS') {
return value.trim();
}
return parseArray(value);
},
});
const common = {
url: inversionFixes[0].url,
invert: inversionFixes[0].invert || [],
noinvert: inversionFixes[0].noinvert || [],
removebg: inversionFixes[0].removebg || [],
css: inversionFixes[0].css || '',
};
if (url) {
// Search for match with given URL
const matches = inversionFixes
.slice(1)
.filter((s) => isURLInList(url, s.url))
.sort((a, b) => b.url[0].length - a.url[0].length);
if (matches.length > 0) {
const found = matches[0];
return {
url: found.url,
invert: common.invert.concat(found.invert || []),
noinvert: common.noinvert.concat(found.noinvert || []),
removebg: common.removebg.concat(found.removebg || []),
css: [common.css, found.css].filter((s) => s).join('\n'),
};
}
}
return common;
}
const inversionFixesCommands: { [key: string]: keyof InversionFix } = {
'INVERT': 'invert',
'NO INVERT': 'noinvert',
'REMOVE BG': 'removebg',
'CSS': 'css',
};
export function parseInversionFixes(text: string): InversionFix[] {
return parseSitesFixesConfig<InversionFix>(text, {
commands: Object.keys(inversionFixesCommands),
getCommandPropName: (command) => inversionFixesCommands[command],
parseCommandValue: (command, value) => {
if (command === 'CSS') {
return value.trim();
}
return parseArray(value);
},
});
}
export function formatInversionFixes(inversionFixes: InversionFix[]): string {
const fixes = inversionFixes.slice().sort((a, b) => compareURLPatterns(a.url[0], b.url[0]));
return formatSitesFixesConfig(fixes, {
props: Object.values(inversionFixesCommands),
getPropCommandName: (prop) => Object.entries(inversionFixesCommands).find(([, p]) => p === prop)![0],
formatPropValue: (prop, value) => {
if (prop === 'css') {
return (value as string).trim().replace(/\n+/g, '\n');
}
return formatArray(value as string[]).trim();
},
shouldIgnoreProp: (prop, value) => {
if (prop === 'css') {
return !value;
}
return !(Array.isArray(value) && value.length > 0);
},
});
}