1717 *
1818 */
1919
20- import * as remoteMain from '@electron/remote/main' ;
2120import {
2221 app ,
22+ clipboard ,
2323 dialog ,
2424 BrowserWindow ,
2525 BrowserWindowConstructorOptions ,
2626 Event as ElectronEvent ,
2727 ipcMain ,
2828 Menu ,
29+ nativeImage ,
30+ nativeTheme ,
2931 WebContents ,
3032 desktopCapturer ,
3133 safeStorage ,
@@ -74,8 +76,6 @@ import {ProxyPromptWindow} from './window/ProxyPromptWindow';
7476import { WindowManager } from './window/WindowManager' ;
7577import * as WindowUtil from './window/WindowUtil' ;
7678
77- remoteMain . initialize ( ) ;
78-
7979const APP_PATH = path . join ( app . getAppPath ( ) , config . electronDirectory ) ;
8080const INDEX_HTML = path . join ( APP_PATH , 'renderer/index.html' ) ;
8181const LOG_DIR = path . join ( app . getPath ( 'userData' ) , 'logs' ) ;
@@ -278,7 +278,7 @@ const showMainWindow = async (mainWindowState: windowStateKeeper.State): Promise
278278 title : config . name ,
279279 webPreferences : {
280280 backgroundThrottling : false ,
281- contextIsolation : false ,
281+ contextIsolation : true ,
282282 nodeIntegration : false ,
283283 preload : PRELOAD_JS ,
284284 sandbox : false ,
@@ -296,10 +296,49 @@ const showMainWindow = async (mainWindowState: windowStateKeeper.State): Promise
296296 ipcMain . handle ( EVENT_TYPE . ACTION . DECRYPT , ( event , encrypted : Uint8Array ) =>
297297 safeStorage . decryptString ( Buffer . from ( encrypted ) ) ,
298298 ) ;
299+ ipcMain . handle ( EVENT_TYPE . UI . SHOULD_USE_DARK_COLORS , ( ) => {
300+ return nativeTheme . shouldUseDarkColors ;
301+ } ) ;
299302
300- main = new BrowserWindow ( options ) ;
303+ ipcMain . handle ( EVENT_TYPE . CONTEXT_MENU . COPY_TEXT , ( event , text : string ) => {
304+ clipboard . writeText ( text ) ;
305+ } ) ;
306+
307+ ipcMain . handle ( EVENT_TYPE . CONTEXT_MENU . COPY_IMAGE , async ( event , imageUrl : string ) => {
308+ try {
309+ const response = await fetch ( imageUrl , {
310+ headers : {
311+ 'User-Agent' : config . userAgent ,
312+ } ,
313+ } ) ;
314+ const bytes = await response . arrayBuffer ( ) ;
315+ const image = nativeImage . createFromBuffer ( Buffer . from ( bytes ) ) ;
316+ clipboard . writeImage ( image ) ;
317+ } catch ( error ) {
318+ console . error ( 'Failed to copy image:' , error ) ;
319+ }
320+ } ) ;
301321
302- remoteMain . enable ( main . webContents ) ;
322+ ipcMain . handle ( EVENT_TYPE . CONTEXT_MENU . SAVE_IMAGE , async ( event , imageUrl : string , timestamp ?: string ) => {
323+ try {
324+ const response = await fetch ( imageUrl , {
325+ headers : {
326+ 'User-Agent' : config . userAgent ,
327+ } ,
328+ } ) ;
329+ const bytes = await response . arrayBuffer ( ) ;
330+ ipcMain . emit ( EVENT_TYPE . ACTION . SAVE_PICTURE , event , new Uint8Array ( bytes ) , timestamp ) ;
331+ } catch ( error ) {
332+ console . error ( 'Failed to save image:' , error ) ;
333+ }
334+ } ) ;
335+
336+ ipcMain . handle ( EVENT_TYPE . CONTEXT_MENU . REPLACE_MISSPELLING , ( event , suggestion : string ) => {
337+ const webContents = event . sender ;
338+ webContents . replaceMisspelling ( suggestion ) ;
339+ } ) ;
340+
341+ main = new BrowserWindow ( options ) ;
303342
304343 main . setMenuBarVisibility ( showMenuBar ) ;
305344
@@ -632,7 +671,6 @@ class ElectronWrapperInit {
632671 const enableSpellChecking = settings . restore ( SettingsType . ENABLE_SPELL_CHECKING , true ) ;
633672
634673 app . on ( 'web-contents-created' , async ( webviewEvent : ElectronEvent , contents : WebContents ) => {
635- remoteMain . enable ( contents ) ;
636674 // disable new Windows by default on everything
637675 contents . setWindowOpenHandler ( ( ) => {
638676 return { action : 'deny' } ;
@@ -645,7 +683,7 @@ class ElectronWrapperInit {
645683 params . contextIsolation = 'true' ;
646684 params . plugins = 'false' ;
647685 webPreferences . allowRunningInsecureContent = false ;
648- webPreferences . contextIsolation = false ;
686+ webPreferences . contextIsolation = true ;
649687 webPreferences . experimentalFeatures = false ;
650688 webPreferences . nodeIntegration = false ;
651689 webPreferences . preload = PRELOAD_RENDERER_JS ;
0 commit comments