diff options
Diffstat (limited to 'src/webview/recipe.js')
-rw-r--r-- | src/webview/recipe.js | 65 |
1 files changed, 61 insertions, 4 deletions
diff --git a/src/webview/recipe.js b/src/webview/recipe.js index 07d29f477..bad5a93b2 100644 --- a/src/webview/recipe.js +++ b/src/webview/recipe.js | |||
@@ -1,10 +1,11 @@ | |||
1 | /* eslint-disable import/first */ | 1 | /* eslint-disable import/first */ |
2 | import { ipcRenderer } from 'electron'; | 2 | import { ipcRenderer, remote } from 'electron'; |
3 | import path from 'path'; | 3 | import path from 'path'; |
4 | import { autorun, computed, observable } from 'mobx'; | 4 | import { autorun, computed, observable } from 'mobx'; |
5 | import fs from 'fs-extra'; | 5 | import fs from 'fs-extra'; |
6 | import { loadModule } from 'cld3-asm'; | 6 | import { loadModule } from 'cld3-asm'; |
7 | import { debounce } from 'lodash'; | 7 | import { debounce } from 'lodash'; |
8 | import { FindInPage } from 'electron-find'; | ||
8 | 9 | ||
9 | // For some services darkreader tries to use the chrome extension message API | 10 | // For some services darkreader tries to use the chrome extension message API |
10 | // This will cause the service to fail loading | 11 | // This will cause the service to fail loading |
@@ -47,10 +48,15 @@ class RecipeController { | |||
47 | 'settings-update': 'updateAppSettings', | 48 | 'settings-update': 'updateAppSettings', |
48 | 'service-settings-update': 'updateServiceSettings', | 49 | 'service-settings-update': 'updateServiceSettings', |
49 | 'get-service-id': 'serviceIdEcho', | 50 | 'get-service-id': 'serviceIdEcho', |
51 | 'find-in-page': 'openFindInPage', | ||
50 | }; | 52 | }; |
51 | 53 | ||
52 | universalDarkModeInjected = false; | 54 | universalDarkModeInjected = false; |
53 | 55 | ||
56 | recipe = null; | ||
57 | |||
58 | hasUpdatedBeforeRecipeLoaded = false; | ||
59 | |||
54 | constructor() { | 60 | constructor() { |
55 | this.initialize(); | 61 | this.initialize(); |
56 | } | 62 | } |
@@ -61,6 +67,8 @@ class RecipeController { | |||
61 | 67 | ||
62 | cldIdentifier = null; | 68 | cldIdentifier = null; |
63 | 69 | ||
70 | findInPage = null; | ||
71 | |||
64 | async initialize() { | 72 | async initialize() { |
65 | Object.keys(this.ipcEvents).forEach((channel) => { | 73 | Object.keys(this.ipcEvents).forEach((channel) => { |
66 | ipcRenderer.on(channel, (...args) => { | 74 | ipcRenderer.on(channel, (...args) => { |
@@ -73,6 +81,13 @@ class RecipeController { | |||
73 | setTimeout(() => ipcRenderer.sendToHost('hello'), 100); | 81 | setTimeout(() => ipcRenderer.sendToHost('hello'), 100); |
74 | await spellchecker(); | 82 | await spellchecker(); |
75 | autorun(() => this.update()); | 83 | autorun(() => this.update()); |
84 | |||
85 | document.addEventListener('DOMContentLoaded', () => { | ||
86 | this.findInPage = new FindInPage(remote.getCurrentWebContents(), { | ||
87 | inputFocusColor: '#CE9FFC', | ||
88 | textColor: '#212121', | ||
89 | }); | ||
90 | }); | ||
76 | } | 91 | } |
77 | 92 | ||
78 | loadRecipeModule(event, config, recipe) { | 93 | loadRecipeModule(event, config, recipe) { |
@@ -82,11 +97,15 @@ class RecipeController { | |||
82 | // Delete module from cache | 97 | // Delete module from cache |
83 | delete require.cache[require.resolve(modulePath)]; | 98 | delete require.cache[require.resolve(modulePath)]; |
84 | try { | 99 | try { |
100 | this.recipe = new RecipeWebview(); | ||
85 | // eslint-disable-next-line | 101 | // eslint-disable-next-line |
86 | require(modulePath)(new RecipeWebview(), {...config, recipe,}); | 102 | require(modulePath)(this.recipe, {...config, recipe,}); |
87 | debug('Initialize Recipe', config, recipe); | 103 | debug('Initialize Recipe', config, recipe); |
88 | 104 | ||
89 | this.settings.service = Object.assign(config, { recipe }); | 105 | this.settings.service = Object.assign(config, { recipe }); |
106 | |||
107 | // Make sure to update the WebView, otherwise the custom darkmode handler may not be used | ||
108 | this.update(); | ||
90 | } catch (err) { | 109 | } catch (err) { |
91 | console.error('Recipe initialization failed', err); | 110 | console.error('Recipe initialization failed', err); |
92 | } | 111 | } |
@@ -125,6 +144,10 @@ class RecipeController { | |||
125 | } | 144 | } |
126 | } | 145 | } |
127 | 146 | ||
147 | openFindInPage() { | ||
148 | this.findInPage.openFindWindow(); | ||
149 | } | ||
150 | |||
128 | update() { | 151 | update() { |
129 | debug('enableSpellchecking', this.settings.app.enableSpellchecking); | 152 | debug('enableSpellchecking', this.settings.app.enableSpellchecking); |
130 | debug('isDarkModeEnabled', this.settings.service.isDarkModeEnabled); | 153 | debug('isDarkModeEnabled', this.settings.service.isDarkModeEnabled); |
@@ -151,12 +174,25 @@ class RecipeController { | |||
151 | } | 174 | } |
152 | } | 175 | } |
153 | 176 | ||
177 | if (!this.recipe) { | ||
178 | this.hasUpdatedBeforeRecipeLoaded = true; | ||
179 | } | ||
180 | |||
154 | console.log( | 181 | console.log( |
155 | 'Darkmode enabled?', | 182 | 'Darkmode enabled?', |
156 | this.settings.service.isDarkModeEnabled, | 183 | this.settings.service.isDarkModeEnabled, |
157 | 'Dark theme active?', | 184 | 'Dark theme active?', |
158 | this.settings.app.isDarkThemeActive, | 185 | this.settings.app.isDarkThemeActive, |
159 | ); | 186 | ); |
187 | |||
188 | const handlerConfig = { | ||
189 | removeDarkModeStyle, | ||
190 | disableDarkMode, | ||
191 | enableDarkMode, | ||
192 | injectDarkModeStyle: () => injectDarkModeStyle(this.settings.service.recipe.path), | ||
193 | isDarkModeStyleInjected, | ||
194 | }; | ||
195 | |||
160 | if (this.settings.service.isDarkModeEnabled && this.settings.app.isDarkThemeActive !== false) { | 196 | if (this.settings.service.isDarkModeEnabled && this.settings.app.isDarkThemeActive !== false) { |
161 | debug('Enable dark mode'); | 197 | debug('Enable dark mode'); |
162 | 198 | ||
@@ -166,7 +202,19 @@ class RecipeController { | |||
166 | 202 | ||
167 | console.log('darkmode.css exists? ', darkModeExists ? 'Yes' : 'No'); | 203 | console.log('darkmode.css exists? ', darkModeExists ? 'Yes' : 'No'); |
168 | 204 | ||
169 | if (darkModeExists) { | 205 | // Check if recipe has a custom dark mode handler |
206 | if (this.recipe && this.recipe.darkModeHandler) { | ||
207 | console.log('Using custom dark mode handler'); | ||
208 | |||
209 | // Remove other dark mode styles if they were already loaded | ||
210 | if (this.hasUpdatedBeforeRecipeLoaded) { | ||
211 | this.hasUpdatedBeforeRecipeLoaded = false; | ||
212 | removeDarkModeStyle(); | ||
213 | disableDarkMode(); | ||
214 | } | ||
215 | |||
216 | this.recipe.darkModeHandler(true, handlerConfig); | ||
217 | } else if (darkModeExists) { | ||
170 | console.log('Injecting darkmode.css'); | 218 | console.log('Injecting darkmode.css'); |
171 | injectDarkModeStyle(this.settings.service.recipe.path); | 219 | injectDarkModeStyle(this.settings.service.recipe.path); |
172 | 220 | ||
@@ -186,7 +234,16 @@ class RecipeController { | |||
186 | debug('Remove dark mode'); | 234 | debug('Remove dark mode'); |
187 | console.log('DarkMode disabled - removing remaining styles'); | 235 | console.log('DarkMode disabled - removing remaining styles'); |
188 | 236 | ||
189 | if (isDarkModeStyleInjected()) { | 237 | if (this.recipe && this.recipe.darkModeHandler) { |
238 | // Remove other dark mode styles if they were already loaded | ||
239 | if (this.hasUpdatedBeforeRecipeLoaded) { | ||
240 | this.hasUpdatedBeforeRecipeLoaded = false; | ||
241 | removeDarkModeStyle(); | ||
242 | disableDarkMode(); | ||
243 | } | ||
244 | |||
245 | this.recipe.darkModeHandler(false, handlerConfig); | ||
246 | } else if (isDarkModeStyleInjected()) { | ||
190 | console.log('Removing injected darkmode.css'); | 247 | console.log('Removing injected darkmode.css'); |
191 | removeDarkModeStyle(); | 248 | removeDarkModeStyle(); |
192 | } else { | 249 | } else { |