Vencord/src/plugins/consoleShortcuts/index.ts

156 lines
6.3 KiB
TypeScript
Raw Normal View History

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/>.
*/
import { Devs } from "@utils/constants";
import { relaunch } from "@utils/native";
import { canonicalizeMatch, canonicalizeReplace, canonicalizeReplacement } from "@utils/patches";
2024-05-26 01:11:36 -03:00
import definePlugin, { StartAt } from "@utils/types";
2023-02-28 00:38:28 +01:00
import * as Webpack from "@webpack";
import { cacheFindAll, extract, filters, findModuleId, search } from "@webpack";
2024-05-26 01:11:36 -03:00
import * as Common from "@webpack/common";
2023-04-15 02:26:46 +02:00
import type { ComponentType } from "react";
2022-10-05 14:25:37 +02:00
const WEB_ONLY = (f: string) => () => {
throw new Error(`'${f}' is Discord Desktop only.`);
};
export default definePlugin({
name: "ConsoleShortcuts",
description: "Adds shorter Aliases for many things on the window. Run `shortcutList` for a list.",
authors: [Devs.Ven],
2024-05-26 01:11:36 -03:00
getShortcuts(): Record<PropertyKey, any> {
function newFindWrapper(filterFactory: (...props: any[]) => Webpack.FilterFn) {
const cache = new Map<string, unknown>();
2023-02-28 00:38:28 +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);
2024-05-02 23:18:12 -03:00
const matches = cacheFindAll(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;
2023-11-23 02:44:04 +01:00
const find = newFindWrapper(f => f);
const findByProps = newFindWrapper(filters.byProps);
2024-05-26 01:11:36 -03:00
return {
2024-05-26 01:11:36 -03:00
...Object.fromEntries(Object.keys(Common).map(key => [key, { getter: () => Common[key] }])),
wp: Webpack,
wpc: { getter: () => Webpack.cache },
wreq: { getter: () => Webpack.wreq },
2023-02-28 00:38:28 +01:00
wpsearch: search,
wpex: extract,
2024-05-26 01:11:36 -03:00
wpexs: (code: string) => extract(findModuleId(code)!),
2023-11-23 02:44:04 +01:00
find,
2024-05-02 23:18:12 -03:00
findAll: cacheFindAll,
findByProps,
2024-05-02 23:18:12 -03:00
findAllByProps: (...props: string[]) => cacheFindAll(filters.byProps(...props)),
2023-02-28 00:38:28 +01:00
findByCode: newFindWrapper(filters.byCode),
2024-05-02 23:18:12 -03:00
findAllByCode: (code: string) => cacheFindAll(filters.byCode(code)),
2023-11-23 02:44:04 +01:00
findComponentByCode: newFindWrapper(filters.componentByCode),
2024-05-02 23:18:12 -03:00
findAllComponentsByCode: (...code: string[]) => cacheFindAll(filters.componentByCode(...code)),
findExportedComponent: (...props: string[]) => findByProps(...props)[props[0]],
2023-04-04 10:26:53 -03:00
findStore: newFindWrapper(filters.byStoreName),
2024-05-26 01:11:36 -03:00
PluginsApi: { getter: () => Vencord.Plugins },
plugins: { getter: () => Vencord.Plugins.plugins },
Settings: { getter: () => Vencord.Settings },
Api: { getter: () => Vencord.Api },
reload: () => location.reload(),
2023-04-15 02:26:46 +02:00
restart: IS_WEB ? WEB_ONLY("restart") : relaunch,
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();
2023-11-23 02:44:04 +01:00
const doc = win.document;
doc.body.style.margin = "1em";
if (!win.prepared) {
win.prepared = true;
[...document.querySelectorAll("style"), ...document.querySelectorAll("link[rel=stylesheet]")].forEach(s => {
const n = s.cloneNode(true) as HTMLStyleElement | HTMLLinkElement;
if (s.parentElement?.tagName === "HEAD")
doc.head.append(n);
else if (n.id?.startsWith("vencord-") || n.id?.startsWith("vcd-"))
doc.documentElement.append(n);
else
doc.body.append(n);
});
}
2024-05-26 01:11:36 -03:00
Common.ReactDOM.render(Common.React.createElement(component, props), doc.body.appendChild(document.createElement("div")));
2023-04-15 02:26:46 +02:00
}
};
},
2024-05-26 01:11:36 -03:00
startAt: StartAt.Init,
start() {
const shortcuts = this.getShortcuts();
2024-05-26 01:11:36 -03:00
window.shortcutList = {};
for (const [key, val] of Object.entries(shortcuts)) {
if (val.getter != null) {
Object.defineProperty(window.shortcutList, key, {
get: val.getter,
configurable: true,
enumerable: true
});
Object.defineProperty(window, key, {
get: () => window.shortcutList[key],
configurable: true,
enumerable: true
});
} else {
window.shortcutList[key] = val;
window[key] = val;
}
}
},
stop() {
delete window.shortcutList;
2024-05-26 01:11:36 -03:00
for (const key in this.getShortcuts()) {
delete window[key];
2024-05-26 01:11:36 -03:00
}
}
});