Skip to content

Commit b4f365d

Browse files
committed
Proxying Electron breaks it, so just don't
1 parent ed4d75a commit b4f365d

File tree

1 file changed

+34
-39
lines changed

1 file changed

+34
-39
lines changed

src/patchBrowserWindow.ts

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,34 @@ console.log("Patching Electron's BrowserWindow.");
55

66
const preloadPath = path.join(__dirname, "preload");
77

8-
// Create a Proxy over Electron that returns the PatchedBrowserWindow if BrowserWindow is called.
9-
// Can't just proxy the BrowserWindow class directly because it's a getter.
10-
const electronProxy = new Proxy(electron, {
11-
get(electronTarget, property) {
12-
switch (property) {
13-
case "BrowserWindow":
14-
return new Proxy(electronTarget[property], {
15-
construct(BrowserWindowTarget, args) {
16-
const [options, ...rest] = args;
17-
18-
// TODO: Need some way to let packages control this.
19-
const originalPreload = options.webPreferences.preload;
20-
21-
options.webPreferences.preload = preloadPath;
22-
23-
// Any reason to have this off?
24-
options.webPreferences.experimentalFeatures = true;
25-
26-
// Keybase (dumb).
27-
options.webPreferences.devTools = true;
28-
29-
// @ts-ignore
30-
const window = new BrowserWindowTarget(options, ...rest);
31-
32-
// Put the location and the original preload in a place the main IPC can easily reach.
33-
// @ts-ignore
34-
window.webContents.kernelWindowData = {
35-
originalPreload: originalPreload,
36-
windowOptions: options,
37-
};
38-
39-
return window;
40-
},
41-
});
42-
default:
43-
return electronTarget[property];
44-
}
8+
// Extending the class does not work.
9+
export const ProxiedBrowserWindow = new Proxy(electron.BrowserWindow, {
10+
construct(target, args) {
11+
const options: electron.BrowserWindowConstructorOptions = args[0];
12+
13+
const originalPreload = options.webPreferences.preload;
14+
15+
options.webPreferences.preload = preloadPath;
16+
17+
// Any reason to have this off?
18+
options.webPreferences.experimentalFeatures = true;
19+
20+
// Keybase (dumb).
21+
options.webPreferences.devTools = true;
22+
23+
// TODO: Check for MS Teams compatibility.
24+
25+
// @ts-ignore
26+
const window = new target(options);
27+
28+
// Put the location and the original preload in a place the main IPC can easily reach.
29+
// @ts-ignore
30+
window.webContents.kernelWindowData = {
31+
originalPreload: originalPreload,
32+
windowOptions: options,
33+
};
34+
35+
return window;
4536
},
4637
});
4738

@@ -50,5 +41,9 @@ const electronProxy = new Proxy(electron, {
5041
const electronPath = require.resolve("electron");
5142
// Delete Electron from the require cache because it's a getter.
5243
delete require.cache[electronPath].exports;
53-
// Replace it with the new Electron Proxy.
54-
require.cache[electronPath].exports = electronProxy;
44+
// Replace it with the a new Electron that has the ProxiedBrowserWindow.
45+
// TODO: Look at possible problems if getters aren't used.
46+
require.cache[electronPath].exports = {
47+
...electron,
48+
BrowserWindow: ProxiedBrowserWindow,
49+
};

0 commit comments

Comments
 (0)