diff options
author | Vendicated <vendicated@riseup.net> | 2023-02-28 00:38:28 +0100 |
---|---|---|
committer | Vendicated <vendicated@riseup.net> | 2023-02-28 00:38:28 +0100 |
commit | 235d1141930c24b8b8dd292a836f5b6f4b622700 (patch) | |
tree | 8701cf06908cb3ad853b863f6dd7e3670998cc47 | |
parent | 9aba70dcb16c9577095e65b7eef317a20ea33182 (diff) | |
download | Vencord-235d1141930c24b8b8dd292a836f5b6f4b622700.tar.gz Vencord-235d1141930c24b8b8dd292a836f5b6f4b622700.tar.bz2 Vencord-235d1141930c24b8b8dd292a836f5b6f4b622700.zip |
Improve ConsoleShortcuts plugin
-rw-r--r-- | src/plugins/consoleShortcuts.ts | 54 | ||||
-rw-r--r-- | src/webpack/webpack.ts | 7 |
2 files changed, 43 insertions, 18 deletions
diff --git a/src/plugins/consoleShortcuts.ts b/src/plugins/consoleShortcuts.ts index ae0de9f..bfcd1a1 100644 --- a/src/plugins/consoleShortcuts.ts +++ b/src/plugins/consoleShortcuts.ts @@ -18,6 +18,9 @@ import { Devs } from "@utils/constants"; import definePlugin from "@utils/types"; +import * as Webpack from "@webpack"; +import { extract, filters, findAll, search } from "@webpack"; +import { React } from "@webpack/common"; const WEB_ONLY = (f: string) => () => { throw new Error(`'${f}' is Discord Desktop only.`); @@ -29,19 +32,48 @@ export default definePlugin({ authors: [Devs.Ven], getShortcuts() { + function newFindWrapper(filterFactory: (props: any) => Webpack.FilterFn) { + const cache = new Map<string, any>(); + + return function (filterProps: any) { + const cacheKey = String(filterProps); + if (cache.has(cacheKey)) return cache.get(cacheKey); + + const matches = findAll(filterFactory(filterProps)); + + const result = (() => { + switch (matches.length) { + case 0: return null; + case 1: return matches[0]; + default: + const uniqueMatches = [...new Set(matches)]; + if (uniqueMatches.length > 1) + console.warn(`Warning: This filter matches ${matches.length} modules. Make it more specific!\n`, uniqueMatches); + + return matches[0]; + } + })(); + if (result && cacheKey) cache.set(cacheKey, result); + return result; + }; + } + return { - toClip: IS_WEB ? WEB_ONLY("toClip") : window.DiscordNative.clipboard.copy, - fromClip: IS_WEB ? WEB_ONLY("fromClip") : window.DiscordNative.clipboard.read, - wp: Vencord.Webpack, - wpc: Vencord.Webpack.wreq.c, - wreq: Vencord.Webpack.wreq, - wpsearch: Vencord.Webpack.search, - wpex: Vencord.Webpack.extract, + wp: Webpack, + wpc: Webpack.wreq.c, + wreq: Webpack.wreq, + wpsearch: search, + wpex: extract, wpexs: (code: string) => Vencord.Webpack.extract(Vencord.Webpack.findModuleId(code)!), - findByProps: Vencord.Webpack.findByProps, - find: Vencord.Webpack.find, - Plugins: Vencord.Plugins, - React: Vencord.Webpack.Common.React, + find: newFindWrapper(f => f), + findAll, + findByProps: newFindWrapper(filters.byProps), + findAllByProps: (...props: string[]) => findAll(filters.byProps(...props)), + findByCode: newFindWrapper(filters.byCode), + findAllByCode: (code: string) => findAll(filters.byCode(code)), + PluginsApi: Vencord.Plugins, + plugins: Vencord.Plugins.plugins, + React, Settings: Vencord.Settings, Api: Vencord.Api, reload: () => location.reload(), diff --git a/src/webpack/webpack.ts b/src/webpack/webpack.ts index 5aa7dc7..98a0ea8 100644 --- a/src/webpack/webpack.ts +++ b/src/webpack/webpack.ts @@ -308,13 +308,6 @@ export function findByPropsLazy(...props: string[]) { } /** - * Find all modules that have the specified properties - */ -export function findAllByProps(...props: string[]) { - return findAll(filters.byProps(...props)); -} - -/** * Find a function by its code */ export function findByCode(...code: string[]) { |