aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/recipe.js
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-04-23 01:59:21 +0200
committerLibravatar GitHub <noreply@github.com>2022-04-22 23:59:21 +0000
commitd02644f7c41150709795e57bfd40351b4da35a7b (patch)
tree2403fb76bd5fae1703f8b55172ffce9e0a5d2bce /src/webview/recipe.js
parentComplete tray icons redesign for all platforms (#28) (diff)
downloadferdium-app-d02644f7c41150709795e57bfd40351b4da35a7b.tar.gz
ferdium-app-d02644f7c41150709795e57bfd40351b4da35a7b.tar.zst
ferdium-app-d02644f7c41150709795e57bfd40351b4da35a7b.zip
Preload safe debug shim (#29)
In https://github.com/ferdium/ferdium-app/pull/23 we removed usages of the debug package due to an electron bug. This patch aims to restore some debug functionality by introducing a shim. The shim detect whether if it is being introduced in a preload script where the electron but would be triggered, and falls back to a simple replacement for debug. However, in the main and renderer processes, where a preload script is not being used, we still get full debug functionality. In this way, a module can be used both in a preload script and outside of it, while still preserving debug functionality whenever possible. Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'src/webview/recipe.js')
-rw-r--r--src/webview/recipe.js57
1 files changed, 28 insertions, 29 deletions
diff --git a/src/webview/recipe.js b/src/webview/recipe.js
index 1f3017ccd..eff20c2d3 100644
--- a/src/webview/recipe.js
+++ b/src/webview/recipe.js
@@ -50,8 +50,7 @@ import {
50 50
51import { DEFAULT_APP_SETTINGS } from '../config'; 51import { DEFAULT_APP_SETTINGS } from '../config';
52 52
53// TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed 53const debug = require('../preload-safe-debug')('Ferdium:Plugin');
54// const debug = require('debug')('Ferdium:Plugin');
55 54
56const badgeHandler = new BadgeHandler(); 55const badgeHandler = new BadgeHandler();
57 56
@@ -65,7 +64,7 @@ const notificationsHandler = new NotificationsHandler();
65const originalWindowOpen = window.open; 64const originalWindowOpen = window.open;
66 65
67window.open = (url, frameName, features) => { 66window.open = (url, frameName, features) => {
68 console.log('window.open', url, frameName, features); 67 debug('window.open', url, frameName, features);
69 if (!url) { 68 if (!url) {
70 // The service hasn't yet supplied a URL (as used in Skype). 69 // The service hasn't yet supplied a URL (as used in Skype).
71 // Return a new dummy window object and wait for the service to change the properties 70 // Return a new dummy window object and wait for the service to change the properties
@@ -171,12 +170,12 @@ class RecipeController {
171 async initialize() { 170 async initialize() {
172 for (const channel of Object.keys(this.ipcEvents)) { 171 for (const channel of Object.keys(this.ipcEvents)) {
173 ipcRenderer.on(channel, (...args) => { 172 ipcRenderer.on(channel, (...args) => {
174 console.log('Received IPC event for channel', channel, 'with', ...args); 173 debug('Received IPC event for channel', channel, 'with', ...args);
175 this[this.ipcEvents[channel]](...args); 174 this[this.ipcEvents[channel]](...args);
176 }); 175 });
177 } 176 }
178 177
179 console.log('Send "hello" to host'); 178 debug('Send "hello" to host');
180 setTimeout(() => ipcRenderer.sendToHost('hello'), 100); 179 setTimeout(() => ipcRenderer.sendToHost('hello'), 100);
181 180
182 this.spellcheckingProvider = null; 181 this.spellcheckingProvider = null;
@@ -199,9 +198,9 @@ class RecipeController {
199 } 198 }
200 199
201 loadRecipeModule(event, config, recipe) { 200 loadRecipeModule(event, config, recipe) {
202 console.log('loadRecipeModule'); 201 debug('loadRecipeModule');
203 const modulePath = join(recipe.path, 'webview.js'); 202 const modulePath = join(recipe.path, 'webview.js');
204 console.log('module path', modulePath); 203 debug('module path', modulePath);
205 // Delete module from cache 204 // Delete module from cache
206 delete require.cache[require.resolve(modulePath)]; 205 delete require.cache[require.resolve(modulePath)];
207 try { 206 try {
@@ -214,7 +213,7 @@ class RecipeController {
214 if (existsSync(modulePath)) { 213 if (existsSync(modulePath)) {
215 // eslint-disable-next-line import/no-dynamic-require 214 // eslint-disable-next-line import/no-dynamic-require
216 require(modulePath)(this.recipe, { ...config, recipe }); 215 require(modulePath)(this.recipe, { ...config, recipe });
217 console.log('Initialize Recipe', config, recipe); 216 debug('Initialize Recipe', config, recipe);
218 } 217 }
219 218
220 this.settings.service = Object.assign(config, { recipe }); 219 this.settings.service = Object.assign(config, { recipe });
@@ -266,18 +265,18 @@ class RecipeController {
266 } 265 }
267 266
268 update() { 267 update() {
269 console.log('enableSpellchecking', this.settings.app.enableSpellchecking); 268 debug('enableSpellchecking', this.settings.app.enableSpellchecking);
270 console.log('isDarkModeEnabled', this.settings.service.isDarkModeEnabled); 269 debug('isDarkModeEnabled', this.settings.service.isDarkModeEnabled);
271 console.log( 270 debug(
272 'System spellcheckerLanguage', 271 'System spellcheckerLanguage',
273 this.settings.app.spellcheckerLanguage, 272 this.settings.app.spellcheckerLanguage,
274 ); 273 );
275 console.log( 274 debug(
276 'Service spellcheckerLanguage', 275 'Service spellcheckerLanguage',
277 this.settings.service.spellcheckerLanguage, 276 this.settings.service.spellcheckerLanguage,
278 ); 277 );
279 console.log('darkReaderSettigs', this.settings.service.darkReaderSettings); 278 debug('darkReaderSettigs', this.settings.service.darkReaderSettings);
280 console.log('searchEngine', this.settings.app.searchEngine); 279 debug('searchEngine', this.settings.app.searchEngine);
281 280
282 if (this.userscript && this.userscript.internal_setSettings) { 281 if (this.userscript && this.userscript.internal_setSettings) {
283 this.userscript.internal_setSettings(this.settings); 282 this.userscript.internal_setSettings(this.settings);
@@ -285,10 +284,10 @@ class RecipeController {
285 284
286 if (this.settings.app.enableSpellchecking) { 285 if (this.settings.app.enableSpellchecking) {
287 let { spellcheckerLanguage } = this; 286 let { spellcheckerLanguage } = this;
288 console.log(`Setting spellchecker language to ${spellcheckerLanguage}`); 287 debug(`Setting spellchecker language to ${spellcheckerLanguage}`);
289 if (spellcheckerLanguage.includes('automatic')) { 288 if (spellcheckerLanguage.includes('automatic')) {
290 this.automaticLanguageDetection(); 289 this.automaticLanguageDetection();
291 console.log( 290 debug(
292 'Found `automatic` locale, falling back to user locale until detected', 291 'Found `automatic` locale, falling back to user locale until detected',
293 this.settings.app.locale, 292 this.settings.app.locale,
294 ); 293 );
@@ -296,14 +295,14 @@ class RecipeController {
296 } 295 }
297 switchDict(spellcheckerLanguage, this.settings.service.id); 296 switchDict(spellcheckerLanguage, this.settings.service.id);
298 } else { 297 } else {
299 console.log('Disable spellchecker'); 298 debug('Disable spellchecker');
300 } 299 }
301 300
302 if (!this.recipe) { 301 if (!this.recipe) {
303 this.hasUpdatedBeforeRecipeLoaded = true; 302 this.hasUpdatedBeforeRecipeLoaded = true;
304 } 303 }
305 304
306 console.log( 305 debug(
307 'Darkmode enabled?', 306 'Darkmode enabled?',
308 this.settings.service.isDarkModeEnabled, 307 this.settings.service.isDarkModeEnabled,
309 'Dark theme active?', 308 'Dark theme active?',
@@ -323,11 +322,11 @@ class RecipeController {
323 this.settings.service.isDarkModeEnabled && 322 this.settings.service.isDarkModeEnabled &&
324 this.settings.app.isDarkThemeActive !== false 323 this.settings.app.isDarkThemeActive !== false
325 ) { 324 ) {
326 console.log('Enable dark mode'); 325 debug('Enable dark mode');
327 326
328 // Check if recipe has a custom dark mode handler 327 // Check if recipe has a custom dark mode handler
329 if (this.recipe && this.recipe.darkModeHandler) { 328 if (this.recipe && this.recipe.darkModeHandler) {
330 console.log('Using custom dark mode handler'); 329 debug('Using custom dark mode handler');
331 330
332 // Remove other dark mode styles if they were already loaded 331 // Remove other dark mode styles if they were already loaded
333 if (this.hasUpdatedBeforeRecipeLoaded) { 332 if (this.hasUpdatedBeforeRecipeLoaded) {
@@ -338,7 +337,7 @@ class RecipeController {
338 337
339 this.recipe.darkModeHandler(true, handlerConfig); 338 this.recipe.darkModeHandler(true, handlerConfig);
340 } else if (darkModeStyleExists(this.settings.service.recipe.path)) { 339 } else if (darkModeStyleExists(this.settings.service.recipe.path)) {
341 console.log('Injecting darkmode from recipe'); 340 debug('Injecting darkmode from recipe');
342 injectDarkModeStyle(this.settings.service.recipe.path); 341 injectDarkModeStyle(this.settings.service.recipe.path);
343 342
344 // Make sure universal dark mode is disabled 343 // Make sure universal dark mode is disabled
@@ -348,7 +347,7 @@ class RecipeController {
348 this.settings.app.universalDarkMode && 347 this.settings.app.universalDarkMode &&
349 !ignoreList.includes(window.location.host) 348 !ignoreList.includes(window.location.host)
350 ) { 349 ) {
351 console.log('Injecting Dark Reader'); 350 debug('Injecting Dark Reader');
352 351
353 // Use Dark Reader instead 352 // Use Dark Reader instead
354 const { brightness, contrast, sepia } = 353 const { brightness, contrast, sepia } =
@@ -362,8 +361,8 @@ class RecipeController {
362 this.universalDarkModeInjected = true; 361 this.universalDarkModeInjected = true;
363 } 362 }
364 } else { 363 } else {
365 console.log('Remove dark mode'); 364 debug('Remove dark mode');
366 console.log('DarkMode disabled - removing remaining styles'); 365 debug('DarkMode disabled - removing remaining styles');
367 366
368 if (this.recipe && this.recipe.darkModeHandler) { 367 if (this.recipe && this.recipe.darkModeHandler) {
369 // Remove other dark mode styles if they were already loaded 368 // Remove other dark mode styles if they were already loaded
@@ -375,10 +374,10 @@ class RecipeController {
375 374
376 this.recipe.darkModeHandler(false, handlerConfig); 375 this.recipe.darkModeHandler(false, handlerConfig);
377 } else if (isDarkModeStyleInjected()) { 376 } else if (isDarkModeStyleInjected()) {
378 console.log('Removing injected darkmode from recipe'); 377 debug('Removing injected darkmode from recipe');
379 removeDarkModeStyle(); 378 removeDarkModeStyle();
380 } else { 379 } else {
381 console.log('Removing Dark Reader'); 380 debug('Removing Dark Reader');
382 381
383 disableDarkMode(); 382 disableDarkMode();
384 this.universalDarkModeInjected = false; 383 this.universalDarkModeInjected = false;
@@ -406,7 +405,7 @@ class RecipeController {
406 } 405 }
407 406
408 serviceIdEcho(event) { 407 serviceIdEcho(event) {
409 console.log('Received a service echo ping'); 408 debug('Received a service echo ping');
410 event.sender.send('service-id', this.settings.service.id); 409 event.sender.send('service-id', this.settings.service.id);
411 } 410 }
412 411
@@ -428,7 +427,7 @@ class RecipeController {
428 // Force a minimum length to get better detection results 427 // Force a minimum length to get better detection results
429 if (value.length < 25) return; 428 if (value.length < 25) return;
430 429
431 console.log('Detecting language for', value); 430 debug('Detecting language for', value);
432 const locale = await ipcRenderer.invoke('detect-language', { 431 const locale = await ipcRenderer.invoke('detect-language', {
433 sample: value, 432 sample: value,
434 }); 433 });
@@ -438,7 +437,7 @@ class RecipeController {
438 437
439 const spellcheckerLocale = 438 const spellcheckerLocale =
440 getSpellcheckerLocaleByFuzzyIdentifier(locale); 439 getSpellcheckerLocaleByFuzzyIdentifier(locale);
441 console.log( 440 debug(
442 'Language detected reliably, setting spellchecker language to', 441 'Language detected reliably, setting spellchecker language to',
443 spellcheckerLocale, 442 spellcheckerLocale,
444 ); 443 );