electron#contextBridge TypeScript Examples

The following examples show how to use electron#contextBridge. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: app.ts    From desktop with GNU Affero General Public License v3.0 6 votes vote down vote up
contextBridge.exposeInMainWorld("native", {
    min: () => ipcRenderer.send("min"),
    max: () => ipcRenderer.send("max"),
    close: () => ipcRenderer.send("close"),
    reload: () => ipcRenderer.send("reload"),
    relaunch: () => ipcRenderer.send("relaunch"),

    getConfig: () => config,
    set: (k: string, v: any) => config.set(k, v),

    getAutoStart: () =>
        new Promise((resolve) => {
            ipcRenderer.send("getAutoStart");
            ipcRenderer.on("autoStart", (_, arg) => resolve(arg));
        }),
    enableAutoStart: () =>
        new Promise((resolve) => {
            ipcRenderer.send("setAutoStart", true);
            ipcRenderer.on("autoStart", (_, arg) => resolve(arg));
        }),
    disableAutoStart: () =>
        new Promise((resolve) => {
            ipcRenderer.send("setAutoStart", false);
            ipcRenderer.on("autoStart", (_, arg) => resolve(arg));
        }),
});
Example #2
Source File: preload.ts    From animation-editor with MIT License 6 votes vote down vote up
contextBridge.exposeInMainWorld("electron", <ElectronGlobal>{
	onDoubleClickDragArea: () => ipcRenderer.send("double-click-drag-area"),
	registerUndo: (fn) => {
		_onUndo = fn;
	},
	registerRedo: (fn) => {
		_onRedo = fn;
	},
});
Example #3
Source File: preload.ts    From multi-downloader-nx with MIT License 6 votes vote down vote up
contextBridge.exposeInMainWorld('Electron', {
  ipcRenderer: {
    ...ipcRenderer,
    on: (name: string, handler: (event: Electron.IpcRendererEvent, ...args: any[]) => void) => {
      ipcRenderer.on(name, handler);
      return ipcRenderer;
    },
    removeListener: (name: string, handler: (event: Electron.IpcRendererEvent, ...args: any[]) => void) => {
      ipcRenderer.removeListener(name, handler);
    }
  }
});
Example #4
Source File: preload.ts    From image-optimizer with MIT License 6 votes vote down vote up
contextBridge.exposeInMainWorld('electron', {
  ipc: {
    on: (channel: string, cb: EventCallback) => ipcRenderer.on(channel, cb),
    send: (channel: string, data: any, cb?: EventCallback) => {
      ipcRenderer.send(channel, data)
      if (cb && typeof cb === 'function') {
        ipcRenderer.on(channel, cb)
      }
    },
    removeListener: (channel: string, cb: EventCallback) =>
      ipcRenderer.removeListener(channel, cb),
    removeListeners: (channel: string) =>
      ipcRenderer.removeAllListeners(channel)
  },
  store: {
    set: (key: any, value: any) => store.app.set(key, value),
    get: (key: any) => store.app.get(key),
    on: (key: any, cb: any) => store.app.onDidChange(key, cb)
  }
})
Example #5
Source File: preload.ts    From flect-chime-sdk-demo with Apache License 2.0 6 votes vote down vote up
contextBridge.exposeInMainWorld('myAPI', {
    getWindowId: async (): Promise<string | null> => {
        const sources = await desktopCapturer.getSources({ types: ['window', 'screen'] })
        let targetId: string | null = null
        for (let source of sources) {
            console.log(`WINDOW NAME: name ${source.name}, ${source.display_id}, ${source.id}`);
            if (source.name === "Entire Screen" || source.name === "CHIME_MANAGER") {
            // if (source.name === "CHIME_MANAGER") {
                    targetId = source.id
            }
        }
        return targetId
    },
    getEnvVars: (): { [key: string]: string|undefined } => {
        console.log(process.env)

        const {CODE, UUID, MEETING_NAME, ATTENDEE_ID, RESTAPI_ENDPOINT} = process.env
        return  {CODE, UUID, MEETING_NAME, ATTENDEE_ID, RESTAPI_ENDPOINT}

    },
    finalize: (): void => {
        ipcRenderer.invoke('finalize', 'ping')
    },
    onAmongusUpdateMessage: (listener: (message: string) => void) =>{
        ipcRenderer.on('amongus-gamestate-updated', (ev: IpcRendererEvent, message: string) => listener(message))
    },
    recorderDataAvailable1: (file:string, data:Uint8Array):void =>{
        ipcRenderer.invoke('recorder-data-available1', file, data)
    },
    recorderDataAvailable2: (file:string, data:Uint8Array):void =>{
        ipcRenderer.invoke('recorder-data-available2', file, data)
    },
    

});
Example #6
Source File: index.ts    From vite-electron-react-starter with MIT License 6 votes vote down vote up
/**
 * The "Main World" is the JavaScript context that your main renderer code runs in.
 * By default, the page you load in your renderer executes code in this world.
 *
 * @see https://www.electronjs.org/docs/api/context-bridge
 */

/**
 * After analyzing the `exposeInMainWorld` calls,
 * `packages/preload/exposedInMainWorld.d.ts` file will be generated.
 * It contains all interfaces.
 * `packages/preload/exposedInMainWorld.d.ts` file is required for TS is `renderer`
 *
 * @see https://github.com/cawa-93/dts-for-context-bridge
 */

/**
 * Expose Environment versions.
 * @example
 * console.log( window.versions )
 */
contextBridge.exposeInMainWorld('versions', process.versions);
Example #7
Source File: index.ts    From vite-electron-react-starter with MIT License 6 votes vote down vote up
/**
 * Safe expose node.js API
 * @example
 * window.nodeCrypto('data')
 */
contextBridge.exposeInMainWorld('nodeCrypto', {
  sha256sum(data: BinaryLike) {
    const hash = createHash('sha256');
    hash.update(data);
    return hash.digest('hex');
  },
});
Example #8
Source File: remote.ts    From TidGi-Desktop with Mozilla Public License 2.0 5 votes vote down vote up
contextBridge.exposeInMainWorld('remote', remoteMethods);
Example #9
Source File: preload.ts    From sapio-studio with Mozilla Public License 2.0 5 votes vote down vote up
contextBridge.exposeInMainWorld('electron', api);
Example #10
Source File: preload.ts    From uivonim with GNU Affero General Public License v3.0 5 votes vote down vote up
contextBridge.exposeInMainWorld('api', api)
Example #11
Source File: browserViewMetaData.ts    From TidGi-Desktop with Mozilla Public License 2.0 5 votes vote down vote up
contextBridge.exposeInMainWorld('meta', browserViewMetaData);
Example #12
Source File: i18n.ts    From TidGi-Desktop with Mozilla Public License 2.0 5 votes vote down vote up
contextBridge.exposeInMainWorld('i18n', i18n);
Example #13
Source File: log.ts    From TidGi-Desktop with Mozilla Public License 2.0 5 votes vote down vote up
contextBridge.exposeInMainWorld('log', logMethods);
Example #14
Source File: preload.ts    From fluent-reader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
contextBridge.exposeInMainWorld("utils", utilsBridge)
Example #15
Source File: index.ts    From TidGi-Desktop with Mozilla Public License 2.0 5 votes vote down vote up
contextBridge.exposeInMainWorld('service', service);
Example #16
Source File: preload.ts    From dev-manager-desktop with Apache License 2.0 5 votes vote down vote up
contextBridge.exposeInMainWorld('ipcRenderer', ipcRenderer);
Example #17
Source File: preload.ts    From fluent-reader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
contextBridge.exposeInMainWorld("settings", settingsBridge)
Example #18
Source File: preload.ts    From kliveide with MIT License 5 votes vote down vote up
contextBridge.exposeInMainWorld("ipcRenderer", <IpcRendereApi>{
  send: (channel: string, ...args: any[]) => ipcRenderer.send(channel, ...args),
  on: (
    channel: string,
    listener: (event: IpcRendererEvent, ...args: any[]) => void
  ) => ipcRenderer.on(channel, listener),
});
Example #19
Source File: app.ts    From desktop with GNU Affero General Public License v3.0 5 votes vote down vote up
contextBridge.exposeInMainWorld("nativeVersion", "1.0.5");
Example #20
Source File: app.ts    From desktop with GNU Affero General Public License v3.0 5 votes vote down vote up
contextBridge.exposeInMainWorld("isNative", true);
Example #21
Source File: electron-rt.ts    From react-sqlite-app-starter with MIT License 5 votes vote down vote up
contextBridge.exposeInMainWorld('CapacitorCustomPlatform', {
  name: 'electron',
  plugins: contextApi,
});
Example #22
Source File: electron-rt.ts    From angular-sqlite-app-starter with MIT License 5 votes vote down vote up
contextBridge.exposeInMainWorld('CapacitorCustomPlatform', {
  name: 'electron',
  plugins: contextApi,
});
Example #23
Source File: preload.ts    From excalidraw-desktop with MIT License 5 votes vote down vote up
contextBridge.exposeInMainWorld("remote", {
  getVersion: remote.app.getVersion,
});
Example #24
Source File: preload.ts    From excalidraw-desktop with MIT License 5 votes vote down vote up
contextBridge.exposeInMainWorld("ipcRenderer", {
  send: (channel: string, data: any[]) => {
    ipcRenderer.send(channel, data);
  },
  receive: (channel: string, func: IpcListener) => {
    ipcRenderer.on(channel, (event, ...args: any[]) => func(event, ...args));
  },
});
Example #25
Source File: preload.ts    From crust-apps with Apache License 2.0 5 votes vote down vote up
contextBridge.exposeInMainWorld('ElectronMain', {
  accountStore: {
    all: () => ipcRenderer.invoke('account-store-all'),
    get: (key: string) => ipcRenderer.invoke('account-store-get', key),
    remove: (key: string) => ipcRenderer.invoke('account-store-remove', key),
    set: (key: string, value: KeyringJson) => ipcRenderer.invoke('account-store-set', key, value)
  }
});
Example #26
Source File: preload.ts    From noteworthy with GNU Affero General Public License v3.0 5 votes vote down vote up
contextBridge.exposeInMainWorld(
	"clipboardApi", clipboardApi
);
Example #27
Source File: preload.ts    From noteworthy with GNU Affero General Public License v3.0 5 votes vote down vote up
contextBridge.exposeInMainWorld(
    "restrictedIpcRenderer", restrictedIpcRenderer
);
Example #28
Source File: main.ts    From rewind with MIT License 5 votes vote down vote up
contextBridge.exposeInMainWorld("api", frontendAPI);
Example #29
Source File: preload.ts    From FIDO2Client with MIT License 5 votes vote down vote up
// For safe espose api to renderer, strip all global declare
contextBridge.exposeInMainWorld('api', {
    close(): void {
        ipcRenderer.send('fido2-modal-close');
    },
    acceptRequest(): void {
        return ipcRenderer.send('fido2-event-response', true);
    },
    rejectRequest(): void {
        return ipcRenderer.send('fido2-event-response', false);
    },
    get getRequest(): Promise<ModalRequest> {
        return new Promise<ModalRequest>((resolve, reject) => ipcRenderer.once('fido2-event-request', (_, request) => resolve(request)));
    },
    deviceAttach(listener: (device: any) => void): void {
        ipcRenderer.on('fido2-event-device-attach', (_, device) => listener(device));
    },
    deviceDetach(listener: (device: any) => void): void {
        ipcRenderer.on('fido2-event-device-detach', (_, device) => listener(device));
    },
    selectDevice(device: any): Promise<any> {
        return new Promise<any>((resolve, reject) => {
            ipcRenderer.once('fido2-event-device-selected', (_, info) => resolve(info));
            ipcRenderer.send('fido2-event-select-device', device);
        });
    },
    cancelTransaction(): void {
        return ipcRenderer.send('fido2-event-cancel');
    },
    keepAlive(listener: (status: number) => void): void {
        ipcRenderer.on('fido2-event-keep-alive', (_, status) => listener(status));
    },
    error(e: string): void {
        ipcRenderer.send('fido2-event-error', e);
    },
    get transactionSuccess(): Promise<void> {
        return new Promise<void>((resolve, reject) => ipcRenderer.once('fido2-event-success', () => resolve()));
    },
    enterPin(pin: string): void {
        ipcRenderer.send('fido2-event-pin-available', pin);
    },
    get pinValid(): Promise<void> {
        return new Promise<void>((resolve, reject) => ipcRenderer.once('fido2-event-pin-valid', _ => resolve()));
    },
    pinInvalid(listener: (retries: number) => void): void {
        ipcRenderer.on('fido2-event-pin-invalid', (_, retries) => listener(retries));
    },
    get pinAuthBlocked(): Promise<void> {
        return new Promise<void>((resolve, reject) => ipcRenderer.once('fido2-event-pin-auth-blocked', () => resolve()));
    },
    get pinBlocked(): Promise<void> {
        return new Promise<void>((resolve, reject) => ipcRenderer.once('fido2-event-pin-blocked', () => resolve()));
    },
    get noCredentials(): Promise<void> {
        return new Promise<void>((resolve, reject) => ipcRenderer.once('fido2-event-no-credentials', () => resolve()));
    },
    get timeout(): Promise<void> {
        return new Promise<void>((resolve, reject) => ipcRenderer.once('fido2-event-timeout', () => resolve()));
    }
} as IpcRendererApi);