diff options
author | 2021-10-29 05:58:33 +0530 | |
---|---|---|
committer | 2021-10-29 05:58:33 +0530 | |
commit | a956ab9c9f3c571232118babe699709c9e0144d7 (patch) | |
tree | e612be3f6df3ccae0635400c64c1b9ec65b4bd87 | |
parent | Update submodules, browserslist data updates and linter fixes [skip ci] (diff) | |
download | ferdium-app-a956ab9c9f3c571232118babe699709c9e0144d7.tar.gz ferdium-app-a956ab9c9f3c571232118babe699709c9e0144d7.tar.zst ferdium-app-a956ab9c9f3c571232118babe699709c9e0144d7.zip |
refactor: Remove duplicate code
-rw-r--r-- | src/webview/darkmode.ts | 13 | ||||
-rw-r--r-- | src/webview/recipe.js | 16 |
2 files changed, 14 insertions, 15 deletions
diff --git a/src/webview/darkmode.ts b/src/webview/darkmode.ts index 7b9407049..a0699bdb7 100644 --- a/src/webview/darkmode.ts +++ b/src/webview/darkmode.ts | |||
@@ -9,10 +9,17 @@ const ID = [...Array.from({ length: 20 })] | |||
9 | .map(() => chars[Math.trunc(Math.random() * chars.length)]) | 9 | .map(() => chars[Math.trunc(Math.random() * chars.length)]) |
10 | .join(''); | 10 | .join(''); |
11 | 11 | ||
12 | function darkModeFilePath(recipePath: string) { | ||
13 | return join(recipePath, 'darkmode.css'); | ||
14 | } | ||
15 | |||
16 | export function darkModeStyleExists(recipePath: string) { | ||
17 | return pathExistsSync(darkModeFilePath(recipePath)); | ||
18 | } | ||
19 | |||
12 | export function injectDarkModeStyle(recipePath: string) { | 20 | export function injectDarkModeStyle(recipePath: string) { |
13 | const darkModeStyle = join(recipePath, 'darkmode.css'); | 21 | if (darkModeStyleExists(recipePath)) { |
14 | if (pathExistsSync(darkModeStyle)) { | 22 | const data = readFileSync(darkModeFilePath(recipePath)); |
15 | const data = readFileSync(darkModeStyle); | ||
16 | const styles = document.createElement('style'); | 23 | const styles = document.createElement('style'); |
17 | styles.id = ID; | 24 | styles.id = ID; |
18 | styles.innerHTML = data.toString(); | 25 | styles.innerHTML = data.toString(); |
diff --git a/src/webview/recipe.js b/src/webview/recipe.js index 70241419a..0b44d1d01 100644 --- a/src/webview/recipe.js +++ b/src/webview/recipe.js | |||
@@ -27,6 +27,7 @@ import { DialogTitleHandler } from './dialogTitle'; | |||
27 | import { SessionHandler } from './sessionHandler'; | 27 | import { SessionHandler } from './sessionHandler'; |
28 | import contextMenu from './contextMenu'; | 28 | import contextMenu from './contextMenu'; |
29 | import { | 29 | import { |
30 | darkModeStyleExists, | ||
30 | injectDarkModeStyle, | 31 | injectDarkModeStyle, |
31 | isDarkModeStyleInjected, | 32 | isDarkModeStyleInjected, |
32 | removeDarkModeStyle, | 33 | removeDarkModeStyle, |
@@ -320,15 +321,6 @@ class RecipeController { | |||
320 | ) { | 321 | ) { |
321 | debug('Enable dark mode'); | 322 | debug('Enable dark mode'); |
322 | 323 | ||
323 | // Check if recipe has a darkmode.css | ||
324 | const darkModeStyle = join( | ||
325 | this.settings.service.recipe.path, | ||
326 | 'darkmode.css', | ||
327 | ); | ||
328 | const darkModeExists = pathExistsSync(darkModeStyle); | ||
329 | |||
330 | debug('darkmode.css exists? ', darkModeExists); | ||
331 | |||
332 | // Check if recipe has a custom dark mode handler | 324 | // Check if recipe has a custom dark mode handler |
333 | if (this.recipe && this.recipe.darkModeHandler) { | 325 | if (this.recipe && this.recipe.darkModeHandler) { |
334 | debug('Using custom dark mode handler'); | 326 | debug('Using custom dark mode handler'); |
@@ -341,8 +333,8 @@ class RecipeController { | |||
341 | } | 333 | } |
342 | 334 | ||
343 | this.recipe.darkModeHandler(true, handlerConfig); | 335 | this.recipe.darkModeHandler(true, handlerConfig); |
344 | } else if (darkModeExists) { | 336 | } else if (darkModeStyleExists(this.settings.service.recipe.path)) { |
345 | debug('Injecting darkmode.css'); | 337 | debug('Injecting darkmode from recipe'); |
346 | injectDarkModeStyle(this.settings.service.recipe.path); | 338 | injectDarkModeStyle(this.settings.service.recipe.path); |
347 | 339 | ||
348 | // Make sure universal dark mode is disabled | 340 | // Make sure universal dark mode is disabled |
@@ -379,7 +371,7 @@ class RecipeController { | |||
379 | 371 | ||
380 | this.recipe.darkModeHandler(false, handlerConfig); | 372 | this.recipe.darkModeHandler(false, handlerConfig); |
381 | } else if (isDarkModeStyleInjected()) { | 373 | } else if (isDarkModeStyleInjected()) { |
382 | debug('Removing injected darkmode.css'); | 374 | debug('Removing injected darkmode from recipe'); |
383 | removeDarkModeStyle(); | 375 | removeDarkModeStyle(); |
384 | } else { | 376 | } else { |
385 | debug('Removing Dark Reader'); | 377 | debug('Removing Dark Reader'); |