Equicord/src/plugins/_core/noTrack.ts

138 lines
5.2 KiB
TypeScript
Raw Normal View History

2022-10-22 01:17:06 +02:00
/*
* 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 { definePluginSettings } from "@api/Settings";
import { Devs } from "@utils/constants";
2024-06-28 18:17:38 -03:00
import { Logger } from "@utils/Logger";
2024-06-28 00:53:09 -03:00
import definePlugin, { OptionType, StartAt } from "@utils/types";
2024-06-27 23:58:01 -03:00
const settings = definePluginSettings({
disableAnalytics: {
type: OptionType.BOOLEAN,
description: "Disable Discord's tracking (analytics/'science')",
default: true,
restartNeeded: true
}
});
2022-08-29 22:05:22 +02:00
export default definePlugin({
name: "NoTrack",
description: "Disable Discord's tracking (analytics/'science'), metrics and Sentry crash reporting",
authors: [Devs.Cyn, Devs.Ven, Devs.Nuckyz, Devs.Arrow],
2022-08-31 04:07:16 +02:00
required: true,
settings,
2023-10-25 14:56:20 +02:00
patches: [
2022-09-01 21:27:32 -06:00
{
2023-10-25 14:56:20 +02:00
find: "AnalyticsActionHandlers.handle",
predicate: () => settings.store.disableAnalytics,
2022-09-01 21:27:32 -06:00
replacement: {
2022-09-27 14:34:57 +02:00
match: /^.+$/,
replace: "()=>{}",
2022-09-01 21:27:32 -06:00
},
},
{
find: ".METRICS,",
replacement: [
{
2023-10-25 14:56:20 +02:00
match: /this\._intervalId=/,
replace: "this._intervalId=void 0&&"
},
{
match: /(?:increment|distribution)\(\i(?:,\i)?\){/g,
replace: "$&return;"
}
]
},
{
find: ".installedLogHooks)",
replacement: {
2023-10-25 14:41:57 +01:00
// if getDebugLogging() returns false, the hooks don't get installed.
match: "getDebugLogging(){",
replace: "getDebugLogging(){return false;"
}
},
2024-06-28 00:53:09 -03:00
],
startAt: StartAt.Init,
start() {
2024-06-28 18:17:38 -03:00
// Sentry is initialized in its own WebpackInstance.
// It has everything it needs preloaded, so, it doesn't include any chunk loading functionality.
// Because of that, its WebpackInstance doesnt export wreq.m or wreq.c
// To circuvent this and disable Sentry we are gonna hook when wreq.g of its WebpackInstance is set.
// When that happens we are gonna obtain a reference to its internal module cache (wreq.c) and proxy its prototype,
// so, when the first require to initialize the Sentry is attempted, we are gonna forcefully throw an error and abort everything.
Object.defineProperty(Function.prototype, "g", {
configurable: true,
set(v: any) {
Object.defineProperty(this, "g", {
value: v,
configurable: true,
enumerable: true,
writable: true
});
// Ensure this is most likely the Sentry WebpackInstance.
// Function.g is a very generic property and is not uncommon for another WebpackInstance (or even a React component: <g></g>) to include it
const { stack } = new Error();
if (!(stack?.includes("discord.com") || stack?.includes("discordapp.com")) || this.c != null || !String(this).includes("exports:{}")) {
return;
}
const cacheExtractSym = Symbol("vencord.cacheExtract");
Object.defineProperty(Object.prototype, cacheExtractSym, {
configurable: true,
get() {
// One more condition to check if this is the Sentry WebpackInstance
if (Array.isArray(this)) {
return { exports: {} };
}
new Logger("NoTrack", "#8caaee").info("Disabling Sentry by proxying its WebpackInstance cache");
Object.setPrototypeOf(this, new Proxy(this, {
get() {
throw new Error("Sentry successfully disabled");
}
}));
Reflect.deleteProperty(Object.prototype, cacheExtractSym);
Reflect.deleteProperty(window, "DiscordSentry");
return { exports: {} };
}
});
// WebpackRequire our fake module id
this(cacheExtractSym);
}
});
2024-06-28 00:53:09 -03:00
Object.defineProperty(window, "DiscordSentry", {
configurable: true,
2024-06-28 18:17:38 -03:00
2024-06-28 00:53:09 -03:00
set() {
2024-06-28 18:17:38 -03:00
new Logger("NoTrack", "#8caaee").error("Failed to disable Sentry. Falling back to deleting window.DiscordSentry");
2024-06-28 00:53:09 -03:00
Reflect.deleteProperty(window, "DiscordSentry");
}
});
}
2022-09-01 21:27:32 -06:00
});