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/>.
|
|
|
|
*/
|
|
|
|
|
2022-11-28 13:37:55 +01:00
|
|
|
import { Devs } from "@utils/constants";
|
2023-04-04 01:16:29 +02:00
|
|
|
import { relaunch } from "@utils/native";
|
2023-04-15 00:02:08 -03:00
|
|
|
import { canonicalizeMatch, canonicalizeReplace, canonicalizeReplacement } from "@utils/patches";
|
2022-11-28 13:37:55 +01:00
|
|
|
import definePlugin from "@utils/types";
|
2023-02-28 00:38:28 +01:00
|
|
|
import * as Webpack from "@webpack";
|
|
|
|
import { extract, filters, findAll, search } from "@webpack";
|
2023-04-15 02:26:46 +02:00
|
|
|
import { React, ReactDOM } from "@webpack/common";
|
|
|
|
import type { ComponentType } from "react";
|
2022-10-01 03:58:07 +02:00
|
|
|
|
2022-10-05 14:25:37 +02:00
|
|
|
const WEB_ONLY = (f: string) => () => {
|
|
|
|
throw new Error(`'${f}' is Discord Desktop only.`);
|
|
|
|
};
|
|
|
|
|
2022-10-01 03:58:07 +02:00
|
|
|
export default definePlugin({
|
|
|
|
name: "ConsoleShortcuts",
|
|
|
|
description: "Adds shorter Aliases for many things on the window. Run `shortcutList` for a list.",
|
|
|
|
authors: [Devs.Ven],
|
|
|
|
|
|
|
|
getShortcuts() {
|
2023-03-11 14:18:32 +01:00
|
|
|
function newFindWrapper(filterFactory: (...props: any[]) => Webpack.FilterFn) {
|
|
|
|
const cache = new Map<string, unknown>();
|
2023-02-28 00:38:28 +01:00
|
|
|
|
2023-03-11 14:18:32 +01:00
|
|
|
return function (...filterProps: unknown[]) {
|
2023-02-28 00:38:28 +01:00
|
|
|
const cacheKey = String(filterProps);
|
|
|
|
if (cache.has(cacheKey)) return cache.get(cacheKey);
|
|
|
|
|
2023-03-11 14:18:32 +01:00
|
|
|
const matches = findAll(filterFactory(...filterProps));
|
2023-02-28 00:38:28 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-15 02:26:46 +02:00
|
|
|
let fakeRenderWin: WeakRef<Window> | undefined;
|
2022-10-01 03:58:07 +02:00
|
|
|
return {
|
2023-02-28 02:40:45 +01:00
|
|
|
wp: Vencord.Webpack,
|
2023-02-28 00:38:28 +01:00
|
|
|
wpc: Webpack.wreq.c,
|
|
|
|
wreq: Webpack.wreq,
|
|
|
|
wpsearch: search,
|
|
|
|
wpex: extract,
|
2022-11-12 15:09:02 +01:00
|
|
|
wpexs: (code: string) => Vencord.Webpack.extract(Vencord.Webpack.findModuleId(code)!),
|
2023-02-28 00:38:28 +01:00
|
|
|
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)),
|
2023-04-04 10:26:53 -03:00
|
|
|
findStore: newFindWrapper(filters.byStoreName),
|
2023-02-28 00:38:28 +01:00
|
|
|
PluginsApi: Vencord.Plugins,
|
|
|
|
plugins: Vencord.Plugins.plugins,
|
|
|
|
React,
|
2022-10-01 03:58:07 +02:00
|
|
|
Settings: Vencord.Settings,
|
|
|
|
Api: Vencord.Api,
|
|
|
|
reload: () => location.reload(),
|
2023-04-15 02:26:46 +02:00
|
|
|
restart: IS_WEB ? WEB_ONLY("restart") : relaunch,
|
2023-04-15 00:02:08 -03:00
|
|
|
canonicalizeMatch,
|
|
|
|
canonicalizeReplace,
|
|
|
|
canonicalizeReplacement,
|
2023-04-15 02:26:46 +02:00
|
|
|
fakeRender: (component: ComponentType, props: any) => {
|
|
|
|
const prevWin = fakeRenderWin?.deref();
|
|
|
|
const win = prevWin?.closed === false ? prevWin : window.open("about:blank", "Fake Render", "popup,width=500,height=500")!;
|
|
|
|
fakeRenderWin = new WeakRef(win);
|
|
|
|
win.focus();
|
|
|
|
|
|
|
|
ReactDOM.render(React.createElement(component, props), win.document.body);
|
|
|
|
}
|
2022-10-01 03:58:07 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
start() {
|
|
|
|
const shortcuts = this.getShortcuts();
|
|
|
|
window.shortcutList = shortcuts;
|
|
|
|
for (const [key, val] of Object.entries(shortcuts))
|
|
|
|
window[key] = val;
|
|
|
|
},
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
delete window.shortcutList;
|
|
|
|
for (const key in this.getShortcuts())
|
|
|
|
delete window[key];
|
|
|
|
}
|
|
|
|
});
|