From 8d92592ea6d7aae7b722d5ee28720848f7fbded3 Mon Sep 17 00:00:00 2001 From: Quang Lam Date: Sun, 15 Nov 2020 00:36:16 +0700 Subject: [PATCH] Integrate with Sentry (#253) --- package.json | 1 + public/electron.js | 7 + public/libs/sentry.js | 12 ++ public/preload/default.js | 4 + public/preload/menubar.js | 2 + public/preload/shared.js | 22 +++ src/components/app.js | 5 +- src/components/pages/history/search-box.js | 5 +- src/components/pages/home/index.js | 14 +- .../pages/language-list/search-box.js | 5 +- src/components/pages/ocr/index.js | 8 +- src/components/pages/phrasebook/search-box.js | 5 +- src/components/pages/preferences/index.js | 4 +- src/components/root/dialog-about.js | 4 +- src/components/shared/windows-title-bar.js | 11 +- src/helpers/open-file-to-blob-async.js | 3 +- src/helpers/take-screenshot-to-blob-async.js | 16 +-- src/index.js | 4 - src/listeners/index.js | 46 +++--- src/senders/index.js | 34 +++-- src/state/root/general/reducers.js | 3 +- yarn.lock | 132 ++++++++++++++++-- 22 files changed, 242 insertions(+), 105 deletions(-) create mode 100644 public/libs/sentry.js create mode 100644 public/preload/default.js create mode 100644 public/preload/shared.js diff --git a/package.json b/package.json index bd61c60f..6d416993 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "test": "mocha" }, "dependencies": { + "@sentry/electron": "2.0.3", "cli-truncate": "2.1.0", "electron-is-dev": "1.2.0", "electron-settings": "3.2.0", diff --git a/public/electron.js b/public/electron.js index 6930b51e..aaf158bf 100644 --- a/public/electron.js +++ b/public/electron.js @@ -26,6 +26,12 @@ const { autoUpdater } = require('electron-updater'); const { menubar } = require('menubar'); const windowStateKeeper = require('electron-window-state'); +// Activate the Sentry Electron SDK as early as possible in every process. +if (!isDev) { + // eslint-disable-next-line global-require + require('./libs/sentry'); +} + const { createMenu, showMenu } = require('./libs/menu'); const loadListeners = require('./listeners'); const { getPreference } = require('./libs/preferences'); @@ -242,6 +248,7 @@ if (!gotTheLock) { webPreferences: { nodeIntegration: true, webSecurity: false, + preload: path.join(__dirname, 'preload', 'default.js'), }, }); mainWindowState.manage(mainWindow); diff --git a/public/libs/sentry.js b/public/libs/sentry.js new file mode 100644 index 00000000..9d6d4d9a --- /dev/null +++ b/public/libs/sentry.js @@ -0,0 +1,12 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +const electron = require('electron'); +const { init } = require('@sentry/electron'); + +const isRenderer = (process && process.type === 'renderer'); + +init({ + dsn: 'https://f7ea6d9ea90543a78848d2651be864ce@o476721.ingest.sentry.io/5516769', + release: isRenderer ? electron.remote.app.getVersion() : electron.app.getVersion(), +}); diff --git a/public/preload/default.js b/public/preload/default.js new file mode 100644 index 00000000..631cce52 --- /dev/null +++ b/public/preload/default.js @@ -0,0 +1,4 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +require('./shared'); diff --git a/public/preload/menubar.js b/public/preload/menubar.js index f43d4ee6..a04c4852 100644 --- a/public/preload/menubar.js +++ b/public/preload/menubar.js @@ -2,3 +2,5 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ window.mode = 'menubar'; + +require('./shared'); diff --git a/public/preload/shared.js b/public/preload/shared.js new file mode 100644 index 00000000..0c3a1449 --- /dev/null +++ b/public/preload/shared.js @@ -0,0 +1,22 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +const { + remote, + ipcRenderer, + webFrame, + desktopCapturer, +} = require('electron'); +const isDev = require('electron-is-dev'); + +// Activate the Sentry Electron SDK as early as possible in every process. +if (!isDev) { + // eslint-disable-next-line global-require + require('../libs/sentry'); +} + +window.remote = remote; +window.ipcRenderer = ipcRenderer; +window.desktopCapturer = desktopCapturer; + +webFrame.setVisualZoomLevelLimits(1, 1); diff --git a/src/components/app.js b/src/components/app.js index 82e1807e..bf90053a 100644 --- a/src/components/app.js +++ b/src/components/app.js @@ -47,8 +47,7 @@ import { const styles = (theme) => { // big sur increases title bar height - const { remote } = window.require('electron'); - const titleBarHeight = remote.getGlobal('isMacOs11') ? 28 : 22; + const titleBarHeight = window.remote.getGlobal('isMacOs11') ? 28 : 22; return { container: { @@ -164,7 +163,7 @@ class App extends React.Component { onDoubleClick={() => { // feature: double click on title bar to expand #656 // https://github.com/atomery/webcatalog/issues/656 - const win = window.require('electron').remote.getCurrentWindow(); + const win = window.remote.getCurrentWindow(); if (win.isMaximized()) { win.unmaximize(); } else { diff --git a/src/components/pages/history/search-box.js b/src/components/pages/history/search-box.js index 8dd47c43..fcfed3ba 100644 --- a/src/components/pages/history/search-box.js +++ b/src/components/pages/history/search-box.js @@ -86,11 +86,10 @@ const SearchBox = ({ inputRef.current.select(); }, [inputRef]); useEffect(() => { - const { ipcRenderer } = window.require('electron'); - ipcRenderer.on('open-find', handleOpenFind); + window.ipcRenderer.on('open-find', handleOpenFind); // Remove event listener on cleanup return () => { - ipcRenderer.removeListener('open-find', handleOpenFind); + window.ipcRenderer.removeListener('open-find', handleOpenFind); }; }, [handleOpenFind]); diff --git a/src/components/pages/home/index.js b/src/components/pages/home/index.js index 2701f51b..d0d7c830 100644 --- a/src/components/pages/home/index.js +++ b/src/components/pages/home/index.js @@ -193,13 +193,11 @@ class Home extends React.Component { this.inputRef.current.focus(); } - const { ipcRenderer } = window.require('electron'); - ipcRenderer.on('open-find', this.handleOpenFind); + window.ipcRenderer.on('open-find', this.handleOpenFind); } componentWillUnmount() { - const { ipcRenderer } = window.require('electron'); - ipcRenderer.removeListener('open-find', this.handleOpenFind); + window.ipcRenderer.removeListener('open-find', this.handleOpenFind); } handleOpenFind() { @@ -224,8 +222,6 @@ class Home extends React.Component { textToSpeechPlaying, } = this.props; - const { remote } = window.require('electron'); - if (fullscreenInputBox === true) { return null; } @@ -261,7 +257,7 @@ class Home extends React.Component { Icon: FileCopy, tooltip: getLocale('copy'), onClick: () => { - remote.clipboard.writeText(output.outputText); + window.remote.clipboard.writeText(output.outputText); onOpenSnackbar(getLocale('copied')); }, }, @@ -361,8 +357,6 @@ class Home extends React.Component { translateWhenPressingEnter, } = this.props; - const { remote } = window.require('electron'); - const controllers = [ { Icon: ContentClear, @@ -378,7 +372,7 @@ class Home extends React.Component { )), tooltip: getLocale('translateClipboard'), onClick: () => { - const text = remote.clipboard.readText(); + const text = window.remote.clipboard.readText(); onUpdateInputText(text); onTranslate(inputLang, outputLang, text); }, diff --git a/src/components/pages/language-list/search-box.js b/src/components/pages/language-list/search-box.js index d9c76423..dc7e72a4 100644 --- a/src/components/pages/language-list/search-box.js +++ b/src/components/pages/language-list/search-box.js @@ -88,11 +88,10 @@ const SearchBox = ({ inputRef.current.select(); }, [inputRef, route]); useEffect(() => { - const { ipcRenderer } = window.require('electron'); - ipcRenderer.on('open-find', handleOpenFind); + window.ipcRenderer.on('open-find', handleOpenFind); // Remove event listener on cleanup return () => { - ipcRenderer.removeListener('open-find', handleOpenFind); + window.ipcRenderer.removeListener('open-find', handleOpenFind); }; }, [inputRef, handleOpenFind]); diff --git a/src/components/pages/ocr/index.js b/src/components/pages/ocr/index.js index b4fa91f9..5390a1b5 100644 --- a/src/components/pages/ocr/index.js +++ b/src/components/pages/ocr/index.js @@ -138,8 +138,6 @@ class Ocr extends React.Component { zoomLevel, } = this.props; - const { remote } = window.require('electron'); - if (!imageUrl) return null; return ( @@ -172,7 +170,7 @@ class Ocr extends React.Component { lineHeight: `${line.height}px`, }} onClick={() => { - remote.clipboard.writeText(line.text); + window.remote.clipboard.writeText(line.text); onOpenSnackbar(getLocale('copied')); }} > @@ -254,7 +252,7 @@ class Ocr extends React.Component { { - remote.clipboard.writeText(inputText); + window.remote.clipboard.writeText(inputText); onOpenSnackbar(getLocale('copied')); }} > @@ -267,7 +265,7 @@ class Ocr extends React.Component { { - remote.clipboard.writeText(outputText); + window.remote.clipboard.writeText(outputText); onOpenSnackbar(getLocale('copied')); }} > diff --git a/src/components/pages/phrasebook/search-box.js b/src/components/pages/phrasebook/search-box.js index cb6c672c..5b99d435 100644 --- a/src/components/pages/phrasebook/search-box.js +++ b/src/components/pages/phrasebook/search-box.js @@ -86,11 +86,10 @@ const SearchBox = ({ inputRef.current.select(); }, [inputRef]); useEffect(() => { - const { ipcRenderer } = window.require('electron'); - ipcRenderer.on('open-find', handleOpenFind); + window.ipcRenderer.on('open-find', handleOpenFind); // Remove event listener on cleanup return () => { - ipcRenderer.removeListener('open-find', handleOpenFind); + window.ipcRenderer.removeListener('open-find', handleOpenFind); }; }, [handleOpenFind]); diff --git a/src/components/pages/preferences/index.js b/src/components/pages/preferences/index.js index da63449e..f7260545 100644 --- a/src/components/pages/preferences/index.js +++ b/src/components/pages/preferences/index.js @@ -313,7 +313,7 @@ const Preferences = (props) => { <> window.require('electron').remote.shell.openExternal('https://translatium.app/popclip')} + onClick={() => window.remote.shell.openExternal('https://translatium.app/popclip')} > @@ -432,7 +432,7 @@ const Preferences = (props) => { )} - window.require('electron').remote.app.quit()} /> + window.remote.app.quit()} /> diff --git a/src/components/root/dialog-about.js b/src/components/root/dialog-about.js index e6a74c03..1e82b862 100644 --- a/src/components/root/dialog-about.js +++ b/src/components/root/dialog-about.js @@ -64,8 +64,6 @@ const About = (props) => { open, } = props; - const { remote } = window.require('electron'); - return ( { variant="body2" className={classes.version} > - {`Version v${remote.app.getVersion()}`} + {`Version v${window.remote.app.getVersion()}`}