From f9b3ccf26ffff99b111c19e94d47d2fd76cf8e8c Mon Sep 17 00:00:00 2001 From: Johannes Millan Date: Thu, 29 Oct 2020 21:27:29 +0100 Subject: [PATCH] feat: load custom styles from userData folder #210 --- electron/main-window.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/electron/main-window.ts b/electron/main-window.ts index a1a8a82586..f3e5590fd2 100644 --- a/electron/main-window.ts +++ b/electron/main-window.ts @@ -14,6 +14,7 @@ import { join, normalize } from 'path'; import { format } from 'url'; import { IPC } from './ipc-events.const'; import { getSettings } from './get-settings'; +import { readFileSync, stat } from 'fs'; let mainWin: BrowserWindow; @@ -96,7 +97,19 @@ export const createWindow = ({ slashes: true, }); - mainWin.loadURL(url); + mainWin.loadURL(url).then(() => { + // load custom stylesheet if any + const CSS_FILE_PATH = app.getPath('userData') + '/styles.css'; + stat(app.getPath('userData') + '/styles.css', (err) => { + if (err) { + console.log('No custom styles detected at ' + CSS_FILE_PATH); + } else { + console.log('Loading custom styles from ' + CSS_FILE_PATH); + const styles = readFileSync(CSS_FILE_PATH, {encoding: 'utf8'}); + mainWin.webContents.insertCSS(styles).then(console.log).catch(console.error); + } + }); + }); // show gracefully mainWin.once('ready-to-show', () => {