Add Web/Desktop specific plugin capabilities; misc fixes

This commit is contained in:
Vendicated 2023-03-11 14:18:32 +01:00
parent 3b945b87b8
commit 5d1283bd85
No known key found for this signature in database
GPG key ID: A1DC0CFB5615D905
11 changed files with 89 additions and 85 deletions

View file

@ -33,6 +33,8 @@ export const banner = {
`.trim() `.trim()
}; };
const isWeb = process.argv.slice(0, 2).some(f => f.endsWith("buildWeb.mjs"));
// https://github.com/evanw/esbuild/issues/619#issuecomment-751995294 // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
/** /**
* @type {import("esbuild").Plugin} * @type {import("esbuild").Plugin}
@ -70,7 +72,13 @@ export const globPlugins = {
for (const file of files) { for (const file of files) {
if (file.startsWith(".")) continue; if (file.startsWith(".")) continue;
if (file === "index.ts") continue; if (file === "index.ts") continue;
if (!watch && (file.endsWith(".dev.ts") || file.endsWith(".dev.tsx"))) continue; const fileBits = file.split(".");
if (fileBits.length > 2 && ["ts", "tsx"].includes(fileBits.at(-1))) {
const mod = fileBits.at(-2);
if (mod === "dev" && !watch) continue;
if (mod === "web" && !isWeb) continue;
if (mod === "desktop" && isWeb) continue;
}
const mod = `p${i}`; const mod = `p${i}`;
code += `import ${mod} from "./${dir}/${file.replace(/\.tsx?$/, "")}";\n`; code += `import ${mod} from "./${dir}/${file.replace(/\.tsx?$/, "")}";\n`;

3
src/globals.d.ts vendored
View file

@ -51,8 +51,7 @@ declare global {
* Only available when running in Electron, undefined on web. * Only available when running in Electron, undefined on web.
* Thus, avoid using this or only use it inside an {@link IS_WEB} guard. * Thus, avoid using this or only use it inside an {@link IS_WEB} guard.
* *
* If you really must use it, mark your plugin as Desktop App only via * If you really must use it, mark your plugin as Desktop App only by naming it Foo.desktop.ts(x)
* `target: "DESKTOP"`
*/ */
export var DiscordNative: any; export var DiscordNative: any;

View file

@ -48,7 +48,6 @@ export default definePlugin({
name: "WebRichPresence (arRPC)", name: "WebRichPresence (arRPC)",
description: "Client plugin for arRPC to enable RPC on Discord Web (experimental)", description: "Client plugin for arRPC to enable RPC on Discord Web (experimental)",
authors: [Devs.Ducko], authors: [Devs.Ducko],
target: "WEB",
settingsAboutComponent: () => ( settingsAboutComponent: () => (
<> <>
@ -60,6 +59,9 @@ export default definePlugin({
), ),
async start() { async start() {
// ArmCord comes with its own arRPC implementation, so this plugin just confuses users
if ("armcord" in window) return;
if (ws) ws.close(); if (ws) ws.close();
ws = new WebSocket("ws://127.0.0.1:1337"); // try to open WebSocket ws = new WebSocket("ws://127.0.0.1:1337"); // try to open WebSocket

View file

@ -32,14 +32,14 @@ export default definePlugin({
authors: [Devs.Ven], authors: [Devs.Ven],
getShortcuts() { getShortcuts() {
function newFindWrapper(filterFactory: (props: any) => Webpack.FilterFn) { function newFindWrapper(filterFactory: (...props: any[]) => Webpack.FilterFn) {
const cache = new Map<string, any>(); const cache = new Map<string, unknown>();
return function (filterProps: any) { return function (...filterProps: unknown[]) {
const cacheKey = String(filterProps); const cacheKey = String(filterProps);
if (cache.has(cacheKey)) return cache.get(cacheKey); if (cache.has(cacheKey)) return cache.get(cacheKey);
const matches = findAll(filterFactory(filterProps)); const matches = findAll(filterFactory(...filterProps));
const result = (() => { const result = (() => {
switch (matches.length) { switch (matches.length) {

View file

@ -112,7 +112,7 @@ function initWs(isManual = false) {
}); });
ws.addEventListener("close", e => { ws.addEventListener("close", e => {
if (!wasConnected && !hasErrored) return; if (!wasConnected || hasErrored) return;
logger.info("Dev Companion Disconnected:", e.code, e.reason); logger.info("Dev Companion Disconnected:", e.code, e.reason);
@ -204,8 +204,9 @@ function initWs(isManual = false) {
return reply("Unknown Find Type " + type); return reply("Unknown Find Type " + type);
} }
if (results.length === 0) throw "No results"; const uniqueResultsCount = new Set(results).size;
if (results.length > 1) throw "Found more than one result! Make this filter more specific"; if (uniqueResultsCount === 0) throw "No results";
if (uniqueResultsCount > 1) throw "Found more than one result! Make this filter more specific";
} catch (err) { } catch (err) {
return reply("Failed to find: " + err); return reply("Failed to find: " + err);
} }

View file

@ -25,7 +25,6 @@ export default definePlugin({
name: "NoRPC", name: "NoRPC",
description: "Disables Discord's RPC server.", description: "Disables Discord's RPC server.",
authors: [Devs.Cyn], authors: [Devs.Cyn],
target: "DESKTOP",
patches: [ patches: [
{ {
find: '.ensureModule("discord_rpc")', find: '.ensureModule("discord_rpc")',

View file

@ -23,7 +23,6 @@ export default definePlugin({
name: "NoSystemBadge", name: "NoSystemBadge",
description: "Disables the taskbar and system tray unread count badge.", description: "Disables the taskbar and system tray unread count badge.",
authors: [Devs.rushii], authors: [Devs.rushii],
target: "DESKTOP",
patches: [ patches: [
{ {
find: "setSystemTrayApplications:function", find: "setSystemTrayApplications:function",

View file

@ -23,7 +23,7 @@ export default definePlugin({
name: "WebContextMenus", name: "WebContextMenus",
description: "Re-adds some of context menu items missing on the web version of Discord, namely Copy/Open Link", description: "Re-adds some of context menu items missing on the web version of Discord, namely Copy/Open Link",
authors: [Devs.Ven], authors: [Devs.Ven],
target: "WEB", enabledByDefault: true,
patches: [{ patches: [{
// There is literally no reason for Discord to make this Desktop only. // There is literally no reason for Discord to make this Desktop only.

View file

@ -79,10 +79,6 @@ export interface PluginDef {
* Whether this plugin should be enabled by default, but can be disabled * Whether this plugin should be enabled by default, but can be disabled
*/ */
enabledByDefault?: boolean; enabledByDefault?: boolean;
/**
* Set this if your plugin only works on Browser or Desktop, not both
*/
target?: "WEB" | "DESKTOP" | "BOTH";
/** /**
* Optionally provide settings that the user can configure in the Plugins tab of settings. * Optionally provide settings that the user can configure in the Plugins tab of settings.
* @deprecated Use `settings` instead * @deprecated Use `settings` instead