From 904eb333103e0d3b9f6c23ca49d76d403f43ded3 Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Sat, 5 Jan 2019 21:12:42 +0100 Subject: Merge --- src/helpers/password-helpers.js | 4 +-- src/index.js | 80 +++++++++++++++++++++++++++++------------ src/models/Service.js | 22 ++++++++---- src/stores/ServicesStore.js | 16 ++++++--- src/stores/UIStore.js | 7 ++-- src/webview/recipe.js | 17 ++++----- 6 files changed, 99 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/helpers/password-helpers.js b/src/helpers/password-helpers.js index 7aacaa4d0..cf461e4f7 100644 --- a/src/helpers/password-helpers.js +++ b/src/helpers/password-helpers.js @@ -1,7 +1,7 @@ -import { SHA256 } from 'jshashes'; +import crypto from 'crypto'; export function hash(password) { - return new SHA256().b64(password); + return crypto.createHash('sha256').update(password).digest('base64'); } export function scorePassword(password) { diff --git a/src/index.js b/src/index.js index 830166dcf..75da4ff88 100644 --- a/src/index.js +++ b/src/index.js @@ -46,35 +46,69 @@ if (isWindows) { } // Force single window -const isSecondInstance = app.makeSingleInstance((argv) => { - if (mainWindow) { - if (mainWindow.isMinimized()) mainWindow.restore(); - mainWindow.focus(); +const gotTheLock = app.requestSingleInstanceLock(); +if (!gotTheLock) { + app.quit(); +} else { + app.on('second-instance', (event, argv) => { + // Someone tried to run a second instance, we should focus our window. + if (mainWindow) { + if (mainWindow.isMinimized()) mainWindow.restore(); + mainWindow.focus(); - if (process.platform === 'win32') { - // Keep only command line / deep linked arguments - const url = argv.slice(1); + if (isWindows) { + // Keep only command line / deep linked arguments + const url = argv.slice(1); - if (url) { - handleDeepLink(mainWindow, url.toString()); + if (url) { + handleDeepLink(mainWindow, url.toString()); + } } - } - } - if (argv.includes('--reset-window')) { - // Needs to be delayed to not interfere with mainWindow.restore(); - setTimeout(() => { - debug('Resetting windows via Task'); - mainWindow.setPosition(DEFAULT_WINDOW_OPTIONS.x + 100, DEFAULT_WINDOW_OPTIONS.y + 100); - mainWindow.setSize(DEFAULT_WINDOW_OPTIONS.width, DEFAULT_WINDOW_OPTIONS.height); - }, 1); - } -}); + if (argv.includes('--reset-window')) { + // Needs to be delayed to not interfere with mainWindow.restore(); + setTimeout(() => { + debug('Resetting windows via Task'); + mainWindow.setPosition(DEFAULT_WINDOW_OPTIONS.x + 100, DEFAULT_WINDOW_OPTIONS.y + 100); + mainWindow.setSize(DEFAULT_WINDOW_OPTIONS.width, DEFAULT_WINDOW_OPTIONS.height); + }, 1); + } + } + }); -if (isSecondInstance) { - console.log('An instance of Franz is already running. Exiting...'); - app.exit(); + // Create myWindow, load the rest of the app, etc... + app.on('ready', () => { + }); } +// const isSecondInstance = app.makeSingleInstance((argv) => { +// if (mainWindow) { +// if (mainWindow.isMinimized()) mainWindow.restore(); +// mainWindow.focus(); + +// if (process.platform === 'win32') { +// // Keep only command line / deep linked arguments +// const url = argv.slice(1); + +// if (url) { +// handleDeepLink(mainWindow, url.toString()); +// } +// } +// } + +// if (argv.includes('--reset-window')) { +// // Needs to be delayed to not interfere with mainWindow.restore(); +// setTimeout(() => { +// debug('Resetting windows via Task'); +// mainWindow.setPosition(DEFAULT_WINDOW_OPTIONS.x + 100, DEFAULT_WINDOW_OPTIONS.y + 100); +// mainWindow.setSize(DEFAULT_WINDOW_OPTIONS.width, DEFAULT_WINDOW_OPTIONS.height); +// }, 1); +// } +// }); + +// if (isSecondInstance) { +// console.log('An instance of Franz is already running. Exiting...'); +// app.exit(); +// } // Fix Unity indicator issue // https://github.com/electron/electron/issues/9046 diff --git a/src/models/Service.js b/src/models/Service.js index 4cc6102ff..7a955a6d9 100644 --- a/src/models/Service.js +++ b/src/models/Service.js @@ -114,6 +114,13 @@ export default class Service { }); } + @computed get shareWithWebview() { + return { + spellcheckerLanguage: this.spellcheckerLanguage, + isDarkModeEnabled: this.isDarkModeEnabled, + }; + } + @computed get url() { if (this.recipe.hasCustomUrl && this.customUrl) { let url; @@ -162,14 +169,14 @@ export default class Service { return userAgent; } - initializeWebViewEvents(store) { - this.webview.addEventListener('ipc-message', e => store.actions.service.handleIPCMessage({ + initializeWebViewEvents({ handleIPCMessage, openWindow }) { + this.webview.addEventListener('ipc-message', e => handleIPCMessage({ serviceId: this.id, channel: e.channel, args: e.args, })); - this.webview.addEventListener('new-window', (event, url, frameName, options) => store.actions.service.openWindow({ + this.webview.addEventListener('new-window', (event, url, frameName, options) => openWindow({ event, url, frameName, @@ -182,17 +189,20 @@ export default class Service { this.isError = false; }); - this.webview.addEventListener('did-frame-finish-load', () => { + const didLoad = () => { this.isLoading = false; if (!this.isError) { this.isFirstLoad = false; } - }); + }; + + this.webview.addEventListener('did-frame-finish-load', didLoad.bind(this)); + this.webview.addEventListener('did-navigate', didLoad.bind(this)); this.webview.addEventListener('did-fail-load', (event) => { debug('Service failed to load', this.name, event); - if (event.isMainFrame) { + if (event.isMainFrame && event.errorCode !== -3) { this.isError = true; this.errorMessage = event.errorDescription; this.isLoading = false; diff --git a/src/stores/ServicesStore.js b/src/stores/ServicesStore.js index 5b70ca271..84f84891a 100644 --- a/src/stores/ServicesStore.js +++ b/src/stores/ServicesStore.js @@ -1,5 +1,8 @@ import { - action, reaction, computed, observable, + action, + reaction, + computed, + observable, } from 'mobx'; import { debounce, remove } from 'lodash'; @@ -323,7 +326,11 @@ export default class ServicesStore extends Store { service.webview = webview; if (!service.isAttached) { - service.initializeWebViewEvents(this); + debug('Webview is not attached, initializing'); + service.initializeWebViewEvents({ + handleIPCMessage: this.actions.service.handleIPCMessage, + openWindow: this.actions.service.openWindow, + }); service.initializeWebViewListener(); } @@ -644,14 +651,15 @@ export default class ServicesStore extends Store { const service = this.one(serviceId); if (service.webview) { - service.webview.send('initialize-recipe', service); + debug('Initialize recipe', service.recipe.id, service.name); + service.webview.send('initialize-recipe', service.shareWithWebview, service.recipe); } } _initRecipePolling(serviceId) { const service = this.one(serviceId); - const delay = 1000; + const delay = 2000; if (service) { if (service.timer !== null) { diff --git a/src/stores/UIStore.js b/src/stores/UIStore.js index d37ebe4c7..188c2fc44 100644 --- a/src/stores/UIStore.js +++ b/src/stores/UIStore.js @@ -1,8 +1,7 @@ import { action, observable, computed } from 'mobx'; +import theme from '@meetfranz/theme'; import Store from './lib/Store'; -import * as themeDefault from '../theme/default'; -import * as themeDark from '../theme/dark'; export default class UIStore extends Store { @observable showServicesUpdatedInfoBar = false; @@ -24,10 +23,10 @@ export default class UIStore extends Store { @computed get theme() { if (this.stores.settings.all.app.darkMode) { - return Object.assign({}, themeDefault, themeDark); + return theme('dark'); } - return themeDefault; + return theme('default'); } // Actions diff --git a/src/webview/recipe.js b/src/webview/recipe.js index 944883899..9aa89ce01 100644 --- a/src/webview/recipe.js +++ b/src/webview/recipe.js @@ -42,9 +42,9 @@ class RecipeController { async initialize() { Object.keys(this.ipcEvents).forEach((channel) => { - ipcRenderer.on(channel, (event, data) => { - debug('Received IPC event for channel', channel, 'with', data); - this[this.ipcEvents[channel]](event, data); + ipcRenderer.on(channel, (...args) => { + debug('Received IPC event for channel', channel, 'with', ...args); + this[this.ipcEvents[channel]](...args); }); }); @@ -62,17 +62,18 @@ class RecipeController { autorun(() => this.update()); } - loadRecipeModule(event, data) { + loadRecipeModule(event, config, recipe) { debug('loadRecipeModule'); - const modulePath = path.join(data.recipe.path, 'webview.js'); + const modulePath = path.join(recipe.path, 'webview.js'); + debug('module path', modulePath); // Delete module from cache delete require.cache[require.resolve(modulePath)]; try { // eslint-disable-next-line - require(modulePath)(new RecipeWebview(), data); - debug('Initialize Recipe', data); + require(modulePath)(new RecipeWebview(), {...config, recipe,}); + debug('Initialize Recipe', config, recipe); - this.settings.service = data; + this.settings.service = config; } catch (err) { console.error('Recipe initialization failed', err); } -- cgit v1.2.3-54-g00ecf