Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support discord uri scheme #813

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
"package.json",
"LICENSE"
],
"protocols": {
"name": "Discord",
"schemes": [
"discord"
]
},
"beforePack": "scripts/build/sandboxFix.js",
"linux": {
"icon": "build/icon.icns",
Expand Down Expand Up @@ -111,7 +117,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": {
Expand Down
8 changes: 8 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ if (IS_DEV) {
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(",");
Expand Down Expand Up @@ -112,6 +114,12 @@ async function bootstrap() {
}
}

// MacOS only event
export var darwinURL: string | undefined;
app.on("open-url", (_, url) => {
darwinURL = url;
});

app.on("window-all-closed", () => {
if (process.platform !== "darwin") app.quit();
});
23 changes: 17 additions & 6 deletions src/main/mainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
MIN_WIDTH,
VENCORD_FILES_DIR
} from "./constants";
import { darwinURL } from "./index";
import { Settings, State, VencordSettings } from "./settings";
import { createSplashWindow } from "./splash";
import { makeLinksOpenExternally } from "./utils/makeLinksOpenExternally";
Expand Down Expand Up @@ -455,18 +456,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);
Expand Down Expand Up @@ -499,5 +502,13 @@ export async function createWindows() {
});
});

mainWin.webContents.on("did-navigate", (_, url: string, responseCode: number) => {
Covkie marked this conversation as resolved.
Show resolved Hide resolved
// check url to ensure app doesn't loop
if (new URL(url).pathname !== `/app` && responseCode >= 300) {
loadUrl(undefined);
console.warn(`Caught bad page response: ${responseCode}, dumping to main app`);
}
});

initArRPC();
}
Loading