diff options
author | Vijay A <avijayr@protonmail.com> | 2021-08-21 08:07:32 +0530 |
---|---|---|
committer | Vijay A <avijayr@protonmail.com> | 2021-08-21 08:55:38 +0530 |
commit | b1cf1849f5bfa8f297f78a5ca58d797f466b3086 (patch) | |
tree | 658f3adeb740cf54021dfb6ad951649f0d539e6d /src/webview/darkmode.ts | |
parent | refactor(cleanup): remove code that refers to paid subscription (diff) | |
download | ferdium-app-b1cf1849f5bfa8f297f78a5ca58d797f466b3086.tar.gz ferdium-app-b1cf1849f5bfa8f297f78a5ca58d797f466b3086.tar.zst ferdium-app-b1cf1849f5bfa8f297f78a5ca58d797f466b3086.zip |
chore: typescript conversion
Diffstat (limited to 'src/webview/darkmode.ts')
-rw-r--r-- | src/webview/darkmode.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/webview/darkmode.ts b/src/webview/darkmode.ts new file mode 100644 index 000000000..e06c22f11 --- /dev/null +++ b/src/webview/darkmode.ts | |||
@@ -0,0 +1,38 @@ | |||
1 | /* eslint no-bitwise: ["error", { "int32Hint": true }] */ | ||
2 | |||
3 | import { join } from 'path'; | ||
4 | import { pathExistsSync, readFileSync } from 'fs-extra'; | ||
5 | |||
6 | const debug = require('debug')('Ferdi:DarkMode'); | ||
7 | |||
8 | const chars = [...'abcdefghijklmnopqrstuvwxyz']; | ||
9 | |||
10 | const ID = [...Array(20)].map(() => chars[Math.random() * chars.length | 0]).join(''); | ||
11 | |||
12 | export function injectDarkModeStyle(recipePath: string) { | ||
13 | const darkModeStyle = join(recipePath, 'darkmode.css'); | ||
14 | if (pathExistsSync(darkModeStyle)) { | ||
15 | const data = readFileSync(darkModeStyle); | ||
16 | const styles = document.createElement('style'); | ||
17 | styles.id = ID; | ||
18 | styles.innerHTML = data.toString(); | ||
19 | |||
20 | document.querySelector('head')?.appendChild(styles); | ||
21 | |||
22 | debug('Injected Dark Mode style with ID', ID); | ||
23 | } | ||
24 | } | ||
25 | |||
26 | export function removeDarkModeStyle() { | ||
27 | const style = document.querySelector(`#${ID}`); | ||
28 | |||
29 | if (style) { | ||
30 | style.remove(); | ||
31 | |||
32 | debug('Removed Dark Mode Style with ID', ID); | ||
33 | } | ||
34 | } | ||
35 | |||
36 | export function isDarkModeStyleInjected() { | ||
37 | return !!document.querySelector(`#${ID}`); | ||
38 | } | ||