aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/darkmode.ts
blob: e06c22f11bc3769e25e6de43569ba53d7e10a0b7 (plain) (blame)
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
/* eslint no-bitwise: ["error", { "int32Hint": true }] */

import { join } from 'path';
import { pathExistsSync, readFileSync } from 'fs-extra';

const debug = require('debug')('Ferdi:DarkMode');

const chars = [...'abcdefghijklmnopqrstuvwxyz'];

const ID = [...Array(20)].map(() => chars[Math.random() * chars.length | 0]).join('');

export function injectDarkModeStyle(recipePath: string) {
  const darkModeStyle = join(recipePath, 'darkmode.css');
  if (pathExistsSync(darkModeStyle)) {
    const data = readFileSync(darkModeStyle);
    const styles = document.createElement('style');
    styles.id = ID;
    styles.innerHTML = data.toString();

    document.querySelector('head')?.appendChild(styles);

    debug('Injected Dark Mode style with ID', ID);
  }
}

export function removeDarkModeStyle() {
  const style = document.querySelector(`#${ID}`);

  if (style) {
    style.remove();

    debug('Removed Dark Mode Style with ID', ID);
  }
}

export function isDarkModeStyleInjected() {
  return !!document.querySelector(`#${ID}`);
}