diff options
author | mhatvan <markus_hatvan@aon.at> | 2021-08-05 08:58:28 +0200 |
---|---|---|
committer | Vijay Raghavan Aravamudhan <vraravam@users.noreply.github.com> | 2021-08-05 08:05:49 +0000 |
commit | 305c2c5ecb49a1349be2efb1ef557d61da9a64dc (patch) | |
tree | 19a05ed4ddfc1e5b5bb7fc3ecc071ad562da820f /src/index.js | |
parent | refactor: minor refactoring: solve name-clash of env vars vs vars in the program (diff) | |
download | ferdium-app-305c2c5ecb49a1349be2efb1ef557d61da9a64dc.tar.gz ferdium-app-305c2c5ecb49a1349be2efb1ef557d61da9a64dc.tar.zst ferdium-app-305c2c5ecb49a1349be2efb1ef557d61da9a64dc.zip |
refactor: general code improvements
- replace deprecated fs.exists with fs.existsSync
- replace console.log with debug
- replace hardcoded FERDI_VERSION in start.js with dynamic one from package.json
- correct JSDoc annotations in Handler.js
- simplify macOSPermissions.js
- updates to various eslint rules
- add FileReader to known globals
Diffstat (limited to 'src/index.js')
-rw-r--r-- | src/index.js | 156 |
1 files changed, 100 insertions, 56 deletions
diff --git a/src/index.js b/src/index.js index 63e6e3d0f..7c52fa1b3 100644 --- a/src/index.js +++ b/src/index.js | |||
@@ -1,12 +1,6 @@ | |||
1 | /* eslint-disable import/first */ | 1 | /* eslint-disable import/first */ |
2 | 2 | ||
3 | import { | 3 | import { app, BrowserWindow, shell, ipcMain, session } from 'electron'; |
4 | app, | ||
5 | BrowserWindow, | ||
6 | shell, | ||
7 | ipcMain, | ||
8 | session, | ||
9 | } from 'electron'; | ||
10 | 4 | ||
11 | import fs from 'fs-extra'; | 5 | import fs from 'fs-extra'; |
12 | import path from 'path'; | 6 | import path from 'path'; |
@@ -34,7 +28,7 @@ import DBus from './lib/DBus'; | |||
34 | import Settings from './electron/Settings'; | 28 | import Settings from './electron/Settings'; |
35 | import handleDeepLink from './electron/deepLinking'; | 29 | import handleDeepLink from './electron/deepLinking'; |
36 | import { isPositionValid } from './electron/windowUtils'; | 30 | import { isPositionValid } from './electron/windowUtils'; |
37 | import { appId } from './package.json'; // eslint-disable-line import/no-unresolved | 31 | import { appId } from '../package.json'; |
38 | import './electron/exception'; | 32 | import './electron/exception'; |
39 | 33 | ||
40 | import { asarPath } from './helpers/asar-helpers'; | 34 | import { asarPath } from './helpers/asar-helpers'; |
@@ -90,7 +84,9 @@ if (settings.get('sentry')) { | |||
90 | const liftSingleInstanceLock = settings.get('liftSingleInstanceLock') || false; | 84 | const liftSingleInstanceLock = settings.get('liftSingleInstanceLock') || false; |
91 | 85 | ||
92 | // Force single window | 86 | // Force single window |
93 | const gotTheLock = liftSingleInstanceLock ? true : app.requestSingleInstanceLock(); | 87 | const gotTheLock = liftSingleInstanceLock |
88 | ? true | ||
89 | : app.requestSingleInstanceLock(); | ||
94 | if (!gotTheLock) { | 90 | if (!gotTheLock) { |
95 | app.quit(); | 91 | app.quit(); |
96 | } else { | 92 | } else { |
@@ -106,7 +102,7 @@ if (!gotTheLock) { | |||
106 | mainWindow.focus(); | 102 | mainWindow.focus(); |
107 | 103 | ||
108 | if (isWindows) { | 104 | if (isWindows) { |
109 | onDidLoad((window) => { | 105 | onDidLoad(window => { |
110 | // Keep only command line / deep linked arguments | 106 | // Keep only command line / deep linked arguments |
111 | const url = argv.slice(1); | 107 | const url = argv.slice(1); |
112 | if (url) { | 108 | if (url) { |
@@ -117,8 +113,14 @@ if (!gotTheLock) { | |||
117 | // Needs to be delayed to not interfere with mainWindow.restore(); | 113 | // Needs to be delayed to not interfere with mainWindow.restore(); |
118 | setTimeout(() => { | 114 | setTimeout(() => { |
119 | debug('Resetting windows via Task'); | 115 | debug('Resetting windows via Task'); |
120 | window.setPosition(DEFAULT_WINDOW_OPTIONS.x + 100, DEFAULT_WINDOW_OPTIONS.y + 100); | 116 | window.setPosition( |
121 | window.setSize(DEFAULT_WINDOW_OPTIONS.width, DEFAULT_WINDOW_OPTIONS.height); | 117 | DEFAULT_WINDOW_OPTIONS.x + 100, |
118 | DEFAULT_WINDOW_OPTIONS.y + 100, | ||
119 | ); | ||
120 | window.setSize( | ||
121 | DEFAULT_WINDOW_OPTIONS.width, | ||
122 | DEFAULT_WINDOW_OPTIONS.height, | ||
123 | ); | ||
122 | }, 1); | 124 | }, 1); |
123 | } else if (argv.includes('--quit')) { | 125 | } else if (argv.includes('--quit')) { |
124 | // Needs to be delayed to not interfere with mainWindow.restore(); | 126 | // Needs to be delayed to not interfere with mainWindow.restore(); |
@@ -135,7 +137,10 @@ if (!gotTheLock) { | |||
135 | 137 | ||
136 | // Fix Unity indicator issue | 138 | // Fix Unity indicator issue |
137 | // https://github.com/electron/electron/issues/9046 | 139 | // https://github.com/electron/electron/issues/9046 |
138 | if (isLinux && ['Pantheon', 'Unity:Unity7'].indexOf(process.env.XDG_CURRENT_DESKTOP) !== -1) { | 140 | if ( |
141 | isLinux && | ||
142 | ['Pantheon', 'Unity:Unity7'].indexOf(process.env.XDG_CURRENT_DESKTOP) !== -1 | ||
143 | ) { | ||
139 | process.env.XDG_CURRENT_DESKTOP = 'Unity'; | 144 | process.env.XDG_CURRENT_DESKTOP = 'Unity'; |
140 | } | 145 | } |
141 | 146 | ||
@@ -169,7 +174,9 @@ const createWindow = () => { | |||
169 | } | 174 | } |
170 | 175 | ||
171 | // Create the browser window. | 176 | // Create the browser window. |
172 | const backgroundColor = settings.get('darkMode') ? '#1E1E1E' : settings.get('accentColor'); | 177 | const backgroundColor = settings.get('darkMode') |
178 | ? '#1E1E1E' | ||
179 | : settings.get('accentColor'); | ||
173 | 180 | ||
174 | mainWindow = new BrowserWindow({ | 181 | mainWindow = new BrowserWindow({ |
175 | x: posX, | 182 | x: posX, |
@@ -193,7 +200,7 @@ const createWindow = () => { | |||
193 | 200 | ||
194 | app.on('web-contents-created', (e, contents) => { | 201 | app.on('web-contents-created', (e, contents) => { |
195 | if (contents.getType() === 'webview') { | 202 | if (contents.getType() === 'webview') { |
196 | contents.on('new-window', (event) => { | 203 | contents.on('new-window', event => { |
197 | event.preventDefault(); | 204 | event.preventDefault(); |
198 | }); | 205 | }); |
199 | } | 206 | } |
@@ -242,7 +249,7 @@ const createWindow = () => { | |||
242 | 249 | ||
243 | // Windows deep linking handling on app launch | 250 | // Windows deep linking handling on app launch |
244 | if (isWindows) { | 251 | if (isWindows) { |
245 | onDidLoad((window) => { | 252 | onDidLoad(window => { |
246 | const url = process.argv.slice(1); | 253 | const url = process.argv.slice(1); |
247 | if (url) { | 254 | if (url) { |
248 | handleDeepLink(window, url.toString()); | 255 | handleDeepLink(window, url.toString()); |
@@ -251,12 +258,16 @@ const createWindow = () => { | |||
251 | } | 258 | } |
252 | 259 | ||
253 | // Emitted when the window is closed. | 260 | // Emitted when the window is closed. |
254 | mainWindow.on('close', (e) => { | 261 | mainWindow.on('close', e => { |
255 | debug('Window: close window'); | 262 | debug('Window: close window'); |
256 | // Dereference the window object, usually you would store windows | 263 | // Dereference the window object, usually you would store windows |
257 | // in an array if your app supports multi windows, this is the time | 264 | // in an array if your app supports multi windows, this is the time |
258 | // when you should delete the corresponding element. | 265 | // when you should delete the corresponding element. |
259 | if (!willQuitApp && (settings.get('runInBackground') === undefined || settings.get('runInBackground'))) { | 266 | if ( |
267 | !willQuitApp && | ||
268 | (settings.get('runInBackground') === undefined || | ||
269 | settings.get('runInBackground')) | ||
270 | ) { | ||
260 | e.preventDefault(); | 271 | e.preventDefault(); |
261 | if (isWindows) { | 272 | if (isWindows) { |
262 | debug('Window: minimize'); | 273 | debug('Window: minimize'); |
@@ -315,7 +326,7 @@ const createWindow = () => { | |||
315 | 326 | ||
316 | if (isMac) { | 327 | if (isMac) { |
317 | // eslint-disable-next-line global-require | 328 | // eslint-disable-next-line global-require |
318 | const { default: askFormacOSPermissions } = require('./electron/macOSPermissions'); | 329 | const { askFormacOSPermissions } = require('./electron/macOSPermissions'); |
319 | setTimeout(() => askFormacOSPermissions(mainWindow), ms('30s')); | 330 | setTimeout(() => askFormacOSPermissions(mainWindow), ms('30s')); |
320 | } | 331 | } |
321 | 332 | ||
@@ -351,15 +362,24 @@ const createWindow = () => { | |||
351 | const argv = require('minimist')(process.argv.slice(1)); | 362 | const argv = require('minimist')(process.argv.slice(1)); |
352 | 363 | ||
353 | if (argv['auth-server-whitelist']) { | 364 | if (argv['auth-server-whitelist']) { |
354 | app.commandLine.appendSwitch('auth-server-whitelist', argv['auth-server-whitelist']); | 365 | app.commandLine.appendSwitch( |
366 | 'auth-server-whitelist', | ||
367 | argv['auth-server-whitelist'], | ||
368 | ); | ||
355 | } | 369 | } |
356 | if (argv['auth-negotiate-delegate-whitelist']) { | 370 | if (argv['auth-negotiate-delegate-whitelist']) { |
357 | app.commandLine.appendSwitch('auth-negotiate-delegate-whitelist', argv['auth-negotiate-delegate-whitelist']); | 371 | app.commandLine.appendSwitch( |
372 | 'auth-negotiate-delegate-whitelist', | ||
373 | argv['auth-negotiate-delegate-whitelist'], | ||
374 | ); | ||
358 | } | 375 | } |
359 | 376 | ||
360 | // Disable Chromium's poor MPRIS implementation | 377 | // Disable Chromium's poor MPRIS implementation |
361 | // and apply workaround for https://github.com/electron/electron/pull/26432 | 378 | // and apply workaround for https://github.com/electron/electron/pull/26432 |
362 | app.commandLine.appendSwitch('disable-features', 'HardwareMediaKeyHandling,MediaSessionService,CrossOriginOpenerPolicy'); | 379 | app.commandLine.appendSwitch( |
380 | 'disable-features', | ||
381 | 'HardwareMediaKeyHandling,MediaSessionService,CrossOriginOpenerPolicy', | ||
382 | ); | ||
363 | 383 | ||
364 | // This method will be called when Electron has finished | 384 | // This method will be called when Electron has finished |
365 | // initialization and is ready to create browser windows. | 385 | // initialization and is ready to create browser windows. |
@@ -376,19 +396,29 @@ app.on('ready', () => { | |||
376 | } | 396 | } |
377 | 397 | ||
378 | if (isWindows) { | 398 | if (isWindows) { |
379 | app.setUserTasks([{ | 399 | app.setUserTasks([ |
380 | program: process.execPath, | 400 | { |
381 | arguments: `${isDevMode ? `${__dirname} ` : ''}--reset-window`, | 401 | program: process.execPath, |
382 | iconPath: asarPath(path.join(isDevMode ? `${__dirname}../src/` : __dirname, 'assets/images/taskbar/win32/display.ico')), | 402 | arguments: `${isDevMode ? `${__dirname} ` : ''}--reset-window`, |
383 | iconIndex: 0, | 403 | iconPath: asarPath( |
384 | title: 'Move Ferdi to Current Display', | 404 | path.join( |
385 | description: 'Restore the position and size of Ferdi', | 405 | isDevMode ? `${__dirname}../src/` : __dirname, |
386 | }, { | 406 | 'assets/images/taskbar/win32/display.ico', |
387 | program: process.execPath, | 407 | ), |
388 | arguments: `${isDevMode ? `${__dirname} ` : ''}--quit`, | 408 | ), |
389 | iconIndex: 0, | 409 | iconIndex: 0, |
390 | title: 'Quit Ferdi', | 410 | title: 'Move Ferdi to Current Display', |
391 | }]); | 411 | description: 'Restore the position and size of Ferdi', |
412 | }, | ||
413 | { | ||
414 | program: process.execPath, | ||
415 | arguments: `${isDevMode ? `${__dirname} ` : ''}--quit`, | ||
416 | iconIndex: 0, | ||
417 | iconPath: null, | ||
418 | title: 'Quit Ferdi', | ||
419 | description: null, | ||
420 | }, | ||
421 | ]); | ||
392 | } | 422 | } |
393 | 423 | ||
394 | createWindow(); | 424 | createWindow(); |
@@ -420,27 +450,35 @@ ipcMain.on('feature-basic-auth-credentials', (e, { user, password }) => { | |||
420 | 450 | ||
421 | ipcMain.on('open-browser-window', (e, { url, serviceId }) => { | 451 | ipcMain.on('open-browser-window', (e, { url, serviceId }) => { |
422 | const serviceSession = session.fromPartition(`persist:service-${serviceId}`); | 452 | const serviceSession = session.fromPartition(`persist:service-${serviceId}`); |
423 | const child = new BrowserWindow({ parent: mainWindow, webPreferences: { session: serviceSession } }); | 453 | const child = new BrowserWindow({ |
454 | parent: mainWindow, | ||
455 | webPreferences: { session: serviceSession }, | ||
456 | }); | ||
424 | child.show(); | 457 | child.show(); |
425 | child.loadURL(url); | 458 | child.loadURL(url); |
426 | debug('Received open-browser-window', url); | 459 | debug('Received open-browser-window', url); |
427 | }); | 460 | }); |
428 | 461 | ||
429 | ipcMain.on('modifyRequestHeaders', (e, { modifiedRequestHeaders, serviceId }) => { | 462 | ipcMain.on( |
430 | debug('Received modifyRequestHeaders', modifiedRequestHeaders, serviceId); | 463 | 'modifyRequestHeaders', |
431 | modifiedRequestHeaders.forEach((headerFilterSet) => { | 464 | (e, { modifiedRequestHeaders, serviceId }) => { |
432 | const { headers, requestFilters } = headerFilterSet; | 465 | debug('Received modifyRequestHeaders', modifiedRequestHeaders, serviceId); |
433 | session.fromPartition(`persist:service-${serviceId}`).webRequest.onBeforeSendHeaders(requestFilters, (details, callback) => { | 466 | modifiedRequestHeaders.forEach(headerFilterSet => { |
434 | for (const key in headers) { | 467 | const { headers, requestFilters } = headerFilterSet; |
435 | if (Object.prototype.hasOwnProperty.call(headers, key)) { | 468 | session |
436 | const value = headers[key]; | 469 | .fromPartition(`persist:service-${serviceId}`) |
437 | details.requestHeaders[key] = value; | 470 | .webRequest.onBeforeSendHeaders(requestFilters, (details, callback) => { |
438 | } | 471 | for (const key in headers) { |
439 | } | 472 | if (Object.prototype.hasOwnProperty.call(headers, key)) { |
440 | callback({ requestHeaders: details.requestHeaders }); | 473 | const value = headers[key]; |
474 | details.requestHeaders[key] = value; | ||
475 | } | ||
476 | } | ||
477 | callback({ requestHeaders: details.requestHeaders }); | ||
478 | }); | ||
441 | }); | 479 | }); |
442 | }); | 480 | }, |
443 | }); | 481 | ); |
444 | 482 | ||
445 | ipcMain.on('feature-basic-auth-cancel', () => { | 483 | ipcMain.on('feature-basic-auth-cancel', () => { |
446 | debug('Cancel basic auth'); | 484 | debug('Cancel basic auth'); |
@@ -453,7 +491,7 @@ ipcMain.on('feature-basic-auth-cancel', () => { | |||
453 | 491 | ||
454 | ipcMain.on('find-in-page', (e, text, options) => { | 492 | ipcMain.on('find-in-page', (e, text, options) => { |
455 | const { sender: webContents } = e; | 493 | const { sender: webContents } = e; |
456 | if (webContents !== mainWindow.webContents && typeof (text) === 'string') { | 494 | if (webContents !== mainWindow.webContents && typeof text === 'string') { |
457 | const sanitizedOptions = {}; | 495 | const sanitizedOptions = {}; |
458 | for (const option of ['forward', 'findNext', 'matchCase']) { | 496 | for (const option of ['forward', 'findNext', 'matchCase']) { |
459 | if (option in options) { | 497 | if (option in options) { |
@@ -471,7 +509,11 @@ ipcMain.on('find-in-page', (e, text, options) => { | |||
471 | ipcMain.on('stop-find-in-page', (e, action) => { | 509 | ipcMain.on('stop-find-in-page', (e, action) => { |
472 | const { sender: webContents } = e; | 510 | const { sender: webContents } = e; |
473 | if (webContents !== mainWindow.webContents) { | 511 | if (webContents !== mainWindow.webContents) { |
474 | const validActions = ['clearSelection', 'keepSelection', 'activateSelection']; | 512 | const validActions = [ |
513 | 'clearSelection', | ||
514 | 'keepSelection', | ||
515 | 'activateSelection', | ||
516 | ]; | ||
475 | if (validActions.includes(action)) { | 517 | if (validActions.includes(action)) { |
476 | webContents.stopFindInPage(action); | 518 | webContents.stopFindInPage(action); |
477 | } | 519 | } |
@@ -483,12 +525,14 @@ ipcMain.on('stop-find-in-page', (e, action) => { | |||
483 | app.on('window-all-closed', () => { | 525 | app.on('window-all-closed', () => { |
484 | // On OS X it is common for applications and their menu bar | 526 | // On OS X it is common for applications and their menu bar |
485 | // to stay active until the user quits explicitly with Cmd + Q | 527 | // to stay active until the user quits explicitly with Cmd + Q |
486 | if (settings.get('runInBackground') === undefined | 528 | if ( |
487 | || settings.get('runInBackground')) { | 529 | settings.get('runInBackground') === undefined || |
530 | settings.get('runInBackground') | ||
531 | ) { | ||
488 | debug('Window: all windows closed, quit app'); | 532 | debug('Window: all windows closed, quit app'); |
489 | app.quit(); | 533 | app.quit(); |
490 | } else { | 534 | } else { |
491 | debug('Window: don\'t quit app'); | 535 | debug("Window: don't quit app"); |
492 | } | 536 | } |
493 | }); | 537 | }); |
494 | 538 | ||
@@ -517,7 +561,7 @@ app.on('will-finish-launching', () => { | |||
517 | app.on('open-url', (event, url) => { | 561 | app.on('open-url', (event, url) => { |
518 | event.preventDefault(); | 562 | event.preventDefault(); |
519 | 563 | ||
520 | onDidLoad((window) => { | 564 | onDidLoad(window => { |
521 | debug('open-url event', url); | 565 | debug('open-url event', url); |
522 | handleDeepLink(window, url); | 566 | handleDeepLink(window, url); |
523 | }); | 567 | }); |