Skip to content
This repository has been archived by the owner on Jun 27, 2021. It is now read-only.

Commit

Permalink
Future Discord Update (based on Canary) Prep 1
Browse files Browse the repository at this point in the history
ED itself and most of the built in plugins are working again.
- ED Settings functional
- Injection not crashing
- Only hidden channels has obvious issue
- Devtools custom warning text still broken
- Stable support not harmed so far
- Due to Discord's electron security updates (which is why a breaking patch was needed in the first place), remote module is no longer relied on by ED, and plugins can no longer use it. (as of now)
  • Loading branch information
MasicoreLord authored and MasicoreLord committed Feb 6, 2021
1 parent e2ac95c commit 2f3ad75
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 39 deletions.
45 changes: 19 additions & 26 deletions dom_shit.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const path = window.require('path');
const fs = window.require('fs');
const electron = window.require('electron');
const Module = window.require('module').Module;
Module.globalPaths.push(path.resolve(electron.remote.app.getAppPath(), 'node_modules'));
const currentWindow = electron.remote.getCurrentWindow();
if (currentWindow.__preload) {
process.electronBinding('command_line').appendSwitch('preload', currentWindow.__preload);
electron.contextBridge.exposeInMainWorld = (key, val) => window[key] = val; // Expose DiscordNative
require(currentWindow.__preload);
const path = require('path');
const fs = require('fs');
const electron = require('electron');
const mainProcessInfo = JSON.parse(electron.ipcRenderer.sendSync('main-process-info'));
const Module = require('module');
Module.globalPaths.push(mainProcessInfo.originalNodeModulesPath);
if (mainProcessInfo.originalPreloadScript) {
process.electronBinding('command_line').appendSwitch('preload', mainProcessInfo.originalPreloadScript);
// This hack is no longer needed due to context isolation having to be on
//electron.contextBridge.exposeInMainWorld = (key, val) => window[key] = val; // Expose DiscordNative
require(mainProcessInfo.originalPreloadScript);
}

//Get inject directory
Expand Down Expand Up @@ -122,13 +123,16 @@ process.once('loaded', async () => {
ED.plugins = plugins;
c.log(`Plugins validated.`);

while (!window.webpackJsonp)
//while (!electron.webFrame.top.context.window.webpackJsonp)
//electron.webFrame.executeJavaScript(`
// preloadInfoShare.webpackJsonp = window.webpackJsonp;
//`);
await c.sleep(100); // wait until this is loaded in order to use it for modules

ED.webSocket = window._ws;
//ED.webSocket = window._ws;

/* Add helper functions that make plugins easy to create */
window.req = window.webpackJsonp.push([[], {
/* Add helper functions that make plugins easy to create */
window.req = electron.webFrame.top.context.window.webpackJsonp.push([[], {
'__extra_id__': (module, exports, req) => module.exports = req
}, [['__extra_id__']]]);
delete window.req.m['__extra_id__'];
Expand Down Expand Up @@ -210,17 +214,6 @@ process.once('loaded', async () => {
EDApi.monkeyPatch(ht, 'showToken', window.fixedShowToken);
if (!ED.localStorage.getItem('token') && ht.getToken())
window.fixedShowToken(); // prevent you from being logged out for no reason

// change the console warning to be more fun
const wc = require('electron').remote.getCurrentWebContents();
wc.removeAllListeners('devtools-opened');
wc.on('devtools-opened', () => {
console.log('%cHold Up!', 'color: #FF5200; -webkit-text-stroke: 2px black; font-size: 72px; font-weight: bold;');
console.log('%cIf you\'re reading this, you\'re probably smarter than most Discord developers.', 'font-size: 16px;');
console.log('%cPasting anything in here could actually improve the Discord client.', 'font-size: 18px; font-weight: bold; color: red;');
console.log('%cUnless you understand exactly what you\'re doing, keep this window open to browse our bad code.', 'font-size: 16px;');
console.log('%cIf you don\'t understand exactly what you\'re doing, you should come work with us: https://discordapp.com/jobs', 'font-size: 16px;');
});
});


Expand Down Expand Up @@ -577,4 +570,4 @@ window.BdApi.Themes = new class AddonAPI {
reload() {}
get() {return null;}
getAll() {return [];}
};
};
25 changes: 16 additions & 9 deletions injection.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('./main_process_shit');
const electron = require('electron');
const path = require('path');
electron.app.commandLine.appendSwitch("no-force-async-hooks-checks");
Expand All @@ -11,23 +12,29 @@ electron.session.defaultSession.webRequest.onHeadersReceived(function(details, c

class BrowserWindow extends electron.BrowserWindow {
constructor(originalOptions) {
if (!originalOptions || !originalOptions.webPreferences || !originalOptions.title) return super(originalOptions); // eslint-disable-line constructor-super
let win = new electron.BrowserWindow(originalOptions);
if (!originalOptions || !originalOptions.webPreferences || !originalOptions.title) return win; // eslint-disable-line constructor-super
const originalPreloadScript = originalOptions.webPreferences.preload;

// Make sure Node integration is enabled
originalOptions.webPreferences.nodeIntegration = true;
// Make sure remote module is enabled
originalOptions.webPreferences.enableRemoteModule = true;
// Make sure context isolation is disabled
originalOptions.webPreferences.contextIsolation = false;
originalOptions.webPreferences.preload = path.join(process.env.injDir, 'dom_shit.js');
originalOptions.webPreferences.transparency = true;

super(originalOptions);
this.__preload = originalPreloadScript;
// change the console warning to be more fun
win.webContents.on('devtools-opened', (event) => {
console.log('%cHold Up!', 'color: #FF5200; -webkit-text-stroke: 2px black; font-size: 72px; font-weight: bold;');
console.log('%cIf you\'re reading this, you\'re probably smarter than most Discord developers.', 'font-size: 16px;');
console.log('%cPasting anything in here could actually improve the Discord client.', 'font-size: 18px; font-weight: bold; color: red;');
console.log('%cUnless you understand exactly what you\'re doing, keep this window open to browse our bad code.', 'font-size: 16px;');
console.log('%cIf you don\'t understand exactly what you\'re doing, you should come work with us: https://discordapp.com/jobs', 'font-size: 16px;');
});
win = new electron.BrowserWindow(originalOptions);
win.webContents.__preload = originalPreloadScript;
return win;
}
}

BrowserWindow.webContents;

const electron_path = require.resolve('electron');
Object.assign(BrowserWindow, electron.BrowserWindow); // Assigns the new chrome-specific ones

Expand Down
20 changes: 20 additions & 0 deletions main_process_shit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const electron = require('electron');
const ipcMain = require('electron').ipcMain;
const path = require('path');

ipcMain.on('main-process-info', (event, arg) => {
event.returnValue = `{
"originalNodeModulesPath": "${path.resolve(electron.app.getAppPath(), 'node_modules')}",
"originalPreloadScript": "${event.sender.__preload}"
}`
});

ipcMain.on('current-web-contents', (event, arg) => {
event.returnValue = event.sender.__currentWebContents
});

ipcMain.on('main-process-utils', (event, arg) => {
event.returnValue = `{
"dialog": "${electron.dialog}"
}`
});
3 changes: 2 additions & 1 deletion plugins/direct_download.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const Plugin = require('../plugin');

// contains modified code from https://stackoverflow.com/a/47820271
const { dialog } = require('electron').remote;
const ipcRenderer = require('electron').ipcRenderer;
const { dialog } = JSON.parse(ipcRenderer.sendSync('main-process-utils'));
const http = require('https');
const fs = require('fs');
let ttM = {}, iteM = {};
Expand Down
2 changes: 1 addition & 1 deletion plugins/ed_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ module.exports = new Plugin({
const DiscordUIGenerator = {
reactMarkdownRules: (() => {
const simpleMarkdown = EDApi.findModule("markdownToReact");
const rules = window._.clone(simpleMarkdown.defaultRules);
const rules = require("electron").webFrame.top.context.window._.clone(simpleMarkdown.defaultRules);

rules.paragraph.react = (node, output, state) => {
return e(Fragment, null, output(node.content, state))
Expand Down
4 changes: 2 additions & 2 deletions plugins/hidden_channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = new Plugin({
ai = EDApi.findModule('actionIcon');

const getUser = EDApi.findModule('getCurrentUser').getCurrentUser;
const getAllChannels = EDApi.findModule('getGuildChannels').getGuildChannels;
const getAllChannels = EDApi.findModule('getChannels').getChannels;
const can = EDApi.findModule('computePermissions').can;

g_dc = EDApi.findModule('getDefaultChannel');
Expand Down Expand Up @@ -137,7 +137,7 @@ module.exports = new Plugin({
return egg;
});*/

const cancan = EDApi.findModuleByProps('can', 'canUser').can;
const cancan = EDApi.findModuleByProps('can').can;
gsr = EDApi.findModuleByDisplayName("FluxContainer(GuildSettingsRoles)").prototype;
EDApi.monkeyPatch(gsr, 'render', b => {
const egg = b.callOriginalMethod(b.methodArguments);
Expand Down

0 comments on commit 2f3ad75

Please sign in to comment.