Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
TheColorman committed Jun 28, 2024
2 parents 33cedd5 + df6ffd9 commit 7746192
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 80 deletions.
21 changes: 12 additions & 9 deletions src/plugins/_core/noTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { definePluginSettings } from "@api/Settings";
import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types";
import definePlugin, { OptionType, StartAt } from "@utils/types";

const settings = definePluginSettings({
disableAnalytics: {
Expand Down Expand Up @@ -46,13 +46,6 @@ export default definePlugin({
replace: "()=>{}",
},
},
{
find: "window.DiscordSentry=",
replacement: {
match: /^.+$/,
replace: "()=>{}",
}
},
{
find: ".METRICS,",
replacement: [
Expand All @@ -74,5 +67,15 @@ export default definePlugin({
replace: "getDebugLogging(){return false;"
}
},
]
],

startAt: StartAt.Init,
start() {
Object.defineProperty(window, "DiscordSentry", {
configurable: true,
set() {
Reflect.deleteProperty(window, "DiscordSentry");
}
});
}
});
4 changes: 2 additions & 2 deletions src/plugins/showHiddenThings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ export default definePlugin({
find: '"pepe","nude"',
predicate: () => settings.store.disableDisallowedDiscoveryFilters,
replacement: {
match: /\?\["pepe",.+?\]/,
replace: "?[]",
match: /(?<=[?=])\["pepe",.+?\]/,
replace: "[]",
},
},
// patch request that queries if term is allowed
Expand Down
105 changes: 36 additions & 69 deletions src/webpack/patchWebpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { WEBPACK_CHUNK } from "@utils/constants";
import { Logger } from "@utils/Logger";
import { canonicalizeMatch, canonicalizeReplacement } from "@utils/patches";
import { canonicalizeReplacement } from "@utils/patches";
import { PatchReplacement } from "@utils/types";
import { WebpackInstance } from "discord-types/other";

Expand All @@ -27,7 +27,6 @@ import { patches } from "../plugins";
import { _initWebpack, beforeInitListeners, factoryListeners, moduleListeners, subscriptions, wreq } from ".";

const logger = new Logger("WebpackInterceptor", "#8caaee");
const initCallbackRegex = canonicalizeMatch(/{return \i\(".+?"\)}/);

let webpackChunk: any[];

Expand All @@ -53,71 +52,6 @@ Object.defineProperty(window, WEBPACK_CHUNK, {
}
});

// wreq.O is the webpack onChunksLoaded function
// Discord uses it to await for all the chunks to be loaded before initializing the app
// We monkey patch it to also monkey patch the initialize app callback to get immediate access to the webpack require and run our listeners before doing it
Object.defineProperty(Function.prototype, "O", {
configurable: true,

set(onChunksLoaded: any) {
// When using react devtools or other extensions, or even when discord loads the sentry, we may also catch their webpack here.
// This ensures we actually got the right one
// this.e (wreq.e) is the method for loading a chunk, and only the main webpack has it
const { stack } = new Error();
if ((stack?.includes("discord.com") || stack?.includes("discordapp.com")) && String(this.e).includes("Promise.all")) {
logger.info("Found main WebpackRequire.onChunksLoaded");

delete (Function.prototype as any).O;

const originalOnChunksLoaded = onChunksLoaded;
onChunksLoaded = function (this: unknown, result: any, chunkIds: string[], callback: () => any, priority: number) {
if (callback != null && initCallbackRegex.test(callback.toString())) {
Object.defineProperty(this, "O", {
value: originalOnChunksLoaded,
configurable: true
});

const wreq = this as WebpackInstance;

const originalCallback = callback;
callback = function (this: unknown) {
logger.info("Patched initialize app callback invoked, initializing our internal references to WebpackRequire and running beforeInitListeners");
_initWebpack(wreq);

for (const beforeInitListener of beforeInitListeners) {
beforeInitListener(wreq);
}

originalCallback.apply(this, arguments as any);
};

callback.toString = originalCallback.toString.bind(originalCallback);
arguments[2] = callback;
}

originalOnChunksLoaded.apply(this, arguments as any);
};

onChunksLoaded.toString = originalOnChunksLoaded.toString.bind(originalOnChunksLoaded);

// Returns whether a chunk has been loaded
Object.defineProperty(onChunksLoaded, "j", {
set(v) {
delete onChunksLoaded.j;
onChunksLoaded.j = v;
originalOnChunksLoaded.j = v;
},
configurable: true
});
}

Object.defineProperty(this, "O", {
value: onChunksLoaded,
configurable: true
});
}
});

// wreq.m is the webpack module factory.
// normally, this is populated via webpackGlobal.push, which we patch below.
// However, Discord has their .m prepopulated.
Expand All @@ -133,13 +67,46 @@ Object.defineProperty(Function.prototype, "m", {
// This ensures we actually got the right one
const { stack } = new Error();
if ((stack?.includes("discord.com") || stack?.includes("discordapp.com")) && !Array.isArray(v)) {
logger.info("Found Webpack module factory", stack.match(/\/assets\/(.+?\.js)/)?.[1] ?? "");
const fileName = stack.match(/\/assets\/(.+?\.js)/)?.[1] ?? "";

logger.info("Found Webpack module factory", fileName);
patchFactories(v);

// Define a setter for the bundlePath property of WebpackRequire. Only the main Webpack has this property.
// So if the setter is called, this means we can initialize the internal references to WebpackRequire.
Object.defineProperty(this, "p", {
configurable: true,

set(this: WebpackInstance, bundlePath: string) {
Object.defineProperty(this, "p", {
value: bundlePath,
configurable: true,
enumerable: true,
writable: true
});

clearTimeout(setterTimeout);

if (bundlePath !== "/assets/") return;

logger.info(`Main Webpack found in ${fileName}, initializing internal references to WebpackRequire`);
_initWebpack(this);

for (const beforeInitListener of beforeInitListeners) {
beforeInitListener(this);
}
}
});
// setImmediate to clear this property setter if this is not the main Webpack.
// If this is the main Webpack, wreq.p will always be set before the timeout runs.
const setterTimeout = setTimeout(() => Reflect.deleteProperty(this, "p"), 0);
}

Object.defineProperty(this, "m", {
value: v,
configurable: true
configurable: true,
enumerable: true,
writable: true
});
}
});
Expand Down

0 comments on commit 7746192

Please sign in to comment.