2020-11-10 17:06:03 +00:00
|
|
|
const { getWebPreference } = process._linkedBinding('electron_renderer_web_frame');
|
2020-06-23 03:32:45 +00:00
|
|
|
const binding = process._linkedBinding('electron_renderer_context_bridge');
|
2019-10-18 19:57:09 +00:00
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
const contextIsolationEnabled = getWebPreference(window, 'contextIsolation');
|
2019-10-18 19:57:09 +00:00
|
|
|
|
|
|
|
const checkContextIsolationEnabled = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (!contextIsolationEnabled) 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) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
checkContextIsolationEnabled();
|
|
|
|
return binding.exposeAPIInMainWorld(key, api);
|
2020-05-15 18:57:40 +00:00
|
|
|
}
|
2020-05-11 20:41:42 +00:00
|
|
|
} as any;
|
2019-10-18 19:57:09 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
export default contextBridge;
|
2020-04-22 19:42:51 +00:00
|
|
|
|
|
|
|
export const internalContextBridge = {
|
|
|
|
contextIsolationEnabled,
|
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;
|
|
|
|
}
|