Vencord/src/patcher.ts

75 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-08-28 20:25:27 -04:00
import electron, { app, BrowserWindowConstructorOptions } from "electron";
import installExt, { REACT_DEVELOPER_TOOLS } from "electron-devtools-installer";
import { join } from "path";
2022-08-29 12:11:44 -04:00
import { initIpc } from './ipcMain';
2022-08-28 20:25:27 -04:00
2022-08-31 16:09:36 -04:00
console.log("[Vencord] Starting up...");
2022-08-28 20:25:27 -04: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-28 20:25:27 -04:00
process.env.DISCORD_PRELOAD = original;
2022-08-29 12:11:44 -04:00
super(options);
initIpc(this);
} else super(options);
2022-08-28 20:25:27 -04:00
}
}
Object.assign(BrowserWindow, electron.BrowserWindow);
2022-08-29 12:45:58 -04:00
// esbuild may rename our BrowserWindow, which leads to it being excluded
// from getFocusedWindow(), so this is necessary
2022-09-16 16:59:34 -04:00
// https://github.com/discord/electron/blob/13-x-y/lib/browser/api/browser-window.ts#L60-L62
2022-08-29 12:45:58 -04:00
Object.defineProperty(BrowserWindow, "name", { value: "BrowserWindow", configurable: true });
2022-08-28 20:25:27 -04: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 12:11:44 -04:00
// Patch appSettings to force enable devtools
2022-08-28 20:25:27 -04:00
Object.defineProperty(global, "appSettings", {
2022-08-29 12:11:44 -04:00
set: (v: typeof global.appSettings) => {
2022-08-28 20:25:27 -04:00
v.set("DANGEROUS_ENABLE_DEVTOOLS_ONLY_ENABLE_IF_YOU_KNOW_WHAT_YOURE_DOING", true);
2022-08-29 12:11:44 -04:00
// @ts-ignore
2022-08-28 20:25:27 -04: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 12:11:44 -04:00
installExt(REACT_DEVELOPER_TOOLS)
2022-08-31 16:09:36 -04:00
.then(() => console.info("Installed React DevTools"))
.catch((err) => console.error("Failed to install React DevTools", err));
2022-08-28 20:25:27 -04:00
// Remove CSP
electron.session.defaultSession.webRequest.onHeadersReceived(({ responseHeaders, url }, cb) => {
2022-08-29 12:11:44 -04:00
if (responseHeaders) {
2022-09-27 10:57:46 -04:00
delete responseHeaders["content-security-policy-reportgnly"];
2022-08-28 20:25:27 -04: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-28 20:25:27 -04:00
}
2022-08-29 12:11:44 -04:00
cb({ cancel: false, responseHeaders });
2022-08-28 20:25:27 -04:00
});
// Drop science and sentry requests
electron.session.defaultSession.webRequest.onBeforeRequest(
{ urls: ["https://*/api/v*/science", "https://sentry.io/*"] },
(_, callback) => callback({ cancel: true })
);
});