admin管理员组

文章数量:1123377

I have created my Electron App with

create-electron-app $projDir --template=vite-typescript

In my BrowserWindow's webpreferences I have set

contextIsolation: true
nodeIntegration: false

I have defined the API which i want to use in my Renderer context inside preload.ts like this:

const WORLD_ID: number = 1004;
const API_KEY: string = "api";

interface IApi {
    myFun: <T>(...args: any) => Promise<T>;
}

const api: IApi = {
    myFun: <T>(...args: any) => ipcRenderer.invoke("my-event", ...args) as Promise<T>
}

contextBridge.exposeInIsolatedWorld(
    WORLD_ID, API_KEY, api 
);

export { type IApi, API_KEY, WORLD_ID };

i have defined a interface.d.ts for the api:

import { type IApi, API_KEY } from '@/preload';

declare global {
    interface Window {
        [API_KEY]: IApi 
    }
}

The documentation () suggests I can now use the api like this:

// Renderer (In isolated world id1004)

window.api.myFun()
  1. I wasn't able to find any example on how to run the Renderer in an isolated world (as so kindly remarked in the docs). Can you help?
  2. webFrame.executeInIsolatedWorld() does not work since webFrame requires the path module, which is not available in the renderer. Is there an alternative?

本文标签: typescriptUse isolated world API in electron rendererStack Overflow