Vencord/src/patcher.ts

68 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-08-29 00:25:27 +00:00
import electron, { app, BrowserWindowConstructorOptions } from "electron";
import installExt, { REACT_DEVELOPER_TOOLS } from "electron-devtools-installer";
import { join } from "path";
2022-08-29 16:11:44 +00:00
import { initIpc } from './ipcMain';
2022-08-29 00:25:27 +00:00
2022-08-31 20:09:36 +00:00
console.log("[Vencord] Starting up...");
2022-08-29 00:25:27 +00:00
class BrowserWindow extends electron.BrowserWindow {
constructor(options: BrowserWindowConstructorOptions) {
if (options?.webPreferences?.preload && options.title) {
const original = options.webPreferences.preload;
options.webPreferences.preload = join(__dirname, "preload.js");
options.webPreferences.sandbox = false;
2022-08-29 00:25:27 +00:00
process.env.DISCORD_PRELOAD = original;
2022-08-29 16:11:44 +00:00
super(options);
initIpc(this);
} else super(options);
2022-08-29 00:25:27 +00:00
}
}
Object.assign(BrowserWindow, electron.BrowserWindow);
2022-08-29 16:45:58 +00:00
// esbuild may rename our BrowserWindow, which leads to it being excluded
// from getFocusedWindow(), so this is necessary
2022-09-16 20:59:34 +00:00
// https://github.com/discord/electron/blob/13-x-y/lib/browser/api/browser-window.ts#L60-L62
2022-08-29 16:45:58 +00:00
Object.defineProperty(BrowserWindow, "name", { value: "BrowserWindow", configurable: true });
2022-08-29 00:25:27 +00:00
// Replace electrons exports with our custom BrowserWindow
const electronPath = require.resolve("electron");
delete require.cache[electronPath]!.exports;
require.cache[electronPath]!.exports = {
...electron,
BrowserWindow
};
2022-08-29 16:11:44 +00:00
// Patch appSettings to force enable devtools
2022-08-29 00:25:27 +00:00
Object.defineProperty(global, "appSettings", {
2022-08-29 16:11:44 +00:00
set: (v: typeof global.appSettings) => {
2022-08-29 00:25:27 +00:00
v.set("DANGEROUS_ENABLE_DEVTOOLS_ONLY_ENABLE_IF_YOU_KNOW_WHAT_YOURE_DOING", true);
2022-08-29 16:11:44 +00:00
// @ts-ignore
2022-08-29 00:25:27 +00:00
delete global.appSettings;
global.appSettings = v;
},
configurable: true
});
process.env.DATA_DIR = join(app.getPath("userData"), "..", "Vencord");
electron.app.whenReady().then(() => {
2022-08-29 16:11:44 +00:00
installExt(REACT_DEVELOPER_TOOLS)
2022-08-31 20:09:36 +00:00
.then(() => console.info("Installed React DevTools"))
.catch((err) => console.error("Failed to install React DevTools", err));
2022-08-29 00:25:27 +00:00
// Remove CSP
electron.session.defaultSession.webRequest.onHeadersReceived(({ responseHeaders, url }, cb) => {
2022-08-29 16:11:44 +00:00
if (responseHeaders) {
2022-09-27 14:59:02 +00:00
delete responseHeaders["content-security-policy-report-only"];
2022-08-29 00:25:27 +00:00
delete responseHeaders["content-security-policy"];
// Fix hosts that don't properly set the content type, such as
// raw.githubusercontent.com
if (url.endsWith(".css"))
responseHeaders["content-type"] = ["text/css"];
2022-08-29 00:25:27 +00:00
}
2022-08-29 16:11:44 +00:00
cb({ cancel: false, responseHeaders });
2022-08-29 00:25:27 +00:00
});
});