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";
|
2024-05-28 22:31:58 +02:00
|
|
|
import { getCurrentChannel, getCurrentGuild } from "@utils/discord";
|
|
|
|
import { SYM_LAZY_CACHED, SYM_LAZY_GET } from "@utils/lazy";
|
2024-07-20 19:17:05 -03:00
|
|
|
import { SYM_LAZY_COMPONENT_INNER } from "@utils/lazyReact";
|
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-28 17:41:34 -03:00
|
|
|
import { SYM_PROXY_INNER_GET, SYM_PROXY_INNER_VALUE } from "@utils/proxyInner";
|
2024-05-28 22:31:58 +02:00
|
|
|
import definePlugin, { PluginNative, StartAt } from "@utils/types";
|
2023-02-28 00:38:28 +01:00
|
|
|
import * as Webpack from "@webpack";
|
2024-06-21 04:30:12 -03:00
|
|
|
import { cacheFindAll, extract, filters, search } from "@webpack";
|
2024-05-26 01:11:36 -03:00
|
|
|
import * as Common from "@webpack/common";
|
2024-06-01 17:51:50 -03:00
|
|
|
import { loadLazyChunks } from "debug/loadLazyChunks";
|
2023-04-15 02:26:46 +02:00
|
|
|
import type { ComponentType } from "react";
|
2022-10-01 03:58:07 +02:00
|
|
|
|
2024-05-28 22:31:58 +02:00
|
|
|
const DESKTOP_ONLY = (f: string) => () => {
|
2022-10-05 14:25:37 +02:00
|
|
|
throw new Error(`'${f}' is Discord Desktop only.`);
|
|
|
|
};
|
|
|
|
|
2024-05-28 22:31:58 +02:00
|
|
|
const define: typeof Object.defineProperty =
|
|
|
|
(obj, prop, desc) => {
|
|
|
|
if (Object.hasOwn(desc, "value"))
|
|
|
|
desc.writable = true;
|
|
|
|
|
|
|
|
return Object.defineProperty(obj, prop, {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
...desc
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function makeShortcuts() {
|
|
|
|
function newFindWrapper(filterFactory: (...props: any[]) => Webpack.FilterFn) {
|
|
|
|
const cache = new Map<string, unknown>();
|
|
|
|
|
|
|
|
return function (...filterProps: unknown[]) {
|
|
|
|
const cacheKey = String(filterProps);
|
|
|
|
if (cache.has(cacheKey)) return cache.get(cacheKey);
|
|
|
|
|
2024-05-28 17:41:34 -03:00
|
|
|
const matches = cacheFindAll(filterFactory(...filterProps));
|
2024-05-28 22:31:58 +02: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;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let fakeRenderWin: WeakRef<Window> | undefined;
|
|
|
|
const find = newFindWrapper(f => f);
|
|
|
|
const findByProps = newFindWrapper(filters.byProps);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...Object.fromEntries(Object.keys(Common).map(key => [key, { getter: () => Common[key] }])),
|
|
|
|
wp: Webpack,
|
|
|
|
wpc: { getter: () => Webpack.cache },
|
|
|
|
wreq: { getter: () => Webpack.wreq },
|
|
|
|
wpsearch: search,
|
|
|
|
wpex: extract,
|
2024-06-21 04:30:12 -03:00
|
|
|
wpexs: (code: string) => extract(Webpack.cacheFindModuleId(code)!),
|
2024-06-01 17:51:50 -03:00
|
|
|
loadLazyChunks: IS_DEV ? loadLazyChunks : () => { throw new Error("loadLazyChunks is dev only."); },
|
2024-06-22 02:37:40 -03:00
|
|
|
filters,
|
2024-05-28 22:31:58 +02:00
|
|
|
find,
|
2024-05-28 17:41:34 -03:00
|
|
|
findAll: cacheFindAll,
|
2024-05-28 22:31:58 +02:00
|
|
|
findByProps,
|
2024-05-28 17:41:34 -03:00
|
|
|
findAllByProps: (...props: string[]) => cacheFindAll(filters.byProps(...props)),
|
2024-05-28 22:31:58 +02:00
|
|
|
findByCode: newFindWrapper(filters.byCode),
|
2024-05-28 17:41:34 -03:00
|
|
|
findAllByCode: (code: string) => cacheFindAll(filters.byCode(code)),
|
2024-06-21 02:23:04 -03:00
|
|
|
findComponentByCode: newFindWrapper(filters.byComponentCode),
|
|
|
|
findAllComponentsByCode: (...code: string[]) => cacheFindAll(filters.byComponentCode(...code)),
|
2024-05-28 22:31:58 +02:00
|
|
|
findExportedComponent: (...props: string[]) => findByProps(...props)[props[0]],
|
|
|
|
findStore: newFindWrapper(filters.byStoreName),
|
2024-06-21 18:21:42 -03:00
|
|
|
findByFactoryCode: newFindWrapper(filters.byFactoryCode),
|
|
|
|
findAllByFactoryCode: (...code: string[]) => cacheFindAll(filters.byFactoryCode(...code)),
|
2024-05-28 22:31:58 +02:00
|
|
|
PluginsApi: { getter: () => Vencord.Plugins },
|
|
|
|
plugins: { getter: () => Vencord.Plugins.plugins },
|
|
|
|
Settings: { getter: () => Vencord.Settings },
|
|
|
|
Api: { getter: () => Vencord.Api },
|
|
|
|
Util: { getter: () => Vencord.Util },
|
|
|
|
reload: () => location.reload(),
|
|
|
|
restart: IS_WEB ? DESKTOP_ONLY("restart") : relaunch,
|
|
|
|
canonicalizeMatch,
|
|
|
|
canonicalizeReplace,
|
|
|
|
canonicalizeReplacement,
|
|
|
|
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();
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Common.ReactDOM.render(Common.React.createElement(component, props), doc.body.appendChild(document.createElement("div")));
|
|
|
|
},
|
|
|
|
|
|
|
|
preEnable: (plugin: string) => (Vencord.Settings.plugins[plugin] ??= { enabled: true }).enabled = true,
|
|
|
|
|
|
|
|
channel: { getter: () => getCurrentChannel(), preload: false },
|
|
|
|
channelId: { getter: () => Common.SelectedChannelStore.getChannelId(), preload: false },
|
|
|
|
guild: { getter: () => getCurrentGuild(), preload: false },
|
|
|
|
guildId: { getter: () => Common.SelectedGuildStore.getGuildId(), preload: false },
|
|
|
|
me: { getter: () => Common.UserStore.getCurrentUser(), preload: false },
|
|
|
|
meId: { getter: () => Common.UserStore.getCurrentUser().id, preload: false },
|
2024-06-14 22:56:21 +02:00
|
|
|
messages: { getter: () => Common.MessageStore.getMessages(Common.SelectedChannelStore.getChannelId()), preload: false },
|
|
|
|
|
|
|
|
Stores: {
|
|
|
|
getter: () => Object.fromEntries(
|
|
|
|
Common.Flux.Store.getAll()
|
|
|
|
.map(store => [store.getName(), store] as const)
|
|
|
|
.filter(([name]) => name.length > 1)
|
|
|
|
)
|
|
|
|
}
|
2024-05-28 22:31:58 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadAndCacheShortcut(key: string, val: any, forceLoad: boolean) {
|
|
|
|
const currentVal = val.getter();
|
|
|
|
if (!currentVal || val.preload === false) return currentVal;
|
|
|
|
|
2024-07-17 15:58:10 -03:00
|
|
|
function unwrapProxy(value: any) {
|
|
|
|
if (value[SYM_LAZY_GET]) {
|
|
|
|
return forceLoad ? value[SYM_LAZY_GET]() : value[SYM_LAZY_CACHED];
|
|
|
|
} else if (value[SYM_PROXY_INNER_GET]) {
|
|
|
|
return forceLoad ? value[SYM_PROXY_INNER_GET]() : value[SYM_PROXY_INNER_VALUE];
|
2024-07-20 19:17:05 -03:00
|
|
|
} else if (value[SYM_LAZY_COMPONENT_INNER]) {
|
|
|
|
return value[SYM_LAZY_COMPONENT_INNER]() != null ? value[SYM_LAZY_COMPONENT_INNER]() : value;
|
2024-07-17 15:58:10 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
2024-05-28 17:41:34 -03:00
|
|
|
}
|
2024-05-28 22:31:58 +02:00
|
|
|
|
2024-07-17 15:58:10 -03:00
|
|
|
const value = unwrapProxy(currentVal);
|
2024-07-20 19:17:05 -03:00
|
|
|
if (value != null && typeof value === "object") {
|
2024-07-17 15:58:10 -03:00
|
|
|
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
|
|
|
|
|
|
for (const propKey in descriptors) {
|
|
|
|
const descriptor = descriptors[propKey];
|
|
|
|
|
|
|
|
if (descriptor.writable === true || descriptor.set != null) {
|
2024-07-20 19:17:05 -03:00
|
|
|
const newValue = unwrapProxy(value[propKey]);
|
|
|
|
if (newValue != null) {
|
|
|
|
value[propKey] = newValue;
|
|
|
|
}
|
2024-07-17 15:58:10 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-20 19:17:05 -03:00
|
|
|
if (value != null) {
|
2024-07-17 15:58:10 -03:00
|
|
|
define(window.shortcutList, key, { value });
|
|
|
|
}
|
2024-05-28 22:31:58 +02:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
startAt: StartAt.Init,
|
2022-10-01 03:58:07 +02:00
|
|
|
start() {
|
2024-05-28 22:31:58 +02:00
|
|
|
const shortcuts = makeShortcuts();
|
2024-05-26 01:11:36 -03:00
|
|
|
window.shortcutList = {};
|
|
|
|
|
|
|
|
for (const [key, val] of Object.entries(shortcuts)) {
|
2024-05-28 22:31:58 +02:00
|
|
|
if ("getter" in val) {
|
|
|
|
define(window.shortcutList, key, {
|
|
|
|
get: () => loadAndCacheShortcut(key, val, true)
|
2024-05-26 01:11:36 -03:00
|
|
|
});
|
|
|
|
|
2024-05-28 22:31:58 +02:00
|
|
|
define(window, key, {
|
|
|
|
get: () => window.shortcutList[key]
|
2024-05-26 01:11:36 -03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
window.shortcutList[key] = val;
|
|
|
|
window[key] = val;
|
|
|
|
}
|
|
|
|
}
|
2024-05-28 22:31:58 +02:00
|
|
|
|
|
|
|
// unproxy loaded modules
|
2024-05-28 18:02:57 -03:00
|
|
|
Webpack.onceDiscordLoaded.then(() => {
|
2024-05-28 22:31:58 +02:00
|
|
|
setTimeout(() => this.eagerLoad(false), 1000);
|
|
|
|
|
|
|
|
if (!IS_WEB) {
|
|
|
|
const Native = VencordNative.pluginHelpers.ConsoleShortcuts as PluginNative<typeof import("./native")>;
|
|
|
|
Native.initDevtoolsOpenEagerLoad();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
async eagerLoad(forceLoad: boolean) {
|
2024-05-28 18:02:57 -03:00
|
|
|
await Webpack.onceDiscordLoaded;
|
2024-05-28 22:31:58 +02:00
|
|
|
|
|
|
|
const shortcuts = makeShortcuts();
|
|
|
|
|
|
|
|
for (const [key, val] of Object.entries(shortcuts)) {
|
|
|
|
if (!Object.hasOwn(val, "getter") || (val as any).preload === false) continue;
|
|
|
|
|
|
|
|
try {
|
|
|
|
loadAndCacheShortcut(key, val, forceLoad);
|
|
|
|
} catch { } // swallow not found errors in DEV
|
|
|
|
}
|
2022-10-01 03:58:07 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
delete window.shortcutList;
|
2024-05-28 22:31:58 +02:00
|
|
|
for (const key in makeShortcuts()) {
|
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
|
|
|
}
|
|
|
|
});
|