Support discord:// uri scheme (#813)

Co-authored-by: v <vendicated@riseup.net>
This commit is contained in:
Cookie 2025-02-05 21:45:20 -05:00 committed by GitHub
parent 6ba562c663
commit 7560727372
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 7 deletions

View file

@ -73,6 +73,12 @@
"package.json", "package.json",
"LICENSE" "LICENSE"
], ],
"protocols": {
"name": "Discord",
"schemes": [
"discord"
]
},
"beforePack": "scripts/build/sandboxFix.js", "beforePack": "scripts/build/sandboxFix.js",
"linux": { "linux": {
"icon": "build/icon.icns", "icon": "build/icon.icns",
@ -113,7 +119,8 @@
"GenericName": "Internet Messenger", "GenericName": "Internet Messenger",
"Type": "Application", "Type": "Application",
"Categories": "Network;InstantMessaging;Chat;", "Categories": "Network;InstantMessaging;Chat;",
"Keywords": "discord;vencord;electron;chat;" "Keywords": "discord;vencord;electron;chat;",
"MimeType": "x-scheme-handler/discord"
} }
}, },
"mac": { "mac": {

View file

@ -29,6 +29,8 @@ console.log("Vesktop v" + app.getVersion());
process.env.VENCORD_USER_DATA_DIR = DATA_DIR; process.env.VENCORD_USER_DATA_DIR = DATA_DIR;
function init() { function init() {
app.setAsDefaultProtocolClient("discord");
const { disableSmoothScroll, hardwareAcceleration } = Settings.store; const { disableSmoothScroll, hardwareAcceleration } = Settings.store;
const enabledFeatures = app.commandLine.getSwitchValue("enable-features").split(","); const enabledFeatures = app.commandLine.getSwitchValue("enable-features").split(",");
@ -119,6 +121,12 @@ async function bootstrap() {
} }
} }
// MacOS only event
export let darwinURL: string | undefined;
app.on("open-url", (_, url) => {
darwinURL = url;
});
app.on("window-all-closed", () => { app.on("window-all-closed", () => {
if (process.platform !== "darwin") app.quit(); if (process.platform !== "darwin") app.quit();
}); });

View file

@ -36,6 +36,7 @@ import {
MIN_WIDTH, MIN_WIDTH,
VENCORD_FILES_DIR VENCORD_FILES_DIR
} from "./constants"; } from "./constants";
import { darwinURL } from "./index";
import { sendRendererCommand } from "./ipcCommands"; import { sendRendererCommand } from "./ipcCommands";
import { Settings, State, VencordSettings } from "./settings"; import { Settings, State, VencordSettings } from "./settings";
import { createSplashWindow } from "./splash"; import { createSplashWindow } from "./splash";
@ -454,18 +455,20 @@ function createMainWindow() {
win.webContents.setUserAgent(BrowserUserAgent); win.webContents.setUserAgent(BrowserUserAgent);
const subdomain = // if the open-url event is fired (in index.ts) while starting up, darwinURL will be set. If not fall back to checking the process args (which Windows and Linux use for URI calling.)
Settings.store.discordBranch === "canary" || Settings.store.discordBranch === "ptb" loadUrl(darwinURL || process.argv.find(arg => arg.startsWith("discord://")));
? `${Settings.store.discordBranch}.`
: "";
win.loadURL(`https://${subdomain}discord.com/app`);
return win; return win;
} }
const runVencordMain = once(() => require(join(VENCORD_FILES_DIR, "vencordDesktopMain.js"))); const runVencordMain = once(() => require(join(VENCORD_FILES_DIR, "vencordDesktopMain.js")));
export function loadUrl(uri: string | undefined) {
const branch = Settings.store.discordBranch;
const subdomain = branch === "canary" || branch === "ptb" ? `${branch}.` : "";
mainWin.loadURL(`https://${subdomain}discord.com/${uri ? new URL(uri).pathname.slice(1) || "app" : "app"}`);
}
export async function createWindows() { export async function createWindows() {
const startMinimized = process.argv.includes("--start-minimized"); const startMinimized = process.argv.includes("--start-minimized");
const splash = createSplashWindow(startMinimized); const splash = createSplashWindow(startMinimized);
@ -498,5 +501,13 @@ export async function createWindows() {
}); });
}); });
mainWin.webContents.on("did-navigate", (_, url: string, responseCode: number) => {
// check url to ensure app doesn't loop
if (responseCode >= 300 && new URL(url).pathname !== `/app`) {
loadUrl(undefined);
console.warn(`'did-navigate': Caught bad page response: ${responseCode}, redirecting to main app`);
}
});
initArRPC(); initArRPC();
} }