2022-10-05 18:42:58 -04:00
|
|
|
import { WEBPACK_CHUNK } from "../utils/constants";
|
2022-09-30 20:27:28 -04:00
|
|
|
import Logger from "../utils/logger";
|
|
|
|
import { _initWebpack } from ".";
|
2022-08-28 20:25:27 -04:00
|
|
|
|
|
|
|
let webpackChunk: any[];
|
|
|
|
|
2022-08-29 14:27:47 -04:00
|
|
|
const logger = new Logger("WebpackInterceptor", "#8caaee");
|
|
|
|
|
2022-08-28 20:25:27 -04:00
|
|
|
Object.defineProperty(window, WEBPACK_CHUNK, {
|
|
|
|
get: () => webpackChunk,
|
2022-10-05 18:42:58 -04:00
|
|
|
set: v => {
|
2022-09-15 12:17:52 -04:00
|
|
|
if (v?.push !== Array.prototype.push) {
|
2022-08-29 14:27:47 -04:00
|
|
|
logger.info(`Patching ${WEBPACK_CHUNK}.push`);
|
|
|
|
_initWebpack(v);
|
2022-08-28 20:25:27 -04: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 14:27:47 -04:00
|
|
|
const { subscriptions, listeners } = Vencord.Webpack;
|
|
|
|
const { patches } = Vencord.Plugins;
|
2022-08-28 20:25:27 -04:00
|
|
|
|
|
|
|
for (const id in modules) {
|
|
|
|
let mod = modules[id];
|
2022-09-15 12:17:52 -04:00
|
|
|
// 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 = mod.toString().replaceAll("\n", "");
|
2022-08-28 20:25:27 -04:00
|
|
|
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 14:27:47 -04:00
|
|
|
logger.error("Error in patched chunk", err);
|
2022-10-08 14:36:57 -04:00
|
|
|
return void originalMod(module, exports, require);
|
2022-08-28 20:25:27 -04:00
|
|
|
}
|
|
|
|
|
2022-09-28 16:49:46 -04:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2022-08-29 14:27:47 -04:00
|
|
|
for (const callback of listeners) {
|
|
|
|
try {
|
|
|
|
callback(exports);
|
|
|
|
} catch (err) {
|
|
|
|
logger.error("Error in webpack listener", err);
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 20:25:27 -04:00
|
|
|
for (const [filter, callback] of subscriptions) {
|
|
|
|
try {
|
|
|
|
if (filter(exports)) {
|
|
|
|
subscriptions.delete(filter);
|
|
|
|
callback(exports);
|
2022-09-28 16:49:46 -04:00
|
|
|
} else if (typeof exports === "object") {
|
|
|
|
if (exports.default && filter(exports.default)) {
|
2022-09-27 08:34:57 -04:00
|
|
|
subscriptions.delete(filter);
|
2022-09-28 16:49:46 -04:00
|
|
|
callback(exports.default);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const nested in exports) if (nested.length < 3) {
|
|
|
|
if (exports[nested] && filter(exports[nested])) {
|
|
|
|
subscriptions.delete(filter);
|
|
|
|
callback(exports[nested]);
|
|
|
|
}
|
2022-09-27 08:34:57 -04:00
|
|
|
}
|
2022-08-28 20:25:27 -04:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2022-08-29 14:27:47 -04:00
|
|
|
logger.error("Error while firing callback for webpack chunk", err);
|
2022-08-28 20:25:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-09-16 14:52:51 -04:00
|
|
|
modules[id].toString = () => mod.toString();
|
|
|
|
modules[id].original = originalMod;
|
2022-08-28 20:25:27 -04: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 14:27:47 -04: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);
|
2022-08-30 22:07:16 -04:00
|
|
|
if (newCode === code) {
|
|
|
|
logger.warn(`Patch by ${patch.plugin} had no effect: ${replacement.match}`);
|
2022-08-31 17:04:18 -04:00
|
|
|
logger.debug("Function Source:\n", code);
|
2022-08-30 22:07:16 -04:00
|
|
|
} else {
|
|
|
|
code = newCode;
|
2022-09-01 23:11:45 -04:00
|
|
|
mod = (0, eval)(`// Webpack Module ${id} - Patched by ${[...patchedBy].join(", ")}\n${newCode}\n//# sourceURL=WebpackModule${id}`);
|
2022-08-30 22:07:16 -04:00
|
|
|
}
|
2022-08-29 14:27:47 -04:00
|
|
|
} catch (err) {
|
2022-09-01 23:11:45 -04:00
|
|
|
// TODO - More meaningful errors. This probably means moving away from string.replace
|
|
|
|
// in favour of manual matching. Then cut out the context and log some sort of
|
|
|
|
// diff
|
2022-08-29 14:27:47 -04:00
|
|
|
logger.error("Failed to apply patch of", patch.plugin, err);
|
2022-09-01 23:11:45 -04:00
|
|
|
logger.debug("Original Source\n", lastCode);
|
|
|
|
logger.debug("Patched Source\n", code);
|
2022-08-29 14:27:47 -04:00
|
|
|
code = lastCode;
|
|
|
|
mod = lastMod;
|
|
|
|
patchedBy.delete(patch.plugin);
|
|
|
|
}
|
2022-08-28 20:25:27 -04:00
|
|
|
}
|
2022-08-29 14:27:47 -04:00
|
|
|
patches.splice(i--, 1);
|
2022-08-28 20:25:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2022-08-29 14:27:47 -04:00
|
|
|
logger.error("oopsie", err);
|
2022-08-28 20:25:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return handlePush.original.call(window[WEBPACK_CHUNK], chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePush.original = window[WEBPACK_CHUNK].push;
|
|
|
|
Object.defineProperty(window[WEBPACK_CHUNK], "push", {
|
|
|
|
get: () => handlePush,
|
2022-10-05 18:42:58 -04:00
|
|
|
set: v => (handlePush.original = v),
|
2022-08-28 20:25:27 -04:00
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
}
|