forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
56 lines (47 loc) · 1.5 KB
/
Copy pathindex.ts
File metadata and controls
56 lines (47 loc) · 1.5 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
import './chrome';
import {setFetchMethod as setFetch} from './fetch';
import {DEFAULT_THEME} from '../defaults';
import {Theme, DynamicThemeFix} from '../definitions';
import ThemeEngines from '../generators/theme-engines';
import {createOrUpdateDynamicTheme, removeDynamicTheme} from '../inject/dynamic-theme';
const isIFrame = (() => {
try {
return window.self !== window.top;
} catch (err) {
console.warn(err);
return true;
}
})();
export function enable(themeOptions: Partial<Theme> = {}, fixes: DynamicThemeFix = null) {
const theme = {...DEFAULT_THEME, ...themeOptions};
if (theme.engine !== ThemeEngines.dynamicTheme) {
throw new Error('Theme engine is not supported');
}
createOrUpdateDynamicTheme(theme, fixes, isIFrame);
}
export function disable() {
removeDynamicTheme();
}
const darkScheme = matchMedia('(prefers-color-scheme: dark)');
let store = {
themeOptions: null as Partial<Theme>,
fixes: null as DynamicThemeFix,
};
function handleColorScheme() {
if (darkScheme.matches) {
enable(store.themeOptions, store.fixes);
} else {
disable();
}
}
export function auto(themeOptions: Partial<Theme> | false = {}, fixes: DynamicThemeFix = null) {
if (themeOptions) {
store = {themeOptions, fixes};
handleColorScheme();
darkScheme.addListener(handleColorScheme);
} else {
darkScheme.removeListener(handleColorScheme);
disable();
}
}
export const setFetchMethod = setFetch;