aboutsummaryrefslogtreecommitdiff
path: root/src/webpack/patchWebpack.ts
blob: 559673060a52abf028f489fe1052be5d3b9f9e3c (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Vencord, a modification for Discord's desktop app
 * Copyright (c) 2022 Vendicated and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import { WEBPACK_CHUNK } from "../utils/constants";
import Logger from "../utils/logger";
import { _initWebpack } from ".";

let webpackChunk: any[];

const logger = new Logger("WebpackInterceptor", "#8caaee");

Object.defineProperty(window, WEBPACK_CHUNK, {
    get: () => webpackChunk,
    set: v => {
        if (v?.push !== Array.prototype.push) {
            logger.info(`Patching ${WEBPACK_CHUNK}.push`);
            _initWebpack(v);
            patchPush();
            // @ts-ignore
            delete window[WEBPACK_CHUNK];
            window[WEBPACK_CHUNK] = v;
        }
        webpackChunk = v;
    },
    configurable: true
});

function patchPush() {
    function handlePush(chunk: any) {
        try {
            const modules = chunk[1];
            const { subscriptions, listeners } = Vencord.Webpack;
            const { patches } = Vencord.Plugins;

            for (const id in modules) {
                let mod = modules[id];
                // Discords Webpack chunks for some ungodly reason contain random
                // newlines. Cyn recommended this workaround and it seems to work fine,
                // however this could potentially break code, so if anything goes weird,
                // this is probably why.
                // Additionally, `[actual newline]` is one less char than "\n", so if Discord
                // ever targets newer browsers, the minifier could potentially use this trick and
                // cause issues.
                let code: string = mod.toString().replaceAll("\n", "");
                const originalMod = mod;
                const patchedBy = new Set();

                modules[id] = function (module, exports, require) {
                    try {
                        mod(module, exports, require);
                    } catch (err) {
                        // Just rethrow discord errors
                        if (mod === originalMod) throw err;

                        logger.error("Error in patched chunk", err);
                        return void originalMod(module, exports, require);
                    }

                    // There are (at the time of writing) 11 modules exporting the window
                    // Make these non enumerable to improve webpack search performance
                    if (module.exports === window) {
                        Object.defineProperty(require.c, id, {
                            value: require.c[id],
                            enumerable: false,
                            configurable: true,
                            writable: true
                        });
                        return;
                    }

                    for (const callback of listeners) {
                        try {
                            callback(exports);
                        } catch (err) {
                            logger.error("Error in webpack listener", err);
                        }
                    }

                    for (const [filter, callback] of subscriptions) {
                        try {
                            if (filter(exports)) {
                                subscriptions.delete(filter);
                                callback(exports);
                            } else if (typeof exports === "object") {
                                if (exports.default && filter(exports.default)) {
                                    subscriptions.delete(filter);
                                    callback(exports.default);
                                }

                                for (const nested in exports) if (nested.length < 3) {
                                    if (exports[nested] && filter(exports[nested])) {
                                        subscriptions.delete(filter);
                                        callback(exports[nested]);
                                    }
                                }
                            }
                        } catch (err) {
                            logger.error("Error while firing callback for webpack chunk", err);
                        }
                    }
                };

                modules[id].toString = () => mod.toString();
                modules[id].original = originalMod;

                for (let i = 0; i < patches.length; i++) {
                    const patch = patches[i];
                    if (patch.predicate && !patch.predicate()) continue;

                    if (code.includes(patch.find)) {
                        patchedBy.add(patch.plugin);

                        // @ts-ignore we change all patch.replacement to array in plugins/index
                        for (const replacement of patch.replacement) {
                            const lastMod = mod;
                            const lastCode = code;

                            try {
                                const newCode = code.replace(replacement.match, replacement.replace);
                                if (newCode === code) {
                                    logger.warn(`Patch by ${patch.plugin} had no effect (Module id is ${id}): ${replacement.match}`);
                                    if (IS_DEV) {
                                        logger.debug("Function Source:\n", code);
                                    }
                                } else {
                                    code = newCode;
                                    mod = (0, eval)(`// Webpack Module ${id} - Patched by ${[...patchedBy].join(", ")}\n${newCode}\n//# sourceURL=WebpackModule${id}`);
                                }
                            } catch (err) {
                                logger.error(`Failed to apply patch ${replacement.match} of ${patch.plugin} to ${id}:\n`, err);

                                if (IS_DEV) {
                                    const changeSize = code.length - lastCode.length;
                                    const match = lastCode.match(replacement.match)!;

                                    // Use 200 surrounding characters of context
                                    const start = Math.max(0, match.index! - 200);
                                    const end = Math.min(lastCode.length, match.index! + match[0].length + 200);
                                    // (changeSize may be negative)
                                    const endPatched = end + changeSize;

                                    const context = lastCode.slice(start, end);
                                    const patchedContext = code.slice(start, endPatched);

                                    // inline require to avoid including it in !IS_DEV builds
                                    const diff = (require("diff") as typeof import("diff")).diffWordsWithSpace(context, patchedContext);
                                    let fmt = "%c %s ";
                                    const elements = [] as string[];
                                    for (const d of diff) {
                                        const color = d.removed
                                            ? "red"
                                            : d.added
                                                ? "lime"
                                                : "grey";
                                        fmt += "%c%s";
                                        elements.push("color:" + color, d.value);
                                    }

                                    logger.errorCustomFmt(...Logger.makeTitle("white", "Before"), context);
                                    logger.errorCustomFmt(...Logger.makeTitle("white", "After"), context);
                                    const [titleFmt, ...titleElements] = Logger.makeTitle("white", "Diff");
                                    logger.errorCustomFmt(titleFmt + fmt, ...titleElements, ...elements);
                                }
                                code = lastCode;
                                mod = lastMod;
                                patchedBy.delete(patch.plugin);
                            }
                        }

                        if (!patch.all) patches.splice(i--, 1);
                    }
                }
            }
        } catch (err) {
            logger.error("Error in handlePush", err);
        }

        return handlePush.original.call(window[WEBPACK_CHUNK], chunk);
    }

    handlePush.original = window[WEBPACK_CHUNK].push;
    Object.defineProperty(window[WEBPACK_CHUNK], "push", {
        get: () => handlePush,
        set: v => (handlePush.original = v),
        configurable: true
    });
}