Skip to content

Commit a0fc1a1

Browse files
CovkieVendicated
authored andcommitted
Support discord:// uri scheme (Vencord#813)
Co-authored-by: v <[email protected]>
1 parent d0f49cb commit a0fc1a1

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

package.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@
7171
"package.json",
7272
"LICENSE"
7373
],
74+
"protocols": {
75+
"name": "Discord",
76+
"schemes": [
77+
"discord"
78+
]
79+
},
7480
"beforePack": "scripts/build/sandboxFix.js",
7581
"linux": {
7682
"icon": "build/icon.icns",
@@ -111,7 +117,8 @@
111117
"GenericName": "Internet Messenger",
112118
"Type": "Application",
113119
"Categories": "Network;InstantMessaging;Chat;",
114-
"Keywords": "discord;vencord;nexulien;electron;chat;"
120+
"Keywords": "discord;vencord;nexulien;electron;chat;",
121+
"MimeType": "x-scheme-handler/discord"
115122
}
116123
},
117124
"mac": {

src/main/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ console.log("Vesktop v" + app.getVersion());
2929
process.env.VENCORD_USER_DATA_DIR = DATA_DIR;
3030

3131
function init() {
32+
app.setAsDefaultProtocolClient("discord");
33+
3234
const { disableSmoothScroll, hardwareAcceleration } = Settings.store;
3335

3436
const enabledFeatures = app.commandLine.getSwitchValue("enable-features").split(",");
@@ -119,6 +121,12 @@ async function bootstrap() {
119121
}
120122
}
121123

124+
// MacOS only event
125+
export let darwinURL: string | undefined;
126+
app.on("open-url", (_, url) => {
127+
darwinURL = url;
128+
});
129+
122130
app.on("window-all-closed", () => {
123131
if (process.platform !== "darwin") app.quit();
124132
});

src/main/mainWindow.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
MIN_WIDTH,
3737
VENCORD_FILES_DIR
3838
} from "./constants";
39+
import { darwinURL } from "./index";
3940
import { sendRendererCommand } from "./ipcCommands";
4041
import { Settings, State, VencordSettings } from "./settings";
4142
import { createSplashWindow } from "./splash";
@@ -454,18 +455,20 @@ function createMainWindow() {
454455

455456
win.webContents.setUserAgent(BrowserUserAgent);
456457

457-
const subdomain =
458-
Settings.store.discordBranch === "canary" || Settings.store.discordBranch === "ptb"
459-
? `${Settings.store.discordBranch}.`
460-
: "";
461-
462-
win.loadURL(`https://${subdomain}discord.com/app`);
458+
// 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.)
459+
loadUrl(darwinURL || process.argv.find(arg => arg.startsWith("discord://")));
463460

464461
return win;
465462
}
466463

467464
const runVencordMain = once(() => require(join(VENCORD_FILES_DIR, "vencordDesktopMain.js")));
468465

466+
export function loadUrl(uri: string | undefined) {
467+
const branch = Settings.store.discordBranch;
468+
const subdomain = branch === "canary" || branch === "ptb" ? `${branch}.` : "";
469+
mainWin.loadURL(`https://${subdomain}discord.com/${uri ? new URL(uri).pathname.slice(1) || "app" : "app"}`);
470+
}
471+
469472
export async function createWindows() {
470473
const startMinimized = process.argv.includes("--start-minimized");
471474
const splash = createSplashWindow(startMinimized);
@@ -498,5 +501,13 @@ export async function createWindows() {
498501
});
499502
});
500503

504+
mainWin.webContents.on("did-navigate", (_, url: string, responseCode: number) => {
505+
// check url to ensure app doesn't loop
506+
if (responseCode >= 300 && new URL(url).pathname !== `/app`) {
507+
loadUrl(undefined);
508+
console.warn(`'did-navigate': Caught bad page response: ${responseCode}, redirecting to main app`);
509+
}
510+
});
511+
501512
initArRPC();
502513
}

0 commit comments

Comments
 (0)