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";
|
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";
|
2024-05-26 01:12:22 -03:00
|
|
|
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-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],
|
|
|
|
|
2024-05-26 01:11:36 -03:00
|
|
|
getShortcuts(): Record<PropertyKey, any> {
|
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);
|
|
|
|
|
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);
|
2023-11-28 22:12:00 -03:00
|
|
|
const findByProps = newFindWrapper(filters.byProps);
|
2024-05-26 01:11:36 -03:00
|
|
|
|
2022-10-01 03:58:07 +02: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,
|
2023-11-28 22:12:00 -03:00
|
|
|
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)),
|
2023-11-28 22:12:00 -03:00
|
|
|
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 },
|
2022-10-01 03:58:07 +02:00
|
|
|
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();
|
|
|
|
|
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
|
|
|
}
|
2022-10-01 03:58:07 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2024-05-26 01:11:36 -03:00
|
|
|
startAt: StartAt.Init,
|
2022-10-01 03:58:07 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2022-10-01 03:58:07 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
delete window.shortcutList;
|
2024-05-26 01:11:36 -03:00
|
|
|
for (const key in this.getShortcuts()) {
|
2022-10-01 03:58:07 +02:00
|
|
|
delete window[key];
|
2024-05-26 01:11:36 -03:00
|
|
|
}
|
2022-10-01 03:58:07 +02:00
|
|
|
}
|
|
|
|
});
|