import { IpcEvents } from "@common/events" ; import { contextBridge , ipcRenderer , clipboard } from "electron" ; import { WindowAfterPreload } from "./preload_types" ; let restrictedIpcRenderer = { send : async ( channel , ... data ) => { let validChannels = [ "command" ] ; if ( validChannels . includes ( channel ) || channel . startsWith ( "RENDER_DID_HANDLE" ) ) { console . log ( `preload :: send() :: channel=${ channel } :: ${ data }` ) ; return await ipcRenderer . send ( channel , ... data ) ; } else { console . log ( `preload :: send() :: invalid channel '${ channel }'!` ) ; } } , receive : ( channel , listener ) => { if ( channel . startsWith ( "RENDER_DID_HANDLE" ) || channel . startsWith ( IpcEvents . RENDERER_INVOKE ) ) { console . log ( `preload :: attaching listener for channel=${ channel }` ) ; ipcRenderer . on ( channel , ( event , ... args ) => { console . log ( `preload :: received message :: channel=${ channel }` ) ; listener ( ... args ) ; } ) ; } else { console . log ( `preload :: receive() :: invalid channel '${ channel }'` ) ; } } , invoke : async ( channel , ... data ) => { let validChannels = [ "command" ] ; if ( validChannels . includes ( channel ) ) { console . log ( `preload :: invoke() :: channel=${ channel } :: ${ data }` ) ; return await ipcRenderer . invoke ( channel , ... data ) ; } else { console . log ( `preload :: invoke() :: invalid channel '${ channel }' :: ${ data }` ) ; } } } ; contextBridge . exposeInMainWorld ( "restrictedIpcRenderer" , restrictedIpcRenderer ) ; let clipboardApi = { getClipboardImageDataURI ( ) { if ( clipboard . availableFormats ( "clipboard" ) . find ( str => str . startsWith ( "image" ) ) ) { return clipboard . readImage ( "clipboard" ) . toDataURL ( ) ; } else { return null ; } } } ; contextBridge . exposeInMainWorld ( "clipboardApi" , clipboardApi ) ;