Improve ConsoleShortcuts plugin

This commit is contained in:
Vendicated 2023-02-28 00:38:28 +01:00
parent 9aba70dcb1
commit 235d114193
No known key found for this signature in database
GPG Key ID: A1DC0CFB5615D905
2 changed files with 43 additions and 18 deletions

@ -18,6 +18,9 @@
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin from "@utils/types"; 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) => () => { const WEB_ONLY = (f: string) => () => {
throw new Error(`'${f}' is Discord Desktop only.`); throw new Error(`'${f}' is Discord Desktop only.`);
@ -29,19 +32,48 @@ export default definePlugin({
authors: [Devs.Ven], authors: [Devs.Ven],
getShortcuts() { 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 { return {
toClip: IS_WEB ? WEB_ONLY("toClip") : window.DiscordNative.clipboard.copy, wp: Webpack,
fromClip: IS_WEB ? WEB_ONLY("fromClip") : window.DiscordNative.clipboard.read, wpc: Webpack.wreq.c,
wp: Vencord.Webpack, wreq: Webpack.wreq,
wpc: Vencord.Webpack.wreq.c, wpsearch: search,
wreq: Vencord.Webpack.wreq, wpex: extract,
wpsearch: Vencord.Webpack.search,
wpex: Vencord.Webpack.extract,
wpexs: (code: string) => Vencord.Webpack.extract(Vencord.Webpack.findModuleId(code)!), wpexs: (code: string) => Vencord.Webpack.extract(Vencord.Webpack.findModuleId(code)!),
findByProps: Vencord.Webpack.findByProps, find: newFindWrapper(f => f),
find: Vencord.Webpack.find, findAll,
Plugins: Vencord.Plugins, findByProps: newFindWrapper(filters.byProps),
React: Vencord.Webpack.Common.React, 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, Settings: Vencord.Settings,
Api: Vencord.Api, Api: Vencord.Api,
reload: () => location.reload(), reload: () => location.reload(),

@ -307,13 +307,6 @@ export function findByPropsLazy(...props: string[]) {
return findLazy(filters.byProps(...props)); return findLazy(filters.byProps(...props));
} }
/**
* Find all modules that have the specified properties
*/
export function findAllByProps(...props: string[]) {
return findAll(filters.byProps(...props));
}
/** /**
* Find a function by its code * Find a function by its code
*/ */