From 60d6c84542fadb6055b1d2d50a5eaf9ffa15cbbc Mon Sep 17 00:00:00 2001 From: Kylie C Date: Tue, 13 Aug 2024 01:45:31 -0400 Subject: [PATCH 1/7] Support discord uri scheme closes: #769 --- package.json | 9 ++++++++- src/main/index.ts | 2 ++ src/main/mainWindow.ts | 18 +++++++++++++----- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index ca5cf49..b850985 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,12 @@ "package.json", "LICENSE" ], + "protocols": { + "name": "Discord", + "schemes": [ + "discord" + ] + }, "beforePack": "scripts/build/sandboxFix.js", "linux": { "icon": "build/icon.icns", @@ -112,7 +118,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 2e0d6f7..4df2ad7 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -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(","); diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index d860b37..bf462e5 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -455,12 +455,20 @@ function createMainWindow() { win.webContents.setUserAgent(BrowserUserAgent); - const subdomain = - Settings.store.discordBranch === "canary" || Settings.store.discordBranch === "ptb" - ? `${Settings.store.discordBranch}.` - : ""; + const branch = Settings.store.discordBranch; + const subdomain = branch === "canary" || branch === "ptb" ? `${branch}.` : ""; + const uri = process.argv.find(arg => arg.startsWith("discord://")); - win.loadURL(`https://${subdomain}discord.com/app`); + const loadUrl = (url: string | undefined) => { + win.loadURL(`https://${subdomain}discord.com${url?.substring("discord://".length) || "/app"}`); + }; + + let uriFiredDarwin = false; + app.on("open-url", (_, url) => { + uriFiredDarwin = true; + loadUrl(url); + }); + uriFiredDarwin || loadUrl(uri); return win; } From 42f76b79aa0bb188ec4a9c051cdd0a8b202da1e3 Mon Sep 17 00:00:00 2001 From: Kylie C Date: Fri, 16 Aug 2024 11:08:27 -0400 Subject: [PATCH 2/7] update uri matching attempt to cover malformed or incomplete uris Use RegExp to avoid escape character hell --- src/main/mainWindow.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index bf462e5..4ff7e8c 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -460,7 +460,7 @@ function createMainWindow() { const uri = process.argv.find(arg => arg.startsWith("discord://")); const loadUrl = (url: string | undefined) => { - win.loadURL(`https://${subdomain}discord.com${url?.substring("discord://".length) || "/app"}`); + win.loadURL(`https://${subdomain}discord.com/${url?.replace(RegExp("^discord://[^/]*/?"), "") || "app"}`); }; let uriFiredDarwin = false; From b3e0d2e5b2f2ae94e4253cc1960d6820a9017e42 Mon Sep 17 00:00:00 2001 From: Kylie C Date: Wed, 11 Sep 2024 17:08:46 -0400 Subject: [PATCH 3/7] fix mac instance reloading also consolidated restore function --- src/main/index.ts | 9 ++------- src/main/mainWindow.ts | 11 +++++++++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index 4df2ad7..0666fc1 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -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"; @@ -69,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(); }); app.whenReady().then(async () => { diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index 4ff7e8c..59ea2c7 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -465,16 +465,23 @@ function createMainWindow() { let uriFiredDarwin = false; app.on("open-url", (_, url) => { + uriFiredDarwin ? restoreVesktop() : loadUrl(url); uriFiredDarwin = true; - loadUrl(url); }); uriFiredDarwin || loadUrl(uri); - return win; } const runVencordMain = once(() => require(join(VENCORD_FILES_DIR, "vencordDesktopMain.js"))); +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); From dbe109c44b9b0e4ab55756946543bf27495d1880 Mon Sep 17 00:00:00 2001 From: Kylie C Date: Wed, 11 Sep 2024 18:56:19 -0400 Subject: [PATCH 4/7] 404 page handler Discord handles incorrect schemes pretty well but `discord:///settings` is a case where it doesn't, so this may be needed to ensure user experience --- src/main/ipc.ts | 7 ++++++- src/main/mainWindow.ts | 17 +++++++++-------- src/preload/index.ts | 4 ++++ src/shared/IpcEvents.ts | 4 +++- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 4fa662c..a438206 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -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"; @@ -135,6 +135,11 @@ handle(IpcEvents.CLIPBOARD_COPY_IMAGE, async (_, buf: ArrayBuffer, src: string) }); }); +handle(IpcEvents.FOUROHFOUR_PAGE_HANDLER, _ => { + loadUrl(undefined); + console.warn("caught incomplete uri scheme. dumping to main app"); +}); + function readCss() { return readFile(VENCORD_QUICKCSS_FILE, "utf-8").catch(() => ""); } diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index 59ea2c7..626582f 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -455,25 +455,26 @@ function createMainWindow() { win.webContents.setUserAgent(BrowserUserAgent); - const branch = Settings.store.discordBranch; - const subdomain = branch === "canary" || branch === "ptb" ? `${branch}.` : ""; - const uri = process.argv.find(arg => arg.startsWith("discord://")); - - const loadUrl = (url: string | undefined) => { - win.loadURL(`https://${subdomain}discord.com/${url?.replace(RegExp("^discord://[^/]*/?"), "") || "app"}`); - }; - let uriFiredDarwin = false; app.on("open-url", (_, url) => { uriFiredDarwin ? restoreVesktop() : loadUrl(url); uriFiredDarwin = true; }); + + const uri = process.argv.find(arg => arg.startsWith("discord://")); uriFiredDarwin || loadUrl(uri); + 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"}`); +} + export function restoreVesktop() { if (mainWin) { if (mainWin.isMinimized()) mainWin.restore(); diff --git a/src/preload/index.ts b/src/preload/index.ts index 75bf9cd..5aa63ad 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -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 { diff --git a/src/shared/IpcEvents.ts b/src/shared/IpcEvents.ts index 51d2a28..0843a47 100644 --- a/src/shared/IpcEvents.ts +++ b/src/shared/IpcEvents.ts @@ -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" } From 5381340074f3f9db7bb393326a4e69cb6a9f3f94 Mon Sep 17 00:00:00 2001 From: Kylie C Date: Fri, 4 Oct 2024 16:47:33 -0400 Subject: [PATCH 5/7] "its more readable" statements by the deranged --- src/main/index.ts | 3 ++- src/main/mainWindow.ts | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index b697fe4..6cc60b6 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -72,7 +72,8 @@ function init() { if (isDeckGameMode) nativeTheme.themeSource = "dark"; app.on("second-instance", (_event, _cmdLine, _cwd, data: any) => { - data.IS_DEV ? app.quit() : restoreVesktop(); + if (data.IS_DEV) app.quit(); + else restoreVesktop(); }); app.whenReady().then(async () => { diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index 65eedd3..0a203f9 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -457,12 +457,13 @@ function createMainWindow() { let uriFiredDarwin = false; app.on("open-url", (_, url) => { - uriFiredDarwin ? restoreVesktop() : loadUrl(url); + if (uriFiredDarwin) restoreVesktop(); + else loadUrl(url); uriFiredDarwin = true; }); const uri = process.argv.find(arg => arg.startsWith("discord://")); - uriFiredDarwin || loadUrl(uri); + if (!uriFiredDarwin) loadUrl(uri); return win; } @@ -472,7 +473,7 @@ const runVencordMain = once(() => require(join(VENCORD_FILES_DIR, "vencordDeskto 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"}`); + mainWin.loadURL(`https://${subdomain}discord.com/${uri ? new URL(uri).pathname.slice(1) || "app" : "app"}`); } export function restoreVesktop() { From df6b85cc7a0f5c8196fded2b13f449ef4ed21625 Mon Sep 17 00:00:00 2001 From: Kylie C Date: Fri, 4 Oct 2024 16:52:22 -0400 Subject: [PATCH 6/7] rename ipc event --- src/main/ipc.ts | 2 +- src/preload/index.ts | 2 +- src/shared/IpcEvents.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index a438206..0d0a772 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -135,7 +135,7 @@ handle(IpcEvents.CLIPBOARD_COPY_IMAGE, async (_, buf: ArrayBuffer, src: string) }); }); -handle(IpcEvents.FOUROHFOUR_PAGE_HANDLER, _ => { +handle(IpcEvents.PAGE_NOT_FOUND_HANDLER, _ => { loadUrl(undefined); console.warn("caught incomplete uri scheme. dumping to main app"); }); diff --git a/src/preload/index.ts b/src/preload/index.ts index 5aa63ad..b43b70d 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -25,7 +25,7 @@ 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.title === "Page Not Found | Discord") ipcRenderer.invoke(IpcEvents.PAGE_NOT_FOUND_HANDLER); }); if (document.readyState === "complete") { diff --git a/src/shared/IpcEvents.ts b/src/shared/IpcEvents.ts index 0843a47..2fba633 100644 --- a/src/shared/IpcEvents.ts +++ b/src/shared/IpcEvents.ts @@ -52,5 +52,5 @@ export const enum IpcEvents { CLIPBOARD_COPY_IMAGE = "VCD_CLIPBOARD_COPY_IMAGE", - FOUROHFOUR_PAGE_HANDLER = "VCD_404_PAGE_HANDLER" + PAGE_NOT_FOUND_HANDLER = "VCD_PAGE_NOT_FOUND_HANDLER" } From 59e51d3f8a20602d88a0d75471957d517979ec24 Mon Sep 17 00:00:00 2001 From: Kylie C Date: Mon, 14 Oct 2024 17:30:32 -0400 Subject: [PATCH 7/7] not insane bad response handler --- src/main/ipc.ts | 7 +------ src/main/mainWindow.ts | 10 +++++++++- src/preload/index.ts | 4 ---- src/shared/IpcEvents.ts | 4 +--- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 0d0a772..4fa662c 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -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 { loadUrl, mainWin } from "./mainWindow"; +import { mainWin } from "./mainWindow"; import { Settings, State } from "./settings"; import { handle, handleSync } from "./utils/ipcWrappers"; import { PopoutWindows } from "./utils/popout"; @@ -135,11 +135,6 @@ handle(IpcEvents.CLIPBOARD_COPY_IMAGE, async (_, buf: ArrayBuffer, src: string) }); }); -handle(IpcEvents.PAGE_NOT_FOUND_HANDLER, _ => { - loadUrl(undefined); - console.warn("caught incomplete uri scheme. dumping to main app"); -}); - function readCss() { return readFile(VENCORD_QUICKCSS_FILE, "utf-8").catch(() => ""); } diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index 0a203f9..b0e4f02 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -470,7 +470,7 @@ function createMainWindow() { const runVencordMain = once(() => require(join(VENCORD_FILES_DIR, "vencordDesktopMain.js"))); -export function loadUrl(uri: string | undefined) { +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"}`); @@ -516,6 +516,14 @@ export async function createWindows() { }); }); + mainWin.webContents.on("did-navigate", (_, url: string, responseCode: number) => { + // 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`); + } + }); + // evil hack to fix electron 32 regression that makes devtools always light theme // https://github.com/electron/electron/issues/43367 // TODO: remove once fixed diff --git a/src/preload/index.ts b/src/preload/index.ts index b43b70d..75bf9cd 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -24,10 +24,6 @@ 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.PAGE_NOT_FOUND_HANDLER); -}); - if (document.readyState === "complete") { document.documentElement.appendChild(style); } else { diff --git a/src/shared/IpcEvents.ts b/src/shared/IpcEvents.ts index 2fba633..51d2a28 100644 --- a/src/shared/IpcEvents.ts +++ b/src/shared/IpcEvents.ts @@ -50,7 +50,5 @@ export const enum IpcEvents { ARRPC_ACTIVITY = "VCD_ARRPC_ACTIVITY", - CLIPBOARD_COPY_IMAGE = "VCD_CLIPBOARD_COPY_IMAGE", - - PAGE_NOT_FOUND_HANDLER = "VCD_PAGE_NOT_FOUND_HANDLER" + CLIPBOARD_COPY_IMAGE = "VCD_CLIPBOARD_COPY_IMAGE" }