diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-04-23 01:59:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-22 23:59:21 +0000 |
commit | d02644f7c41150709795e57bfd40351b4da35a7b (patch) | |
tree | 2403fb76bd5fae1703f8b55172ffce9e0a5d2bce /src/index.ts | |
parent | Complete tray icons redesign for all platforms (#28) (diff) | |
download | ferdium-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/index.ts')
-rw-r--r-- | src/index.ts | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/src/index.ts b/src/index.ts index 6be4f3f18..ef9d5bd72 100644 --- a/src/index.ts +++ b/src/index.ts | |||
@@ -12,6 +12,7 @@ import { | |||
12 | import { emptyDirSync, ensureFileSync } from 'fs-extra'; | 12 | import { emptyDirSync, ensureFileSync } from 'fs-extra'; |
13 | import { join } from 'path'; | 13 | import { join } from 'path'; |
14 | import windowStateKeeper from 'electron-window-state'; | 14 | import windowStateKeeper from 'electron-window-state'; |
15 | import minimist from 'minimist'; | ||
15 | import ms from 'ms'; | 16 | import ms from 'ms'; |
16 | import { enableWebContents, initializeRemote } from './electron-util'; | 17 | import { enableWebContents, initializeRemote } from './electron-util'; |
17 | import { enforceMacOSAppLocation } from './enforce-macos-app-location'; | 18 | import { enforceMacOSAppLocation } from './enforce-macos-app-location'; |
@@ -44,11 +45,10 @@ import { asarPath } from './helpers/asar-helpers'; | |||
44 | import { openExternalUrl } from './helpers/url-helpers'; | 45 | import { openExternalUrl } from './helpers/url-helpers'; |
45 | import userAgent from './helpers/userAgent-helpers'; | 46 | import userAgent from './helpers/userAgent-helpers'; |
46 | 47 | ||
47 | // TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed | 48 | const debug = require('./preload-safe-debug')('Ferdium:App'); |
48 | // const debug = require('debug')('Ferdium:App'); | ||
49 | 49 | ||
50 | // Globally set useragent to fix user agent override in service workers | 50 | // Globally set useragent to fix user agent override in service workers |
51 | console.log('Set userAgent to', userAgent()); | 51 | debug('Set userAgent to ', userAgent()); |
52 | app.userAgentFallback = userAgent(); | 52 | app.userAgentFallback = userAgent(); |
53 | 53 | ||
54 | // Keep a global reference of the window object, if you don't, the window will | 54 | // Keep a global reference of the window object, if you don't, the window will |
@@ -127,7 +127,7 @@ if (!gotTheLock) { | |||
127 | if (argv.includes('--reset-window')) { | 127 | if (argv.includes('--reset-window')) { |
128 | // Needs to be delayed to not interfere with mainWindow.restore(); | 128 | // Needs to be delayed to not interfere with mainWindow.restore(); |
129 | setTimeout(() => { | 129 | setTimeout(() => { |
130 | console.log('Resetting windows via Task'); | 130 | debug('Resetting windows via Task'); |
131 | window.setPosition( | 131 | window.setPosition( |
132 | DEFAULT_WINDOW_OPTIONS.x + 100, | 132 | DEFAULT_WINDOW_OPTIONS.x + 100, |
133 | DEFAULT_WINDOW_OPTIONS.y + 100, | 133 | DEFAULT_WINDOW_OPTIONS.y + 100, |
@@ -140,7 +140,7 @@ if (!gotTheLock) { | |||
140 | } else if (argv.includes('--quit')) { | 140 | } else if (argv.includes('--quit')) { |
141 | // Needs to be delayed to not interfere with mainWindow.restore(); | 141 | // Needs to be delayed to not interfere with mainWindow.restore(); |
142 | setTimeout(() => { | 142 | setTimeout(() => { |
143 | console.log('Quitting Ferdium via Task'); | 143 | debug('Quitting Ferdium via Task'); |
144 | app.quit(); | 144 | app.quit(); |
145 | }, 1); | 145 | }, 1); |
146 | } | 146 | } |
@@ -162,7 +162,7 @@ if ( | |||
162 | 162 | ||
163 | // Disable GPU acceleration | 163 | // Disable GPU acceleration |
164 | if (!retrieveSettingValue('enableGPUAcceleration', false)) { | 164 | if (!retrieveSettingValue('enableGPUAcceleration', false)) { |
165 | console.log('Disable GPU Acceleration'); | 165 | debug('Disable GPU Acceleration'); |
166 | app.disableHardwareAcceleration(); | 166 | app.disableHardwareAcceleration(); |
167 | } | 167 | } |
168 | 168 | ||
@@ -184,7 +184,7 @@ const createWindow = () => { | |||
184 | let posY = mainWindowState.y || DEFAULT_WINDOW_OPTIONS.y; | 184 | let posY = mainWindowState.y || DEFAULT_WINDOW_OPTIONS.y; |
185 | 185 | ||
186 | if (!isPositionValid({ x: posX, y: posY })) { | 186 | if (!isPositionValid({ x: posX, y: posY })) { |
187 | console.log('Window is out of screen bounds, resetting window'); | 187 | debug('Window is out of screen bounds, resetting window'); |
188 | posX = DEFAULT_WINDOW_OPTIONS.x; | 188 | posX = DEFAULT_WINDOW_OPTIONS.x; |
189 | posY = DEFAULT_WINDOW_OPTIONS.y; | 189 | posY = DEFAULT_WINDOW_OPTIONS.y; |
190 | } | 190 | } |
@@ -285,7 +285,7 @@ const createWindow = () => { | |||
285 | 285 | ||
286 | // Emitted when the window is closed. | 286 | // Emitted when the window is closed. |
287 | mainWindow.on('close', e => { | 287 | mainWindow.on('close', e => { |
288 | console.log('Window: close window'); | 288 | debug('Window: close window'); |
289 | // Dereference the window object, usually you would store windows | 289 | // Dereference the window object, usually you would store windows |
290 | // in an array if your app supports multi windows, this is the time | 290 | // in an array if your app supports multi windows, this is the time |
291 | // when you should delete the corresponding element. | 291 | // when you should delete the corresponding element. |
@@ -298,7 +298,7 @@ const createWindow = () => { | |||
298 | ) { | 298 | ) { |
299 | e.preventDefault(); | 299 | e.preventDefault(); |
300 | if (isWindows) { | 300 | if (isWindows) { |
301 | console.log('Window: minimize'); | 301 | debug('Window: minimize'); |
302 | mainWindow?.minimize(); | 302 | mainWindow?.minimize(); |
303 | 303 | ||
304 | if ( | 304 | if ( |
@@ -307,16 +307,16 @@ const createWindow = () => { | |||
307 | DEFAULT_APP_SETTINGS.closeToSystemTray, | 307 | DEFAULT_APP_SETTINGS.closeToSystemTray, |
308 | ) | 308 | ) |
309 | ) { | 309 | ) { |
310 | console.log('Skip taskbar: true'); | 310 | debug('Skip taskbar: true'); |
311 | mainWindow?.setSkipTaskbar(true); | 311 | mainWindow?.setSkipTaskbar(true); |
312 | } | 312 | } |
313 | } else if (isMac && mainWindow?.isFullScreen()) { | 313 | } else if (isMac && mainWindow?.isFullScreen()) { |
314 | console.log('Window: leaveFullScreen and hide'); | 314 | debug('Window: leaveFullScreen and hide'); |
315 | mainWindow.once('show', () => mainWindow?.setFullScreen(true)); | 315 | mainWindow.once('show', () => mainWindow?.setFullScreen(true)); |
316 | mainWindow.once('leave-full-screen', () => mainWindow?.hide()); | 316 | mainWindow.once('leave-full-screen', () => mainWindow?.hide()); |
317 | mainWindow.setFullScreen(false); | 317 | mainWindow.setFullScreen(false); |
318 | } else { | 318 | } else { |
319 | console.log('Window: hide'); | 319 | debug('Window: hide'); |
320 | mainWindow?.hide(); | 320 | mainWindow?.hide(); |
321 | } | 321 | } |
322 | } else { | 322 | } else { |
@@ -337,31 +337,31 @@ const createWindow = () => { | |||
337 | DEFAULT_APP_SETTINGS.minimizeToSystemTray, | 337 | DEFAULT_APP_SETTINGS.minimizeToSystemTray, |
338 | ) | 338 | ) |
339 | ) { | 339 | ) { |
340 | console.log('Skip taskbar: true'); | 340 | debug('Skip taskbar: true'); |
341 | mainWindow?.setSkipTaskbar(true); | 341 | mainWindow?.setSkipTaskbar(true); |
342 | trayIcon.show(); | 342 | trayIcon.show(); |
343 | } | 343 | } |
344 | }); | 344 | }); |
345 | 345 | ||
346 | mainWindow.on('maximize', () => { | 346 | mainWindow.on('maximize', () => { |
347 | console.log('Window: maximize'); | 347 | debug('Window: maximize'); |
348 | // @ts-expect-error Property 'isMaximized' does not exist on type 'App'. | 348 | // @ts-expect-error Property 'isMaximized' does not exist on type 'App'. |
349 | app.isMaximized = true; | 349 | app.isMaximized = true; |
350 | }); | 350 | }); |
351 | 351 | ||
352 | mainWindow.on('unmaximize', () => { | 352 | mainWindow.on('unmaximize', () => { |
353 | console.log('Window: unmaximize'); | 353 | debug('Window: unmaximize'); |
354 | // @ts-expect-error Property 'isMaximized' does not exist on type 'App'. | 354 | // @ts-expect-error Property 'isMaximized' does not exist on type 'App'. |
355 | app.isMaximized = false; | 355 | app.isMaximized = false; |
356 | }); | 356 | }); |
357 | 357 | ||
358 | mainWindow.on('restore', () => { | 358 | mainWindow.on('restore', () => { |
359 | console.log('Window: restore'); | 359 | debug('Window: restore'); |
360 | mainWindow?.setSkipTaskbar(false); | 360 | mainWindow?.setSkipTaskbar(false); |
361 | 361 | ||
362 | // @ts-expect-error Property 'wasMaximized' does not exist on type 'App'. | 362 | // @ts-expect-error Property 'wasMaximized' does not exist on type 'App'. |
363 | if (app.wasMaximized) { | 363 | if (app.wasMaximized) { |
364 | console.log('Window: was maximized before, maximize window'); | 364 | debug('Window: was maximized before, maximize window'); |
365 | mainWindow?.maximize(); | 365 | mainWindow?.maximize(); |
366 | } | 366 | } |
367 | 367 | ||
@@ -371,7 +371,7 @@ const createWindow = () => { | |||
371 | DEFAULT_APP_SETTINGS.enableSystemTray, | 371 | DEFAULT_APP_SETTINGS.enableSystemTray, |
372 | ) | 372 | ) |
373 | ) { | 373 | ) { |
374 | console.log('Tray: hiding tray icon'); | 374 | debug('Tray: hiding tray icon'); |
375 | trayIcon.hide(); | 375 | trayIcon.hide(); |
376 | } | 376 | } |
377 | }); | 377 | }); |
@@ -383,7 +383,7 @@ const createWindow = () => { | |||
383 | } | 383 | } |
384 | 384 | ||
385 | mainWindow.on('show', () => { | 385 | mainWindow.on('show', () => { |
386 | console.log('Skip taskbar: true'); | 386 | debug('Skip taskbar: true'); |
387 | mainWindow?.setSkipTaskbar(false); | 387 | mainWindow?.setSkipTaskbar(false); |
388 | }); | 388 | }); |
389 | 389 | ||
@@ -423,7 +423,7 @@ const createWindow = () => { | |||
423 | // used for Kerberos support | 423 | // used for Kerberos support |
424 | // Usage e.g. MACOS | 424 | // Usage e.g. MACOS |
425 | // $ Ferdium.app/Contents/MacOS/Ferdium --auth-server-whitelist *.mydomain.com --auth-negotiate-delegate-whitelist *.mydomain.com | 425 | // $ Ferdium.app/Contents/MacOS/Ferdium --auth-server-whitelist *.mydomain.com --auth-negotiate-delegate-whitelist *.mydomain.com |
426 | const argv = require('minimist')(process.argv.slice(1)); | 426 | const argv = minimist(process.argv.slice(1)); |
427 | 427 | ||
428 | if (argv['auth-server-whitelist']) { | 428 | if (argv['auth-server-whitelist']) { |
429 | app.commandLine.appendSwitch( | 429 | app.commandLine.appendSwitch( |
@@ -500,18 +500,18 @@ let authCallback = noop; | |||
500 | app.on('login', (event, _webContents, _request, authInfo, callback) => { | 500 | app.on('login', (event, _webContents, _request, authInfo, callback) => { |
501 | // @ts-expect-error Type '(username?: string | undefined, password?: string | undefined) => void' is not assignable to type '() => null'. | 501 | // @ts-expect-error Type '(username?: string | undefined, password?: string | undefined) => void' is not assignable to type '() => null'. |
502 | authCallback = callback; | 502 | authCallback = callback; |
503 | console.log('browser login event', authInfo); | 503 | debug('browser login event', authInfo); |
504 | event.preventDefault(); | 504 | event.preventDefault(); |
505 | 505 | ||
506 | if (!authInfo.isProxy && authInfo.scheme === 'basic') { | 506 | if (!authInfo.isProxy && authInfo.scheme === 'basic') { |
507 | console.log('basic auth handler', authInfo); | 507 | debug('basic auth handler', authInfo); |
508 | basicAuthHandler(mainWindow!, authInfo); | 508 | basicAuthHandler(mainWindow!, authInfo); |
509 | } | 509 | } |
510 | }); | 510 | }); |
511 | 511 | ||
512 | // TODO: evaluate if we need to store the authCallback for every service | 512 | // TODO: evaluate if we need to store the authCallback for every service |
513 | ipcMain.on('feature-basic-auth-credentials', (_e, { user, password }) => { | 513 | ipcMain.on('feature-basic-auth-credentials', (_e, { user, password }) => { |
514 | console.log('Received basic auth credentials', user, '********'); | 514 | debug('Received basic auth credentials', user, '********'); |
515 | 515 | ||
516 | // @ts-expect-error Expected 0 arguments, but got 2. | 516 | // @ts-expect-error Expected 0 arguments, but got 2. |
517 | authCallback(user, password); | 517 | authCallback(user, password); |
@@ -530,13 +530,13 @@ ipcMain.on('open-browser-window', (_e, { url, serviceId }) => { | |||
530 | enableWebContents(child.webContents); | 530 | enableWebContents(child.webContents); |
531 | child.show(); | 531 | child.show(); |
532 | child.loadURL(url); | 532 | child.loadURL(url); |
533 | console.log('Received open-browser-window', url); | 533 | debug('Received open-browser-window', url); |
534 | }); | 534 | }); |
535 | 535 | ||
536 | ipcMain.on( | 536 | ipcMain.on( |
537 | 'modifyRequestHeaders', | 537 | 'modifyRequestHeaders', |
538 | (_e, { modifiedRequestHeaders, serviceId }) => { | 538 | (_e, { modifiedRequestHeaders, serviceId }) => { |
539 | console.log( | 539 | debug( |
540 | `Received modifyRequestHeaders ${modifiedRequestHeaders} for serviceId ${serviceId}`, | 540 | `Received modifyRequestHeaders ${modifiedRequestHeaders} for serviceId ${serviceId}`, |
541 | ); | 541 | ); |
542 | for (const headerFilterSet of modifiedRequestHeaders) { | 542 | for (const headerFilterSet of modifiedRequestHeaders) { |
@@ -557,7 +557,7 @@ ipcMain.on( | |||
557 | ); | 557 | ); |
558 | 558 | ||
559 | ipcMain.on('knownCertificateHosts', (_e, { knownHosts, serviceId }) => { | 559 | ipcMain.on('knownCertificateHosts', (_e, { knownHosts, serviceId }) => { |
560 | console.log( | 560 | debug( |
561 | `Received knownCertificateHosts ${knownHosts} for serviceId ${serviceId}`, | 561 | `Received knownCertificateHosts ${knownHosts} for serviceId ${serviceId}`, |
562 | ); | 562 | ); |
563 | session | 563 | session |
@@ -577,7 +577,7 @@ ipcMain.on('knownCertificateHosts', (_e, { knownHosts, serviceId }) => { | |||
577 | }); | 577 | }); |
578 | 578 | ||
579 | ipcMain.on('feature-basic-auth-cancel', () => { | 579 | ipcMain.on('feature-basic-auth-cancel', () => { |
580 | console.log('Cancel basic auth'); | 580 | debug('Cancel basic auth'); |
581 | 581 | ||
582 | // @ts-expect-error Expected 0 arguments, but got 2. | 582 | // @ts-expect-error Expected 0 arguments, but got 2. |
583 | authCallback(null); | 583 | authCallback(null); |
@@ -596,7 +596,7 @@ ipcMain.on('find-in-page', (e, text, options) => { | |||
596 | } | 596 | } |
597 | } | 597 | } |
598 | const requestId = webContents.findInPage(text, sanitizedOptions); | 598 | const requestId = webContents.findInPage(text, sanitizedOptions); |
599 | console.log('Find in page', text, options, requestId); | 599 | debug('Find in page', text, options, requestId); |
600 | e.returnValue = requestId; | 600 | e.returnValue = requestId; |
601 | } else { | 601 | } else { |
602 | e.returnValue = null; | 602 | e.returnValue = null; |
@@ -625,10 +625,10 @@ ipcMain.on('set-spellchecker-locales', (_e, { locale, serviceId }) => { | |||
625 | 625 | ||
626 | const serviceSession = session.fromPartition(`persist:service-${serviceId}`); | 626 | const serviceSession = session.fromPartition(`persist:service-${serviceId}`); |
627 | const [defaultLocale] = serviceSession.getSpellCheckerLanguages(); | 627 | const [defaultLocale] = serviceSession.getSpellCheckerLanguages(); |
628 | console.log(`Spellchecker default locale is: ${defaultLocale}`); | 628 | debug(`Spellchecker default locale is: ${defaultLocale}`); |
629 | 629 | ||
630 | const locales = [locale, defaultLocale, DEFAULT_APP_SETTINGS.fallbackLocale]; | 630 | const locales = [locale, defaultLocale, DEFAULT_APP_SETTINGS.fallbackLocale]; |
631 | console.log(`Setting spellchecker locales to: ${locales}`); | 631 | debug(`Setting spellchecker locales to: ${locales}`); |
632 | serviceSession.setSpellCheckerLanguages(locales); | 632 | serviceSession.setSpellCheckerLanguages(locales); |
633 | }); | 633 | }); |
634 | 634 | ||
@@ -646,10 +646,10 @@ app.on('window-all-closed', () => { | |||
646 | DEFAULT_APP_SETTINGS.runInBackground, | 646 | DEFAULT_APP_SETTINGS.runInBackground, |
647 | ) | 647 | ) |
648 | ) { | 648 | ) { |
649 | console.log('Window: all windows closed, quit app'); | 649 | debug('Window: all windows closed, quit app'); |
650 | app.quit(); | 650 | app.quit(); |
651 | } else { | 651 | } else { |
652 | console.log("Window: don't quit app"); | 652 | debug("Window: don't quit app"); |
653 | } | 653 | } |
654 | }); | 654 | }); |
655 | 655 | ||
@@ -695,7 +695,7 @@ app.on('will-finish-launching', () => { | |||
695 | event.preventDefault(); | 695 | event.preventDefault(); |
696 | 696 | ||
697 | onDidLoad((window: BrowserWindow) => { | 697 | onDidLoad((window: BrowserWindow) => { |
698 | console.log('open-url event', url); | 698 | debug('open-url event', url); |
699 | handleDeepLink(window, url); | 699 | handleDeepLink(window, url); |
700 | }); | 700 | }); |
701 | }); | 701 | }); |