Plugin

ConsoleJanitor

Disables annoying console messages/errors

Developers Console Utility
index.tsx
Download

Source

src/plugins/consoleJanitor/index.tsx
1import { definePluginSettings } from "@api/Settings";
2import ErrorBoundary from "@components/ErrorBoundary";
3import { SettingsSection } from "@components/settings/tabs/plugins/components/Common";
4import { Devs } from "@utils/constants";
5import definePlugin, { defineDefault, OptionType, StartAt } from "@utils/types";
6import { Checkbox, Text } from "@webpack/common";
7
8const Noop = () => { };
9const NoopLogger = {
10 logDangerously: Noop,
11 log: Noop,
12 verboseDangerously: Noop,
13 verbose: Noop,
14 info: Noop,
15 warn: Noop,
16 error: Noop,
17 trace: Noop,
18 time: Noop,
19 fileOnly: Noop
20};
21
22const logAllow = new Set();
23
24interface AllowLevels {
25 error: boolean;
26 warn: boolean;
27 trace: boolean;
28 log: boolean;
29 info: boolean;
30 debug: boolean;
31}
32
33interface AllowLevelSettingProps {
34 settingKey: keyof AllowLevels;
35}
36
37function AllowLevelSetting({ settingKey }: AllowLevelSettingProps) {
38 const { allowLevel } = settings.use(["allowLevel"]);
39 const value = allowLevel[settingKey];
40
41 return (
42 <Checkbox
43 value={value}
44 onChange={(_, newValue) => settings.store.allowLevel[settingKey] = newValue}
45 size={20}
46 >
47 <Text variant="text-sm/normal">{settingKey[0].toUpperCase() + settingKey.slice(1)}</Text>
48 </Checkbox>
49 );
50}
51
52const AllowLevelSettings = ErrorBoundary.wrap(() => {
53 return (
54 <SettingsSection name="Filter List" id="filterList" description="Always allow loggers of these types">
55 <div style={{ display: "flex", flexDirection: "row" }}>
56 {Object.keys(settings.store.allowLevel).map(key => (
57 <AllowLevelSetting key={key} settingKey={key as keyof AllowLevels} />
58 ))}
59 </div>
60 </SettingsSection>
61 );
62});
63
64const settings = definePluginSettings({
65 disableLoggers: {
66 type: OptionType.BOOLEAN,
67 description: "Disables Discords loggers",
68 default: false,
69 restartNeeded: true
70 },
71 disableSpotifyLogger: {
72 type: OptionType.BOOLEAN,
73 description: "Disable the Spotify logger, which leaks account information and access token",
74 default: true,
75 restartNeeded: true
76 },
77 whitelistedLoggers: {
78 type: OptionType.STRING,
79 description: "Semicolon (;) separated list of loggers to allow even if others are hidden",
80 default: "GatewaySocket; Routing/Utils",
81 multiline: true,
82 onChange(newVal: string) {
83 logAllow.clear();
84 newVal.split(";").map(x => x.trim()).forEach(logAllow.add.bind(logAllow));
85 }
86 },
87 allowLevel: {
88 type: OptionType.COMPONENT,
89 component: AllowLevelSettings,
90 default: defineDefault<AllowLevels>({
91 error: true,
92 warn: false,
93 trace: false,
94 log: false,
95 info: false,
96 debug: false
97 })
98 }
99});
100
101export default definePlugin({
102 name: "ConsoleJanitor",
103 description: "Disables annoying console messages/errors",
104 authors: [Devs.Nuckyz, Devs.sadan],
105 tags: ["Developers", "Console", "Utility"],
106 settings,
107
108 startAt: StartAt.Init,
109 start() {
110 logAllow.clear();
111 this.settings.store.whitelistedLoggers?.split(";").map(x => x.trim()).forEach(logAllow.add.bind(logAllow));
112 },
113
114 Noop,
115 NoopLogger: () => NoopLogger,
116
117 shouldLog(logger: string, level: keyof AllowLevels) {
118 return logAllow.has(logger) || settings.store.allowLevel[level] === true;
119 },
120
121 patches: [
122 {
123 find: "https:class="ts-cmt">//github.com/highlightjs/highlight.js/issues/2277",
124 replacement: {
125 match: /\(console.log\(`Deprecated.+?`\),/,
126 replace: "("
127 }
128 },
129 {
130 find: &#039;The "interpolate" function is deprecated in v10 (use "to" instead)&#039;,
131 replacement: {
132 match: /,console.warn\(&#039;react-spring: The "interpolate" function is deprecated in v10 \(use "to" instead\)&#039;\)/,
133 replace: ""
134 }
135 },
136 {
137 find: &#039;console.warn("Window state not initialized"&#039;,
138 replacement: {
139 match: /console\.warn\("Window state not initialized",\i\),/,
140 replace: ""
141 }
142 },
143 {
144 find: "is not a valid locale.",
145 replacement: [
146 {
147 match: /\i\.error(?=\(`\$\{\i\} is not a valid locale.`)/,
148 replace: "$self.Noop"
149 }
150 ]
151 },
152 {
153 find: &#039;"AppCrashedFatalReport: getLastCrash not supported."&#039;,
154 replacement: {
155 match: /console\.log(?=\("AppCrashedFatalReport: getLastCrash not supported\."\))/,
156 replace: "$self.Noop"
157 }
158 },
159 {
160 find: "RPCServer:WSS",
161 replacement: {
162 match: /\i\.error\(`Error: \$\{(\i)\.message\}/,
163 replace: &#039;!$1.message.includes("EADDRINUSE")&&$&&#039;
164 }
165 },
166 {
167 find: "Tried getting Dispatch instance before instantiated",
168 replacement: {
169 match: /null==\i&&\i\.warn\("Tried getting Dispatch instance before instantiated"\),/,
170 replace: ""
171 }
172 },
173 {
174 find: "Unable to determine render window for element",
175 replacement: {
176 match: /console\.warn\("Unable to determine render window for element",\i\),/,
177 replace: ""
178 }
179 },
180 {
181 find: "failed to send analytics events",
182 replacement: [
183 {
184 match: /console\.error\(`\[analytics\] failed to send analytics events query: \$\{\i\}`\)/,
185 replace: ""
186 }
187 ]
188 },
189 {
190 find: "Slow dispatch on",
191 replacement: [
192 {
193 match: /\i\.totalTime>\d+?&&\i\.verbose\([`"]Slow dispatch on.{0,55}\);/,
194 replace: ""
195 },
196 ]
197 },
198 // Patches Discord generic logger function
199 {
200 find: &#039;"file-only"!==&#039;,
201 predicate: () => settings.store.disableLoggers,
202 replacement: {
203 match: /(?<=&&)(?=console)/,
204 replace: "$self.shouldLog(arguments[0],arguments[1])&&"
205 }
206 },
207 {
208 find: &#039;("Spotify")&#039;,
209 predicate: () => settings.store.disableSpotifyLogger,
210 replacement: {
211 match: /new \i\.\i\("Spotify"\)/,
212 replace: "$self.NoopLogger()"
213 }
214 }
215 ]
216});
217