From ef353f1d66dbf1d14e528830d267aac518ed1beb Mon Sep 17 00:00:00 2001 From: Ven Date: Sat, 1 Oct 2022 02:27:28 +0200 Subject: Better authors field (#18) --- src/Vencord.ts | 2 +- src/plugins/STFU.ts | 3 +- src/plugins/apiMessageEvents.ts | 3 +- src/plugins/apiNotices.ts | 3 +- src/plugins/banger.ts | 5 +- src/plugins/betterGifAltText.ts | 5 +- src/plugins/clickableRoleDot.ts | 3 +- src/plugins/experiments.ts | 3 +- src/plugins/folderSvgIcon.ts | 5 +- src/plugins/ify.ts | 3 +- src/plugins/messageActions.ts | 3 +- src/plugins/nitroBypass.ts | 3 +- src/plugins/noTrack.ts | 3 +- src/plugins/settings.ts | 3 +- src/plugins/silentTyping.ts | 3 +- src/plugins/unindent.ts | 3 +- src/plugins/viewIcons.tsx | 3 +- src/utils/IpcEvents.ts | 2 +- src/utils/constants.ts | 20 ++++++ src/utils/patchWebpack.ts | 148 ---------------------------------------- src/utils/types.ts | 7 +- src/webpack/patchWebpack.ts | 148 ++++++++++++++++++++++++++++++++++++++++ src/webpack/webpack.ts | 6 +- 23 files changed, 216 insertions(+), 171 deletions(-) delete mode 100644 src/utils/patchWebpack.ts create mode 100644 src/webpack/patchWebpack.ts (limited to 'src') diff --git a/src/Vencord.ts b/src/Vencord.ts index cb06182..2798e70 100644 --- a/src/Vencord.ts +++ b/src/Vencord.ts @@ -7,7 +7,7 @@ import { startAllPlugins } from "./plugins"; export { Settings }; -import "./utils/patchWebpack"; +import "./webpack/patchWebpack"; import "./utils/quickCss"; import { checkForUpdates, UpdateLogger } from './utils/updater'; import { onceReady } from "./webpack"; diff --git a/src/plugins/STFU.ts b/src/plugins/STFU.ts index afa29f5..96325ff 100644 --- a/src/plugins/STFU.ts +++ b/src/plugins/STFU.ts @@ -1,9 +1,10 @@ import definePlugin from "../utils/types"; +import { Devs } from '../utils/constants'; export default definePlugin({ name: "STFU", description: "Disables the 'HOLD UP' banner in the console", - author: "Vendicated", + authors: [Devs.Ven], patches: [{ find: "setDevtoolsCallbacks", replacement: { diff --git a/src/plugins/apiMessageEvents.ts b/src/plugins/apiMessageEvents.ts index 98144e2..154e601 100644 --- a/src/plugins/apiMessageEvents.ts +++ b/src/plugins/apiMessageEvents.ts @@ -1,9 +1,10 @@ +import { Devs } from "../utils/constants"; import definePlugin from "../utils/types"; export default definePlugin({ name: "MessageEventsAPI", description: "Api required by anything using message events.", - author: "ArjixWasTaken", + authors: [Devs.Arjix], patches: [ { find: "sendMessage:function", diff --git a/src/plugins/apiNotices.ts b/src/plugins/apiNotices.ts index d58ac4b..1b4f115 100644 --- a/src/plugins/apiNotices.ts +++ b/src/plugins/apiNotices.ts @@ -1,9 +1,10 @@ +import { Devs } from "../utils/constants"; import definePlugin from "../utils/types"; export default definePlugin({ name: "ApiNotices", description: "Fixes notices being automatically dismissed", - author: "Vendicated", + authors: [Devs.Ven], required: true, patches: [ { diff --git a/src/plugins/banger.ts b/src/plugins/banger.ts index d87ef99..2ca2921 100644 --- a/src/plugins/banger.ts +++ b/src/plugins/banger.ts @@ -3,7 +3,10 @@ import definePlugin from "../utils/types"; export default definePlugin({ name: "BANger", description: "Replaces the GIF in the ban dialogue with a custom one.", - author: "Xinto", + authors: [{ + name: "Xinto", + id: 423915768191647755n + }], patches: [ { find: "BanConfirm", diff --git a/src/plugins/betterGifAltText.ts b/src/plugins/betterGifAltText.ts index 4754042..08fa07b 100644 --- a/src/plugins/betterGifAltText.ts +++ b/src/plugins/betterGifAltText.ts @@ -1,8 +1,9 @@ +import { Devs } from "../utils/constants"; import definePlugin from "../utils/types"; export default definePlugin({ name: "BetterGifAltText", - author: "Vendicated", + authors: [Devs.Ven], description: "Change GIF alt text from simply being 'GIF' to containing the gif tags / filename", patches: [ @@ -30,7 +31,7 @@ export default definePlugin({ let url: string = props.original || props.src; try { url = decodeURI(url); - } catch {} + } catch { } let name = url .slice(url.lastIndexOf("/") + 1) diff --git a/src/plugins/clickableRoleDot.ts b/src/plugins/clickableRoleDot.ts index 800a742..3fc4bef 100644 --- a/src/plugins/clickableRoleDot.ts +++ b/src/plugins/clickableRoleDot.ts @@ -1,9 +1,10 @@ +import { Devs } from "../utils/constants"; import definePlugin from "../utils/types"; import { Toasts } from '../webpack/common'; export default definePlugin({ name: "ClickableRoleDot", - author: "Vendicated", + authors: [Devs.Ven], description: "Makes RoleDots (Accessibility Feature) copy colour to clipboard on click", patches: [ diff --git a/src/plugins/experiments.ts b/src/plugins/experiments.ts index 3406eec..37a59c7 100644 --- a/src/plugins/experiments.ts +++ b/src/plugins/experiments.ts @@ -1,8 +1,9 @@ +import { Devs } from "../utils/constants"; import definePlugin from '../utils/types'; export default definePlugin({ name: "Experiments", - author: "Vendicated, Megumin", + authors: [Devs.Ven, Devs.Megu], description: "Enable Experiments", patches: [{ find: "Object.defineProperties(this,{isDeveloper", diff --git a/src/plugins/folderSvgIcon.ts b/src/plugins/folderSvgIcon.ts index 1f91cab..208efaa 100644 --- a/src/plugins/folderSvgIcon.ts +++ b/src/plugins/folderSvgIcon.ts @@ -3,7 +3,10 @@ import definePlugin from "../utils/types"; export default definePlugin({ name: "Folder SVG Icon", description: "Doesn't show the small guild icons in folders", - author: "botato", + authors: [{ + name: "botato", + id: 440990343899643943n + }], patches: [{ find: "().expandedFolderIconWrapper", replacement: [{ diff --git a/src/plugins/ify.ts b/src/plugins/ify.ts index e0d5481..40a9708 100644 --- a/src/plugins/ify.ts +++ b/src/plugins/ify.ts @@ -1,9 +1,10 @@ +import { Devs } from "../utils/constants"; import definePlugin from "../utils/types"; export default definePlugin({ name: "Ify", description: "Disabes Spotify auto-pausing and premium checks", - author: "Cynosphere", + authors: [Devs.Cyn], patches: [ { find: '.displayName="SpotifyStore"', diff --git a/src/plugins/messageActions.ts b/src/plugins/messageActions.ts index 569d3a9..4df3853 100644 --- a/src/plugins/messageActions.ts +++ b/src/plugins/messageActions.ts @@ -1,4 +1,5 @@ import { addClickListener, removeClickListener } from '../api/MessageEvents'; +import { Devs } from "../utils/constants"; import definePlugin from "../utils/types"; import { find, findByProps } from "../webpack"; import { UserStore } from "../webpack/common"; @@ -10,7 +11,7 @@ const keyup = (e: KeyboardEvent) => e.key === "Backspace" && (isDeletePressed = export default definePlugin({ name: "MessageQuickActions", description: "Quick Delete, Quick edit", - author: "Vendicated", + authors: [Devs.Ven], dependencies: ["MessageEventsAPI"], start() { diff --git a/src/plugins/nitroBypass.ts b/src/plugins/nitroBypass.ts index d5c4d22..ebed095 100644 --- a/src/plugins/nitroBypass.ts +++ b/src/plugins/nitroBypass.ts @@ -1,10 +1,11 @@ import { addPreSendListener, addPreEditListener, SendListener, removePreSendListener, removePreEditListener } from '../api/MessageEvents'; import { findByProps } from "../webpack"; import definePlugin from "../utils/types"; +import { Devs } from '../utils/constants'; export default definePlugin({ name: "Nitro Bypass", - author: "ArjixWasTaken", + authors: [Devs.Arjix], description: "Allows you to stream in nitro quality and send fake emojis.", dependencies: ["MessageEventsAPI"], patches: [ diff --git a/src/plugins/noTrack.ts b/src/plugins/noTrack.ts index 49f542e..13b52f8 100644 --- a/src/plugins/noTrack.ts +++ b/src/plugins/noTrack.ts @@ -1,9 +1,10 @@ import definePlugin from "../utils/types"; +import { Devs } from '../utils/constants'; export default definePlugin({ name: "NoTrack", description: "Disable Discord's tracking and crash reporting", - author: "Cynosphere", + authors: [Devs.Cyn], required: true, patches: [ { diff --git a/src/plugins/settings.ts b/src/plugins/settings.ts index 1281f4e..afefa91 100644 --- a/src/plugins/settings.ts +++ b/src/plugins/settings.ts @@ -1,10 +1,11 @@ import definePlugin from "../utils/types"; import gitHash from "git-hash"; +import { Devs } from '../utils/constants'; export default definePlugin({ name: "Settings", description: "Adds Settings UI and debug info", - author: "Vendicated", + authors: [Devs.Ven], required: true, patches: [{ find: "().versionHash", diff --git a/src/plugins/silentTyping.ts b/src/plugins/silentTyping.ts index df233a4..56ef30d 100644 --- a/src/plugins/silentTyping.ts +++ b/src/plugins/silentTyping.ts @@ -1,8 +1,9 @@ import definePlugin from '../utils/types'; +import { Devs } from '../utils/constants'; export default definePlugin({ name: "SilentTyping", - author: "Vendicated", + authors: [Devs.Ven], description: "Hide that you are typing", patches: [{ find: "startTyping:", diff --git a/src/plugins/unindent.ts b/src/plugins/unindent.ts index 3cb34f3..d97de11 100644 --- a/src/plugins/unindent.ts +++ b/src/plugins/unindent.ts @@ -1,10 +1,11 @@ import definePlugin from "../utils/types"; import { addPreSendListener, addPreEditListener, MessageObject, removePreSendListener, removePreEditListener } from '../api/MessageEvents'; +import { Devs } from '../utils/constants'; export default definePlugin({ name: "Unindent", description: "Trims leading indentation from codeblocks", - author: "Vendicated", + authors: [Devs.Ven], patches: [ { find: "inQuote:", diff --git a/src/plugins/viewIcons.tsx b/src/plugins/viewIcons.tsx index f41ae85..2ac0ab1 100644 --- a/src/plugins/viewIcons.tsx +++ b/src/plugins/viewIcons.tsx @@ -1,10 +1,11 @@ +import { Devs } from "../utils/constants"; import IpcEvents from "../utils/IpcEvents"; import definePlugin from '../utils/types'; const OPEN_URL = "Vencord.Plugins.plugins.ViewIcons.openImage("; export default definePlugin({ name: "ViewIcons", - author: "Vendicated", + authors: [Devs.Ven], description: "Makes Avatars/Banners in user profiles clickable, and adds Guild Context Menu Entries to View Banner/Icon.", openImage(url: string) { diff --git a/src/utils/IpcEvents.ts b/src/utils/IpcEvents.ts index b0a53f2..24e52b9 100644 --- a/src/utils/IpcEvents.ts +++ b/src/utils/IpcEvents.ts @@ -8,7 +8,7 @@ function strEnum>(obj: T): T { o[key] = obj[key] as any; o[obj[key]] = key as any; }; - return o; + return Object.freeze(o); } export default strEnum({ diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 3d60e29..57cd9b7 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -1,2 +1,22 @@ export const WEBPACK_CHUNK = "webpackChunkdiscord_app"; export const REACT_GLOBAL = "Vencord.Webpack.Common.React"; + +// Add yourself here if you made more than one plugin +export const Devs = Object.freeze({ + Ven: { + name: "Vendicated", + id: 343383572805058560n + }, + Arjix: { + name: "ArjixWasTaken", + id: 674710789138939916n + }, + Cyn: { + name: "Cynosphere", + id: 150745989836308480n + }, + Megu: { + name: "Megumin", + id: 545581357812678656n + } +}); diff --git a/src/utils/patchWebpack.ts b/src/utils/patchWebpack.ts deleted file mode 100644 index 0508968..0000000 --- a/src/utils/patchWebpack.ts +++ /dev/null @@ -1,148 +0,0 @@ -import { WEBPACK_CHUNK } from './constants'; -import Logger from "./logger"; -import { _initWebpack } from "../webpack"; - -let webpackChunk: any[]; - -const logger = new Logger("WebpackInterceptor", "#8caaee"); - -Object.defineProperty(window, WEBPACK_CHUNK, { - get: () => webpackChunk, - set: (v) => { - if (v?.push !== Array.prototype.push) { - logger.info(`Patching ${WEBPACK_CHUNK}.push`); - _initWebpack(v); - 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]; - const { subscriptions, listeners } = Vencord.Webpack; - const { patches } = Vencord.Plugins; - - for (const id in modules) { - let mod = modules[id]; - // 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", ""); - 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; - - logger.error("Error in patched chunk", err); - return originalMod(module, exports, require); - } - - // 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; - } - - for (const callback of listeners) { - try { - callback(exports); - } catch (err) { - logger.error("Error in webpack listener", err); - } - } - for (const [filter, callback] of subscriptions) { - try { - if (filter(exports)) { - subscriptions.delete(filter); - callback(exports); - } else if (typeof exports === "object") { - if (exports.default && filter(exports.default)) { - subscriptions.delete(filter); - callback(exports.default); - } - - for (const nested in exports) if (nested.length < 3) { - if (exports[nested] && filter(exports[nested])) { - subscriptions.delete(filter); - callback(exports[nested]); - } - } - } - } catch (err) { - logger.error("Error while firing callback for webpack chunk", err); - } - } - }; - modules[id].toString = () => mod.toString(); - modules[id].original = originalMod; - - for (let i = 0; i < patches.length; i++) { - const patch = patches[i]; - if (code.includes(patch.find)) { - patchedBy.add(patch.plugin); - // @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); - if (newCode === code) { - logger.warn(`Patch by ${patch.plugin} had no effect: ${replacement.match}`); - logger.debug("Function Source:\n", code); - } else { - code = newCode; - mod = (0, eval)(`// Webpack Module ${id} - Patched by ${[...patchedBy].join(", ")}\n${newCode}\n//# sourceURL=WebpackModule${id}`); - } - } catch (err) { - // 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 - logger.error("Failed to apply patch of", patch.plugin, err); - logger.debug("Original Source\n", lastCode); - logger.debug("Patched Source\n", code); - code = lastCode; - mod = lastMod; - patchedBy.delete(patch.plugin); - } - } - patches.splice(i--, 1); - } - } - } - } catch (err) { - logger.error("oopsie", err); - } - - 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 - }); -} - diff --git a/src/utils/types.ts b/src/utils/types.ts index 05441e8..a6b79c5 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -14,6 +14,11 @@ export interface Patch { replacement: PatchReplacement | PatchReplacement[]; } +export interface PluginAuthor { + name: string; + id: BigInt; +} + export interface Plugin extends PluginDef { patches?: Patch[]; started: boolean; @@ -22,7 +27,7 @@ export interface Plugin extends PluginDef { interface PluginDef { name: string; description: string; - author: string; + authors: PluginAuthor[]; start?(): void; stop?(): void; patches?: Omit[]; diff --git a/src/webpack/patchWebpack.ts b/src/webpack/patchWebpack.ts new file mode 100644 index 0000000..469b930 --- /dev/null +++ b/src/webpack/patchWebpack.ts @@ -0,0 +1,148 @@ +import { WEBPACK_CHUNK } from '../utils/constants'; +import Logger from "../utils/logger"; +import { _initWebpack } from "."; + +let webpackChunk: any[]; + +const logger = new Logger("WebpackInterceptor", "#8caaee"); + +Object.defineProperty(window, WEBPACK_CHUNK, { + get: () => webpackChunk, + set: (v) => { + if (v?.push !== Array.prototype.push) { + logger.info(`Patching ${WEBPACK_CHUNK}.push`); + _initWebpack(v); + 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]; + const { subscriptions, listeners } = Vencord.Webpack; + const { patches } = Vencord.Plugins; + + for (const id in modules) { + let mod = modules[id]; + // 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", ""); + 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; + + logger.error("Error in patched chunk", err); + return originalMod(module, exports, require); + } + + // 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; + } + + for (const callback of listeners) { + try { + callback(exports); + } catch (err) { + logger.error("Error in webpack listener", err); + } + } + for (const [filter, callback] of subscriptions) { + try { + if (filter(exports)) { + subscriptions.delete(filter); + callback(exports); + } else if (typeof exports === "object") { + if (exports.default && filter(exports.default)) { + subscriptions.delete(filter); + callback(exports.default); + } + + for (const nested in exports) if (nested.length < 3) { + if (exports[nested] && filter(exports[nested])) { + subscriptions.delete(filter); + callback(exports[nested]); + } + } + } + } catch (err) { + logger.error("Error while firing callback for webpack chunk", err); + } + } + }; + modules[id].toString = () => mod.toString(); + modules[id].original = originalMod; + + for (let i = 0; i < patches.length; i++) { + const patch = patches[i]; + if (code.includes(patch.find)) { + patchedBy.add(patch.plugin); + // @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); + if (newCode === code) { + logger.warn(`Patch by ${patch.plugin} had no effect: ${replacement.match}`); + logger.debug("Function Source:\n", code); + } else { + code = newCode; + mod = (0, eval)(`// Webpack Module ${id} - Patched by ${[...patchedBy].join(", ")}\n${newCode}\n//# sourceURL=WebpackModule${id}`); + } + } catch (err) { + // 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 + logger.error("Failed to apply patch of", patch.plugin, err); + logger.debug("Original Source\n", lastCode); + logger.debug("Patched Source\n", code); + code = lastCode; + mod = lastMod; + patchedBy.delete(patch.plugin); + } + } + patches.splice(i--, 1); + } + } + } + } catch (err) { + logger.error("oopsie", err); + } + + 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 + }); +} + diff --git a/src/webpack/webpack.ts b/src/webpack/webpack.ts index ea5a7e3..9be576a 100644 --- a/src/webpack/webpack.ts +++ b/src/webpack/webpack.ts @@ -43,7 +43,7 @@ export function _initWebpack(instance: typeof window.webpackChunkdiscord_app) { export function find(filter: FilterFn, getDefault = true) { if (typeof filter !== "function") - throw new Error("Invalid filter. Expected a function got", filter); + throw new Error("Invalid filter. Expected a function got " + typeof filter); for (const key in cache) { const mod = cache[key]; @@ -70,7 +70,7 @@ export function find(filter: FilterFn, getDefault = true) { // TODO fix export function findAll(filter: FilterFn, getDefault = true) { - if (typeof filter !== "function") throw new Error("Invalid filter. Expected a function got", filter); + if (typeof filter !== "function") throw new Error("Invalid filter. Expected a function got " + typeof filter); const ret = [] as any[]; for (const key in cache) { @@ -108,7 +108,7 @@ export function findByDisplayName(deezNuts: string) { export function waitFor(filter: string | string[] | FilterFn, callback: CallbackFn) { if (typeof filter === "string") filter = filters.byProps([filter]); else if (Array.isArray(filter)) filter = filters.byProps(filter); - else if (typeof filter !== "function") throw new Error("filter must be a string, string[] or function, got", filter); + else if (typeof filter !== "function") throw new Error("filter must be a string, string[] or function, got " + typeof filter); const existing = find(filter!); if (existing) return void callback(existing); -- cgit