Vencord/src/utils/patchWebpack.ts

115 lines
4.4 KiB
TypeScript
Raw Normal View History

2022-08-29 00:25:27 +00:00
import { WEBPACK_CHUNK } from './constants';
2022-08-29 18:27:47 +00:00
import Logger from "./logger";
import { _initWebpack } from "./webpack";
2022-08-29 00:25:27 +00:00
let webpackChunk: any[];
2022-08-29 18:27:47 +00:00
const logger = new Logger("WebpackInterceptor", "#8caaee");
2022-08-29 00:25:27 +00:00
Object.defineProperty(window, WEBPACK_CHUNK, {
get: () => webpackChunk,
set: (v) => {
// There are two possible values for push.
// - Native push with toString result of function push() { [native code] }
// - 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")) {
2022-08-29 18:27:47 +00:00
logger.info(`Patching ${WEBPACK_CHUNK}.push`);
_initWebpack(v);
2022-08-29 00:25:27 +00:00
patchPush();
// @ts-ignore
delete window[WEBPACK_CHUNK];
window[WEBPACK_CHUNK] = v;
}
webpackChunk = v;
},
configurable: true
});
function patchPush() {
function handlePush(chunk) {
try {
const modules = chunk[1];
2022-08-29 18:27:47 +00:00
const { subscriptions, listeners } = Vencord.Webpack;
const { patches } = Vencord.Plugins;
2022-08-29 00:25:27 +00:00
for (const id in modules) {
let mod = modules[id];
let code = mod.toString();
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;
2022-08-29 18:27:47 +00:00
logger.error("Error in patched chunk", err);
2022-08-29 00:25:27 +00:00
return originalMod(module, exports, require);
}
2022-08-29 18:27:47 +00:00
for (const callback of listeners) {
try {
callback(exports);
} catch (err) {
logger.error("Error in webpack listener", err);
}
}
2022-08-29 00:25:27 +00:00
for (const [filter, callback] of subscriptions) {
try {
if (filter(exports)) {
subscriptions.delete(filter);
callback(exports);
} else if (exports.default && filter(exports.default)) {
subscriptions.delete(filter);
callback(exports.default);
}
} catch (err) {
2022-08-29 18:27:47 +00:00
logger.error("Error while firing callback for webpack chunk", err);
2022-08-29 00:25:27 +00:00
}
}
};
for (let i = 0; i < patches.length; i++) {
const patch = patches[i];
if (code.includes(patch.find)) {
patchedBy.add(patch.plugin);
2022-08-29 18:27:47 +00:00
// @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);
}
2022-08-29 00:25:27 +00:00
}
2022-08-29 18:27:47 +00:00
patches.splice(i--, 1);
2022-08-29 00:25:27 +00:00
}
}
}
} catch (err) {
2022-08-29 18:27:47 +00:00
logger.error("oopsie", err);
2022-08-29 00:25:27 +00:00
}
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
});
}