2020-06-23 03:32:45 +00:00
|
|
|
const binding = process._linkedBinding('electron_renderer_context_bridge');
|
2019-10-18 19:57:09 +00:00
|
|
|
|
|
|
|
const checkContextIsolationEnabled = () => {
|
2021-11-08 16:57:56 +00:00
|
|
|
if (!process.contextIsolated) throw new Error('contextBridge API can only be used when contextIsolation is enabled');
|
2019-10-18 19:57:09 +00:00
|
|
|
};
|
|
|
|
|
2020-05-11 20:41:42 +00:00
|
|
|
const contextBridge: Electron.ContextBridge = {
|
2020-12-04 17:43:20 +00:00
|
|
|
exposeInMainWorld: (key: string, api: any) => {
|
2019-10-18 19:57:09 +00:00
|
|
|
checkContextIsolationEnabled();
|
2022-09-21 18:17:10 +00:00
|
|
|
return binding.exposeAPIInWorld(0, key, api);
|
|
|
|
},
|
|
|
|
exposeInIsolatedWorld: (worldId: number, key: string, api: any) => {
|
|
|
|
checkContextIsolationEnabled();
|
|
|
|
return binding.exposeAPIInWorld(worldId, key, api);
|
2020-05-15 18:57:40 +00:00
|
|
|
}
|
2021-01-29 20:41:59 +00:00
|
|
|
};
|
2019-10-18 19:57:09 +00:00
|
|
|
|
|
|
|
export default contextBridge;
|
2020-04-22 19:42:51 +00:00
|
|
|
|
|
|
|
export const internalContextBridge = {
|
2021-11-08 16:57:56 +00:00
|
|
|
contextIsolationEnabled: process.contextIsolated,
|
2020-04-27 19:46:04 +00:00
|
|
|
overrideGlobalValueFromIsolatedWorld: (keys: string[], value: any) => {
|
|
|
|
return binding._overrideGlobalValueFromIsolatedWorld(keys, value, false);
|
|
|
|
},
|
|
|
|
overrideGlobalValueWithDynamicPropsFromIsolatedWorld: (keys: string[], value: any) => {
|
|
|
|
return binding._overrideGlobalValueFromIsolatedWorld(keys, value, true);
|
2020-04-22 19:42:51 +00:00
|
|
|
},
|
|
|
|
overrideGlobalPropertyFromIsolatedWorld: (keys: string[], getter: Function, setter?: Function) => {
|
|
|
|
return binding._overrideGlobalPropertyFromIsolatedWorld(keys, getter, setter || null);
|
|
|
|
},
|
|
|
|
isInMainWorld: () => binding._isCalledFromMainWorld() as boolean
|
|
|
|
};
|
2020-05-11 20:41:42 +00:00
|
|
|
|
2020-05-15 18:57:40 +00:00
|
|
|
if (binding._isDebug) {
|
2020-05-11 20:41:42 +00:00
|
|
|
contextBridge.internalContextBridge = internalContextBridge;
|
|
|
|
}
|