diff --git a/common/constants/ipcevents.js b/common/constants/ipcevents.js index 17b98cad..730f98cd 100644 --- a/common/constants/ipcevents.js +++ b/common/constants/ipcevents.js @@ -11,4 +11,5 @@ export const CLOSE_DEVTOOLS = "bd-close-devtools"; export const OPEN_WINDOW = "bd-open-window"; export const INSPECT_ELEMENT = "bd-inspect-element"; export const MINIMUM_SIZE = "bd-minimum-size"; -export const DEVTOOLS_WARNING = "bd-remove-devtools-message"; \ No newline at end of file +export const DEVTOOLS_WARNING = "bd-remove-devtools-message"; +export const REGISTER_PRELOAD = "bd-register-preload"; \ No newline at end of file diff --git a/injector/src/index.js b/injector/src/index.js index 4e025614..7abb0df8 100644 --- a/injector/src/index.js +++ b/injector/src/index.js @@ -9,8 +9,6 @@ import CSP from "./modules/csp"; if (!process.argv.includes("--vanilla")) { process.env.NODE_OPTIONS = "--no-force-async-hooks-checks"; app.commandLine.appendSwitch("no-force-async-hooks-checks"); - process.electronBinding("command_line").appendSwitch("no-force-async-hooks-checks"); - // Patch and replace the built-in BrowserWindow BrowserWindow.patchBrowserWindow(); diff --git a/injector/src/modules/ipc.js b/injector/src/modules/ipc.js index 59c315d1..80074508 100644 --- a/injector/src/modules/ipc.js +++ b/injector/src/modules/ipc.js @@ -77,6 +77,10 @@ const setMinimumSize = (event, width, height) => { const stopDevtoolsWarning = event => event.sender.removeAllListeners("devtools-opened"); +const registerPreload = (event, path) => { + app.commandLine.appendSwitch("preload", path); +}; + export default class IPCMain { static registerEvents() { ipc.on(IPCEvents.GET_PATH, getPath); @@ -86,6 +90,7 @@ export default class IPCMain { ipc.on(IPCEvents.INSPECT_ELEMENT, inspectElement); ipc.on(IPCEvents.MINIMUM_SIZE, setMinimumSize); ipc.on(IPCEvents.DEVTOOLS_WARNING, stopDevtoolsWarning); + ipc.on(IPCEvents.REGISTER_PRELOAD, registerPreload); ipc.handle(IPCEvents.RUN_SCRIPT, runScript); ipc.handle(IPCEvents.OPEN_WINDOW, createBrowserWindow); } diff --git a/injector/src/preload.js b/injector/src/preload.js index 98b46a18..59a4b936 100644 --- a/injector/src/preload.js +++ b/injector/src/preload.js @@ -1,6 +1,17 @@ const Module = require("module"); const path = require("path"); const electron = require("electron"); +const NodeEvents = require("events"); + +const cloneObject = function (target, newObject = {}, keys) { + if (!Array.isArray(keys)) keys = Object.keys(Object.getOwnPropertyDescriptors(target)); + return keys.reduce((clone, key) => { + if (typeof(target[key]) === "object" && !Array.isArray(target[key]) && target[key] !== null && !(target[key] instanceof NodeEvents)) clone[key] = cloneObject(target[key], {}); + else clone[key] = target[key]; + + return clone; + }, newObject); +}; /* global window:false */ @@ -11,16 +22,26 @@ Object.defineProperty(window, "webpackJsonp", { electron.webFrame.top.context.global = electron.webFrame.top.context; electron.webFrame.top.context.require = require; -electron.webFrame.top.context.process = process; electron.webFrame.top.context.Buffer = Buffer; +electron.webFrame.top.context.process = new class PatchedProcess extends NodeEvents { + get __ORIGINAL_PROCESS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED__() {return process;} + + constructor() { + super(); + + Object.assign(this, + cloneObject(process, {}, Object.keys(NodeEvents.prototype)), + cloneObject(process, {}) + ); + } +}; // Load Discord's original preload const preload = process.env.DISCORD_PRELOAD; if (preload) { // Restore original preload for future windows - process.electronBinding("command_line").appendSwitch("preload", preload); - + electron.ipcRenderer.send("bd-register-preload", preload); // Run original preload try { const originalKill = process.kill; diff --git a/renderer/src/moduleloader.js b/renderer/src/moduleloader.js index 29258c22..fe929dfc 100644 --- a/renderer/src/moduleloader.js +++ b/renderer/src/moduleloader.js @@ -7,6 +7,10 @@ export default function() { const load = Module._load; Module._load = function(request) { + if (request === "process") { + return window.process; + } + if (request === namespace || request.startsWith(prefix)) { const requested = request.substr(prefix.length); if (requested == "bdapi") return BdApi;