From cac77dce406be92755519be595cf460e8f758b16 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Sat, 1 Oct 2022 17:05:18 +0200 Subject: [PATCH] Fix DiscordNative.desktopCapture.getDesktopCaptureSources() in electron17+ --- src/ipcMain.ts | 3 ++- src/preload.ts | 19 ++++++++++++++++++- src/utils/IpcEvents.ts | 3 ++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/ipcMain.ts b/src/ipcMain.ts index 9795ff17..89c49ab7 100644 --- a/src/ipcMain.ts +++ b/src/ipcMain.ts @@ -2,7 +2,7 @@ import { execFile as cpExecFile } from 'child_process'; import { createHash } from "crypto"; -import { app, BrowserWindow, ipcMain, shell } from "electron"; +import { app, BrowserWindow, desktopCapturer, ipcMain, shell } from "electron"; import { createReadStream, mkdirSync, readFileSync, watch } from "fs"; import { open, readFile, writeFile } from "fs/promises"; import { join } from 'path'; @@ -76,6 +76,7 @@ function serializeErrors(func: (...args: any[]) => any) { }; } +ipcMain.handle(IpcEvents.GET_DESKTOP_CAPTURE_SOURCES, (_, opts) => desktopCapturer.getSources(opts)); ipcMain.handle(IpcEvents.GET_SETTINGS_DIR, () => SETTINGS_DIR); ipcMain.handle(IpcEvents.GET_QUICK_CSS, () => readCss()); ipcMain.handle(IpcEvents.OPEN_PATH, (_, ...pathElements) => shell.openPath(join(...pathElements))); diff --git a/src/preload.ts b/src/preload.ts index 73d2eb54..c716d2cb 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -1,7 +1,24 @@ -import { contextBridge, webFrame } from "electron"; +import electron, { contextBridge, webFrame } from "electron"; import { readFileSync } from "fs"; import { join } from "path"; import VencordNative from "./VencordNative"; +import { ipcRenderer } from 'electron'; +import IpcEvents from "./utils/IpcEvents"; + +if (electron.desktopCapturer === void 0) { + // Fix for desktopCapturer being main only in Electron 17+ + // Discord accesses this in discord_desktop_core (DiscordNative.desktopCapture.getDesktopCaptureSources) + // and errors with cannot "read property getSources() of undefined" + // see discord_desktop_core/app/discord_native/renderer/desktopCapture.js + const electronPath = require.resolve("electron"); + delete require.cache[electronPath]!.exports; + require.cache[electronPath]!.exports = { + ...electron, + desktopCapturer: { + getSources: (opts) => ipcRenderer.invoke(IpcEvents.GET_DESKTOP_CAPTURE_SOURCES, opts) + } + }; +} contextBridge.exposeInMainWorld("VencordNative", VencordNative); diff --git a/src/utils/IpcEvents.ts b/src/utils/IpcEvents.ts index 24e52b99..3ceaa9f1 100644 --- a/src/utils/IpcEvents.ts +++ b/src/utils/IpcEvents.ts @@ -23,5 +23,6 @@ export default strEnum({ GET_REPO: "VencordGetRepo", GET_HASHES: "VencordGetHashes", UPDATE: "VencordUpdate", - BUILD: "VencordBuild" + BUILD: "VencordBuild", + GET_DESKTOP_CAPTURE_SOURCES: "VencordGetDesktopCaptureSources" } as const);