aboutsummaryrefslogtreecommitdiff
path: root/src/utils/patchWebpack.ts
diff options
context:
space:
mode:
authorVendicated <vendicated@riseup.net>2022-08-29 20:27:47 +0200
committerVendicated <vendicated@riseup.net>2022-08-29 20:27:47 +0200
commitc39ff8f6481463a1700014ebd204f2b6189759a1 (patch)
treea5a4d1fe04a9d99029d480ec3f5a2ca2fb9c0b13 /src/utils/patchWebpack.ts
parent1709ab61ef2138d350b3d45a3eba0d94d5adf0a1 (diff)
downloadVencord-c39ff8f6481463a1700014ebd204f2b6189759a1.tar.gz
Vencord-c39ff8f6481463a1700014ebd204f2b6189759a1.tar.bz2
Vencord-c39ff8f6481463a1700014ebd204f2b6189759a1.zip
More progress
Diffstat (limited to 'src/utils/patchWebpack.ts')
-rw-r--r--src/utils/patchWebpack.ts53
1 files changed, 35 insertions, 18 deletions
diff --git a/src/utils/patchWebpack.ts b/src/utils/patchWebpack.ts
index 6a379de..0e94694 100644
--- a/src/utils/patchWebpack.ts
+++ b/src/utils/patchWebpack.ts
@@ -1,7 +1,12 @@
import { WEBPACK_CHUNK } from './constants';
+import Logger from "./logger";
+import { _initWebpack } from "./webpack";
let webpackChunk: any[];
+const logger = new Logger("WebpackInterceptor", "#8caaee");
+
+
Object.defineProperty(window, WEBPACK_CHUNK, {
get: () => webpackChunk,
set: (v) => {
@@ -10,6 +15,8 @@ Object.defineProperty(window, WEBPACK_CHUNK, {
// - Webpack's push with toString result of function() { [native code] }
// We don't want to override the native one, so check for "push"
if (v && !v.push.toString().includes("push")) {
+ logger.info(`Patching ${WEBPACK_CHUNK}.push`);
+ _initWebpack(v);
patchPush();
// @ts-ignore
delete window[WEBPACK_CHUNK];
@@ -24,8 +31,8 @@ function patchPush() {
function handlePush(chunk) {
try {
const modules = chunk[1];
- const subscriptions = new Set<any>();
- const patches = [] as any[];
+ const { subscriptions, listeners } = Vencord.Webpack;
+ const { patches } = Vencord.Plugins;
for (const id in modules) {
let mod = modules[id];
@@ -40,10 +47,17 @@ function patchPush() {
// Just rethrow discord errors
if (mod === originalMod) throw err;
- console.error("[Webpack] Error in patched chunk", err);
+ logger.error("Error in patched chunk", err);
return originalMod(module, exports, require);
}
+ 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)) {
@@ -54,7 +68,7 @@ function patchPush() {
callback(exports.default);
}
} catch (err) {
- console.error("[Webpack] Error while firing callback for webpack chunk", err);
+ logger.error("Error while firing callback for webpack chunk", err);
}
}
};
@@ -63,25 +77,28 @@ function patchPush() {
const patch = patches[i];
if (code.includes(patch.find)) {
patchedBy.add(patch.plugin);
- const lastMod = mod;
- const lastCode = code;
- try {
- const newCode = code.replace(patch.replacement.match, patch.replacement.replace);
- const newMod = (0, eval)(`// Webpack Module ${id} - Patched by ${[...patchedBy].join(", ")}\n${newCode}\n//# sourceURL=WebpackModule${id}`);
- code = newCode;
- mod = newMod;
- patches.splice(i--, 1);
- } catch (err) {
- console.error("[Webpack] Failed to apply patch of", patch.plugin, err);
- code = lastCode;
- mod = lastMod;
- patchedBy.delete(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);
+ const newMod = (0, eval)(`// Webpack Module ${id} - Patched by ${[...patchedBy].join(", ")}\n${newCode}\n//# sourceURL=WebpackModule${id}`);
+ code = newCode;
+ mod = newMod;
+ } catch (err) {
+ logger.error("Failed to apply patch of", patch.plugin, err);
+ code = lastCode;
+ mod = lastMod;
+ patchedBy.delete(patch.plugin);
+ }
}
+ patches.splice(i--, 1);
}
}
}
} catch (err) {
- console.error("oopsie", err);
+ logger.error("oopsie", err);
}
return handlePush.original.call(window[WEBPACK_CHUNK], chunk);