From b4f365d42e614b7b3c24c58ea2c6087d9bbd30b1 Mon Sep 17 00:00:00 2001 From: Kyza Date: Sat, 9 Apr 2022 13:25:21 -0400 Subject: [PATCH] Proxying Electron breaks it, so just don't --- src/patchBrowserWindow.ts | 73 ++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/src/patchBrowserWindow.ts b/src/patchBrowserWindow.ts index 5d02ac3..c28fb7a 100644 --- a/src/patchBrowserWindow.ts +++ b/src/patchBrowserWindow.ts @@ -5,43 +5,34 @@ console.log("Patching Electron's BrowserWindow."); const preloadPath = path.join(__dirname, "preload"); -// Create a Proxy over Electron that returns the PatchedBrowserWindow if BrowserWindow is called. -// Can't just proxy the BrowserWindow class directly because it's a getter. -const electronProxy = new Proxy(electron, { - get(electronTarget, property) { - switch (property) { - case "BrowserWindow": - return new Proxy(electronTarget[property], { - construct(BrowserWindowTarget, args) { - const [options, ...rest] = args; - - // TODO: Need some way to let packages control this. - const originalPreload = options.webPreferences.preload; - - options.webPreferences.preload = preloadPath; - - // Any reason to have this off? - options.webPreferences.experimentalFeatures = true; - - // Keybase (dumb). - options.webPreferences.devTools = true; - - // @ts-ignore - const window = new BrowserWindowTarget(options, ...rest); - - // Put the location and the original preload in a place the main IPC can easily reach. - // @ts-ignore - window.webContents.kernelWindowData = { - originalPreload: originalPreload, - windowOptions: options, - }; - - return window; - }, - }); - default: - return electronTarget[property]; - } +// Extending the class does not work. +export const ProxiedBrowserWindow = new Proxy(electron.BrowserWindow, { + construct(target, args) { + const options: electron.BrowserWindowConstructorOptions = args[0]; + + const originalPreload = options.webPreferences.preload; + + options.webPreferences.preload = preloadPath; + + // Any reason to have this off? + options.webPreferences.experimentalFeatures = true; + + // Keybase (dumb). + options.webPreferences.devTools = true; + + // TODO: Check for MS Teams compatibility. + + // @ts-ignore + const window = new target(options); + + // Put the location and the original preload in a place the main IPC can easily reach. + // @ts-ignore + window.webContents.kernelWindowData = { + originalPreload: originalPreload, + windowOptions: options, + }; + + return window; }, }); @@ -50,5 +41,9 @@ const electronProxy = new Proxy(electron, { const electronPath = require.resolve("electron"); // Delete Electron from the require cache because it's a getter. delete require.cache[electronPath].exports; -// Replace it with the new Electron Proxy. -require.cache[electronPath].exports = electronProxy; +// Replace it with the a new Electron that has the ProxiedBrowserWindow. +// TODO: Look at possible problems if getters aren't used. +require.cache[electronPath].exports = { + ...electron, + BrowserWindow: ProxiedBrowserWindow, +};