diff --git a/package.json b/package.json index 2973f84..ad56aa4 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,12 @@ "package.json", "LICENSE" ], + "protocols": { + "name": "Discord", + "schemes": [ + "discord" + ] + }, "beforePack": "scripts/build/sandboxFix.js", "linux": { "icon": "build/icon.icns", @@ -113,7 +119,8 @@ "GenericName": "Internet Messenger", "Type": "Application", "Categories": "Network;InstantMessaging;Chat;", - "Keywords": "discord;vencord;electron;chat;" + "Keywords": "discord;vencord;electron;chat;", + "MimeType": "x-scheme-handler/discord" } }, "mac": { diff --git a/src/main/index.ts b/src/main/index.ts index 6f36a5d..9e9244f 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -29,6 +29,8 @@ console.log("Vesktop v" + app.getVersion()); process.env.VENCORD_USER_DATA_DIR = DATA_DIR; function init() { + app.setAsDefaultProtocolClient("discord"); + const { disableSmoothScroll, hardwareAcceleration } = Settings.store; 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", () => { if (process.platform !== "darwin") app.quit(); }); diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index d7c124d..46e0774 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -36,6 +36,7 @@ import { MIN_WIDTH, VENCORD_FILES_DIR } from "./constants"; +import { darwinURL } from "./index"; import { sendRendererCommand } from "./ipcCommands"; import { Settings, State, VencordSettings } from "./settings"; import { createSplashWindow } from "./splash"; @@ -454,18 +455,20 @@ function createMainWindow() { win.webContents.setUserAgent(BrowserUserAgent); - const subdomain = - Settings.store.discordBranch === "canary" || Settings.store.discordBranch === "ptb" - ? `${Settings.store.discordBranch}.` - : ""; - - win.loadURL(`https://${subdomain}discord.com/app`); + // 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.) + loadUrl(darwinURL || process.argv.find(arg => arg.startsWith("discord://"))); return win; } 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() { const startMinimized = process.argv.includes("--start-minimized"); 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(); }