Skip to content

Commit

Permalink
Proxying Electron breaks it, so just don't
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyza committed Apr 9, 2022
1 parent ed4d75a commit b4f365d
Showing 1 changed file with 34 additions and 39 deletions.
73 changes: 34 additions & 39 deletions src/patchBrowserWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
});

Expand All @@ -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,
};

0 comments on commit b4f365d

Please sign in to comment.