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
11 changes: 4 additions & 7 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { autoUpdater } from "electron-updater";

import { DATA_DIR } from "./constants";
import { createFirstLaunchTour } from "./firstLaunch";
import { createWindows, mainWin } from "./mainWindow";
import { createWindows, restoreVesktop } from "./mainWindow";
import { registerMediaPermissionsHandler } from "./mediaPermissions";
import { registerScreenShareHandler } from "./screenShare";
import { Settings, State } from "./settings";
Expand All @@ -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 @@ -67,12 +69,7 @@ function init() {
if (isDeckGameMode) nativeTheme.themeSource = "dark";

app.on("second-instance", (_event, _cmdLine, _cwd, data: any) => {
if (data.IS_DEV) app.quit();
else if (mainWin) {
if (mainWin.isMinimized()) mainWin.restore();
if (!mainWin.isVisible()) mainWin.show();
mainWin.focus();
}
data.IS_DEV ? app.quit() : restoreVesktop();
Covkie marked this conversation as resolved.
Show resolved Hide resolved
});

app.whenReady().then(async () => {
Expand Down
7 changes: 6 additions & 1 deletion src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { IpcEvents } from "../shared/IpcEvents";
import { setBadgeCount } from "./appBadge";
import { autoStart } from "./autoStart";
import { VENCORD_FILES_DIR, VENCORD_QUICKCSS_FILE, VENCORD_THEMES_DIR } from "./constants";
import { mainWin } from "./mainWindow";
import { loadUrl, mainWin } from "./mainWindow";
import { Settings, State } from "./settings";
import { handle, handleSync } from "./utils/ipcWrappers";
import { PopoutWindows } from "./utils/popout";
Expand Down Expand Up @@ -135,6 +135,11 @@ handle(IpcEvents.CLIPBOARD_COPY_IMAGE, async (_, buf: ArrayBuffer, src: string)
});
});

handle(IpcEvents.FOUROHFOUR_PAGE_HANDLER, _ => {
Covkie marked this conversation as resolved.
Show resolved Hide resolved
loadUrl(undefined);
Covkie marked this conversation as resolved.
Show resolved Hide resolved
console.warn("caught incomplete uri scheme. dumping to main app");
});

function readCss() {
return readFile(VENCORD_QUICKCSS_FILE, "utf-8").catch(() => "");
}
Expand Down
26 changes: 21 additions & 5 deletions src/main/mainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,18 +455,34 @@ function createMainWindow() {

win.webContents.setUserAgent(BrowserUserAgent);

const subdomain =
Settings.store.discordBranch === "canary" || Settings.store.discordBranch === "ptb"
? `${Settings.store.discordBranch}.`
: "";
let uriFiredDarwin = false;
app.on("open-url", (_, url) => {
uriFiredDarwin ? restoreVesktop() : loadUrl(url);
Covkie marked this conversation as resolved.
Show resolved Hide resolved
uriFiredDarwin = true;
Covkie marked this conversation as resolved.
Show resolved Hide resolved
});

win.loadURL(`https://${subdomain}discord.com/app`);
const uri = process.argv.find(arg => arg.startsWith("discord://"));
uriFiredDarwin || loadUrl(uri);
Covkie marked this conversation as resolved.
Show resolved Hide resolved

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?.replace(RegExp("^discord://[^/]*/?"), "") || "app"}`);
Covkie marked this conversation as resolved.
Show resolved Hide resolved
}

export function restoreVesktop() {
if (mainWin) {
if (mainWin.isMinimized()) mainWin.restore();
if (!mainWin.isVisible()) mainWin.show();
mainWin.focus();
}
}

export async function createWindows() {
const startMinimized = process.argv.includes("--start-minimized");
const splash = createSplashWindow(startMinimized);
Expand Down
4 changes: 4 additions & 0 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const style = document.createElement("style");
style.id = "vcd-css-core";
style.textContent = readFileSync(rendererCss, "utf-8");

document.addEventListener("DOMContentLoaded", () => {
if (document.title === "Page Not Found | Discord") ipcRenderer.invoke(IpcEvents.FOUROHFOUR_PAGE_HANDLER);
});

if (document.readyState === "complete") {
document.documentElement.appendChild(style);
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/shared/IpcEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ export const enum IpcEvents {

ARRPC_ACTIVITY = "VCD_ARRPC_ACTIVITY",

CLIPBOARD_COPY_IMAGE = "VCD_CLIPBOARD_COPY_IMAGE"
CLIPBOARD_COPY_IMAGE = "VCD_CLIPBOARD_COPY_IMAGE",

FOUROHFOUR_PAGE_HANDLER = "VCD_404_PAGE_HANDLER"
}