aboutsummaryrefslogtreecommitdiff
path: root/src/patcher.ts
blob: eb45b981d7b81c01a676d59459ec4269153c3c25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import electron, { app, BrowserWindowConstructorOptions } from "electron";
import installExt, { REACT_DEVELOPER_TOOLS } from "electron-devtools-installer";
import { join } from "path";

console.log("[Vencord] Starting up...");

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");

            process.env.APP_PATH = app.getAppPath();
            process.env.DISCORD_PRELOAD = original;
        }
        super(options);
    }
}
Object.assign(BrowserWindow, electron.BrowserWindow);

// Replace electrons exports with our custom BrowserWindow
const electronPath = require.resolve("electron");
delete require.cache[electronPath]!.exports;
require.cache[electronPath]!.exports = {
    ...electron,
    BrowserWindow
};

// Patch appSettingsa to force enable devtools
Object.defineProperty(global, "appSettings", {
    set: (v) => {
        v.set("DANGEROUS_ENABLE_DEVTOOLS_ONLY_ENABLE_IF_YOU_KNOW_WHAT_YOURE_DOING", true);
        delete global.appSettings;
        global.appSettings = v;
    },
    configurable: true
});

process.env.DATA_DIR = join(app.getPath("userData"), "..", "Vencord");

electron.app.whenReady().then(() => {
    /* installExt(REACT_DEVELOPER_TOOLS)
        .then(() => console.log("Installed React DevTools"))
        .catch((err) => console.error("Failed to install React DevTools", err)); */

    // Remove CSP
    electron.session.defaultSession.webRequest.onHeadersReceived(({ responseHeaders, url }, cb) => {
        if (responseHeaders && url.endsWith(".css")) {
            delete responseHeaders["content-security-policy-report-only"];
            delete responseHeaders["content-security-policy"];
            // probably makes github raw work? not tested.
            responseHeaders["content-type"] = ["text/css"];
            responseHeaders;
        }
        cb({ cancel: false, responseHeaders: responseHeaders });
    });

    // Drop science and sentry requests
    electron.session.defaultSession.webRequest.onBeforeRequest(
        { urls: ["https://*/api/v*/science", "https://sentry.io/*"] },
        (_, callback) => callback({ cancel: true })
    );
});