electron/lib/renderer/api/context-bridge.ts
Akshay Deo dfc134de42
feat: add exposeInIsolatedWorld(worldId, key, api) to contextBridge (#34974)
* feat: add exposeInIsolatedWorld(worldId, key, api) to contextBridge

* Updates exposeInIslatedWorld worldId documentation
2022-09-21 13:17:10 -05:00

36 lines
1.4 KiB
TypeScript

const binding = process._linkedBinding('electron_renderer_context_bridge');
const checkContextIsolationEnabled = () => {
if (!process.contextIsolated) throw new Error('contextBridge API can only be used when contextIsolation is enabled');
};
const contextBridge: Electron.ContextBridge = {
exposeInMainWorld: (key: string, api: any) => {
checkContextIsolationEnabled();
return binding.exposeAPIInWorld(0, key, api);
},
exposeInIsolatedWorld: (worldId: number, key: string, api: any) => {
checkContextIsolationEnabled();
return binding.exposeAPIInWorld(worldId, key, api);
}
};
export default contextBridge;
export const internalContextBridge = {
contextIsolationEnabled: process.contextIsolated,
overrideGlobalValueFromIsolatedWorld: (keys: string[], value: any) => {
return binding._overrideGlobalValueFromIsolatedWorld(keys, value, false);
},
overrideGlobalValueWithDynamicPropsFromIsolatedWorld: (keys: string[], value: any) => {
return binding._overrideGlobalValueFromIsolatedWorld(keys, value, true);
},
overrideGlobalPropertyFromIsolatedWorld: (keys: string[], getter: Function, setter?: Function) => {
return binding._overrideGlobalPropertyFromIsolatedWorld(keys, getter, setter || null);
},
isInMainWorld: () => binding._isCalledFromMainWorld() as boolean
};
if (binding._isDebug) {
contextBridge.internalContextBridge = internalContextBridge;
}