2018-01-10 20:19:34 +01:00
|
|
|
/**
|
|
|
|
* BetterDiscord Core Entry
|
|
|
|
* Copyright (c) 2015-present JsSucks - https://github.com/JsSucks
|
|
|
|
* All rights reserved.
|
|
|
|
* https://github.com/JsSucks - https://betterdiscord.net
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
2018-02-03 00:42:12 +01:00
|
|
|
* LICENSE file in the root directory of this source tree.
|
2018-01-10 20:19:34 +01:00
|
|
|
*/
|
|
|
|
|
2018-05-28 02:52:12 +02:00
|
|
|
import path from 'path';
|
|
|
|
import sass from 'node-sass';
|
|
|
|
import { BrowserWindow, dialog } from 'electron';
|
2018-07-04 20:40:25 +02:00
|
|
|
import deepmerge from 'deepmerge';
|
2018-01-10 23:15:31 +01:00
|
|
|
|
2018-05-28 02:52:12 +02:00
|
|
|
import { FileUtils, BDIpc, Config, WindowUtils, CSSEditor, Database } from './modules';
|
2018-01-10 17:02:29 +01:00
|
|
|
|
2018-03-22 03:19:25 +01:00
|
|
|
const tests = typeof PRODUCTION === 'undefined';
|
2018-03-21 18:41:27 +01:00
|
|
|
|
|
|
|
const _basePath = tests ? path.resolve(__dirname, '..', '..') : __dirname;
|
|
|
|
const _baseDataPath = tests ? path.resolve(_basePath, 'tests') : _basePath;
|
|
|
|
|
|
|
|
const sparkplug = path.resolve(__dirname, 'sparkplug.js');
|
|
|
|
|
2018-03-19 18:15:31 +01:00
|
|
|
const _clientScript = tests
|
2018-03-21 18:41:27 +01:00
|
|
|
? path.resolve(_basePath, 'client', 'dist', 'betterdiscord.client.js')
|
|
|
|
: path.resolve(_basePath, 'betterdiscord.client.js');
|
2018-03-19 22:26:13 +01:00
|
|
|
const _cssEditorPath = tests
|
2018-03-20 00:02:40 +01:00
|
|
|
? path.resolve(__dirname, '..', '..', 'csseditor', 'dist')
|
2018-03-19 22:26:13 +01:00
|
|
|
: path.resolve(__dirname, 'csseditor');
|
2018-03-19 18:15:31 +01:00
|
|
|
|
2018-03-21 18:41:27 +01:00
|
|
|
const _dataPath = path.resolve(_baseDataPath, 'data');
|
|
|
|
const _extPath = path.resolve(_baseDataPath, 'ext');
|
|
|
|
const _pluginPath = path.resolve(_extPath, 'plugins');
|
|
|
|
const _themePath = path.resolve(_extPath, 'themes');
|
|
|
|
const _modulePath = path.resolve(_extPath, 'modules');
|
|
|
|
|
2018-03-21 21:47:46 +01:00
|
|
|
const version = require(path.resolve(_basePath, 'package.json')).version;
|
|
|
|
|
2018-03-19 18:15:31 +01:00
|
|
|
const paths = [
|
2018-03-21 18:41:27 +01:00
|
|
|
{ id: 'base', path: _basePath },
|
|
|
|
{ id: 'cs', path: _clientScript },
|
|
|
|
{ id: 'data', path: _dataPath },
|
|
|
|
{ id: 'ext', path: _extPath },
|
|
|
|
{ id: 'plugins', path: _pluginPath },
|
|
|
|
{ id: 'themes', path: _themePath },
|
|
|
|
{ id: 'modules', path: _modulePath },
|
|
|
|
{ id: 'csseditor', path: _cssEditorPath }
|
2018-03-19 18:15:31 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
const globals = {
|
2018-03-21 21:47:46 +01:00
|
|
|
version,
|
2018-03-19 18:15:31 +01:00
|
|
|
paths
|
2018-03-21 18:41:27 +01:00
|
|
|
};
|
2018-01-11 13:37:52 +01:00
|
|
|
|
2018-03-31 23:44:24 +02:00
|
|
|
class PatchedBrowserWindow extends BrowserWindow {
|
|
|
|
constructor(originalOptions) {
|
2018-07-04 20:40:25 +02:00
|
|
|
const userOptions = PatchedBrowserWindow.userWindowPreferences;
|
|
|
|
|
|
|
|
const options = deepmerge(originalOptions, userOptions);
|
2018-03-31 23:44:24 +02:00
|
|
|
options.webPreferences = Object.assign({}, options.webPreferences);
|
|
|
|
|
2018-03-31 23:49:00 +02:00
|
|
|
// Make sure Node integration is enabled
|
2018-05-28 02:42:59 +02:00
|
|
|
options.webPreferences.preload = sparkplug;
|
2018-03-31 23:44:24 +02:00
|
|
|
|
2018-05-28 02:42:59 +02:00
|
|
|
super(options);
|
|
|
|
|
2018-07-04 20:40:25 +02:00
|
|
|
this.__bd_preload = [originalOptions.webPreferences.preload];
|
|
|
|
|
|
|
|
if (userOptions.webPreferences && userOptions.webPreferences.preload) {
|
|
|
|
this.__bd_preload.push(path.resolve(_dataPath, userOptions.webPreferences.preload));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static get userWindowPreferences() {
|
|
|
|
try {
|
|
|
|
const userWindowPreferences = require(path.join(_dataPath, 'window'));
|
|
|
|
if (typeof userWindowPreferences === 'object') return userWindowPreferences;
|
|
|
|
} catch (err) {
|
|
|
|
console.log('[BetterDiscord] Error getting window preferences:', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2018-03-31 23:44:24 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-10 17:02:29 +01:00
|
|
|
|
2018-03-31 23:44:24 +02:00
|
|
|
class Comms {
|
2018-02-13 22:03:48 +01:00
|
|
|
constructor(bd) {
|
2018-02-15 18:09:06 +01:00
|
|
|
this.bd = bd;
|
2018-01-10 17:02:29 +01:00
|
|
|
this.initListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
initListeners() {
|
2018-03-21 21:52:42 +01:00
|
|
|
BDIpc.on('ping', () => 'pong', true);
|
2018-01-10 17:02:29 +01:00
|
|
|
|
2018-03-21 21:52:42 +01:00
|
|
|
BDIpc.on('bd-getConfig', () => this.bd.config.config, true);
|
2018-02-13 22:03:48 +01:00
|
|
|
|
2018-03-21 21:52:42 +01:00
|
|
|
BDIpc.on('bd-sendToDiscord', (event, m) => this.sendToDiscord(m.channel, m.message), true);
|
2018-01-17 12:28:52 +01:00
|
|
|
|
2018-03-21 21:52:42 +01:00
|
|
|
BDIpc.on('bd-openCssEditor', (event, options) => this.bd.csseditor.openEditor(options), true);
|
|
|
|
BDIpc.on('bd-sendToCssEditor', (event, m) => this.sendToCssEditor(m.channel, m.message), true);
|
|
|
|
|
|
|
|
BDIpc.on('bd-native-open', (event, options) => {
|
|
|
|
dialog.showOpenDialog(BrowserWindow.fromWebContents(event.ipcEvent.sender), options, filenames => {
|
|
|
|
event.reply(filenames);
|
2018-02-03 00:42:12 +01:00
|
|
|
});
|
|
|
|
});
|
2018-02-11 15:59:55 +01:00
|
|
|
|
2018-03-21 21:52:42 +01:00
|
|
|
BDIpc.on('bd-compileSass', (event, options) => {
|
|
|
|
if (typeof options.path === 'string' && typeof options.data === 'string') {
|
|
|
|
options.data = `${options.data} @import '${options.path.replace(/\\/g, '\\\\').replace(/'/g, '\\\'')}';`;
|
|
|
|
options.path = undefined;
|
2018-02-13 20:56:01 +01:00
|
|
|
}
|
2018-02-11 15:59:55 +01:00
|
|
|
|
2018-03-21 21:52:42 +01:00
|
|
|
sass.render(options, (err, result) => {
|
|
|
|
if (err) event.reject(err);
|
|
|
|
else event.reply(result);
|
2018-02-11 15:59:55 +01:00
|
|
|
});
|
|
|
|
});
|
2018-03-07 09:12:44 +01:00
|
|
|
|
2018-03-21 21:52:42 +01:00
|
|
|
BDIpc.on('bd-dba', (event, options) => this.bd.dbInstance.exec(options), true);
|
2018-01-10 17:02:29 +01:00
|
|
|
}
|
|
|
|
|
2018-01-14 07:00:21 +01:00
|
|
|
async send(channel, message) {
|
|
|
|
BDIpc.send(channel, message);
|
|
|
|
}
|
|
|
|
|
2018-03-21 21:52:42 +01:00
|
|
|
async sendToDiscord(channel, message) {
|
|
|
|
return this.bd.windowUtils.send(channel, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendToCssEditor(channel, message) {
|
|
|
|
return this.bd.csseditor.send(channel, message);
|
|
|
|
}
|
2018-01-10 17:02:29 +01:00
|
|
|
}
|
|
|
|
|
2018-05-28 02:52:12 +02:00
|
|
|
export class BetterDiscord {
|
2018-01-10 17:02:29 +01:00
|
|
|
|
|
|
|
constructor(args) {
|
2018-02-13 22:03:48 +01:00
|
|
|
if (BetterDiscord.loaded) {
|
|
|
|
console.log('Creating two BetterDiscord objects???');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
BetterDiscord.loaded = true;
|
|
|
|
|
2018-01-10 23:41:57 +01:00
|
|
|
this.injectScripts = this.injectScripts.bind(this);
|
2018-01-12 02:06:43 +01:00
|
|
|
this.ignite = this.ignite.bind(this);
|
2018-03-21 18:41:27 +01:00
|
|
|
|
|
|
|
this.config = new Config(args || globals);
|
2018-03-21 21:47:46 +01:00
|
|
|
this.dbInstance = new Database(this.config.getPath('data'));
|
2018-02-13 22:03:48 +01:00
|
|
|
this.comms = new Comms(this);
|
2018-03-21 18:41:27 +01:00
|
|
|
|
2018-01-10 23:15:31 +01:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2018-03-21 18:41:27 +01:00
|
|
|
await this.waitForWindowUtils();
|
2018-01-10 23:41:57 +01:00
|
|
|
|
2018-03-21 21:47:46 +01:00
|
|
|
if (!tests) {
|
|
|
|
const basePath = this.config.getPath('base');
|
|
|
|
const files = await FileUtils.listDirectory(basePath);
|
|
|
|
const latestCs = FileUtils.resolveLatest(files, file => file.endsWith('.js') && file.startsWith('client.'), file => file.replace('client.', '').replace('.js', ''), 'client.', '.js');
|
|
|
|
this.config.getPath('cs', true).path = path.resolve(basePath, latestCs);
|
|
|
|
}
|
|
|
|
|
|
|
|
await FileUtils.ensureDirectory(this.config.getPath('ext'));
|
2018-03-20 11:45:11 +01:00
|
|
|
|
2018-03-21 21:47:46 +01:00
|
|
|
this.csseditor = new CSSEditor(this, this.config.getPath('csseditor'));
|
2018-02-13 22:03:48 +01:00
|
|
|
|
2018-03-21 21:47:46 +01:00
|
|
|
this.windowUtils.on('did-finish-load', () => this.injectScripts(true));
|
2018-01-14 07:00:21 +01:00
|
|
|
|
2018-03-21 18:41:27 +01:00
|
|
|
this.windowUtils.on('did-navigate-in-page', (event, url, isMainFrame) => {
|
2018-01-14 07:00:21 +01:00
|
|
|
this.windowUtils.send('did-navigate-in-page', { event, url, isMainFrame });
|
|
|
|
});
|
|
|
|
|
2018-01-10 23:15:31 +01:00
|
|
|
setTimeout(() => {
|
2018-03-19 18:15:31 +01:00
|
|
|
this.injectScripts();
|
2018-01-10 23:15:31 +01:00
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
async waitForWindow() {
|
2018-01-12 02:06:43 +01:00
|
|
|
const self = this;
|
2018-03-19 18:15:31 +01:00
|
|
|
return new Promise(resolve => {
|
2018-01-10 23:15:31 +01:00
|
|
|
const defer = setInterval(() => {
|
|
|
|
const windows = BrowserWindow.getAllWindows();
|
2018-01-12 02:06:43 +01:00
|
|
|
|
2018-03-19 18:15:31 +01:00
|
|
|
if (windows.length === 1 && windows[0].webContents.getURL().includes('discordapp.com')) {
|
2018-01-10 23:15:31 +01:00
|
|
|
resolve(windows[0]);
|
|
|
|
clearInterval(defer);
|
|
|
|
}
|
2018-01-12 02:06:43 +01:00
|
|
|
}, 10);
|
2018-01-10 23:15:31 +01:00
|
|
|
});
|
2018-01-10 17:02:29 +01:00
|
|
|
}
|
|
|
|
|
2018-03-21 18:41:27 +01:00
|
|
|
async waitForWindowUtils() {
|
|
|
|
if (this.windowUtils) return this.windowUtils;
|
|
|
|
const window = await this.waitForWindow();
|
|
|
|
return this.windowUtils = new WindowUtils({ window });
|
|
|
|
}
|
|
|
|
|
|
|
|
get window() {
|
|
|
|
return this.windowUtils ? this.windowUtils.window : undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hooks things that Discord removes from global. These will be removed again in the client script.
|
|
|
|
*/
|
|
|
|
ignite() {
|
|
|
|
return BetterDiscord.ignite(this.window);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hooks things that Discord removes from global. These will be removed again in the client script.
|
|
|
|
* @param {BrowserWindow} window The window to inject the sparkplug script into
|
|
|
|
*/
|
|
|
|
static ignite(window) {
|
|
|
|
return WindowUtils.injectScript(window, sparkplug);
|
2018-01-12 02:06:43 +01:00
|
|
|
}
|
|
|
|
|
2018-03-21 18:41:27 +01:00
|
|
|
/**
|
|
|
|
* Injects the client script into the main window.
|
|
|
|
* @param {Boolean} reload Whether the main window was reloaded
|
|
|
|
*/
|
2018-03-20 11:45:11 +01:00
|
|
|
async injectScripts(reload = false) {
|
2018-01-12 02:06:43 +01:00
|
|
|
console.log(`RELOAD? ${reload}`);
|
2018-03-21 21:47:46 +01:00
|
|
|
return this.windowUtils.injectScript(this.config.getPath('cs'));
|
2018-01-10 23:41:57 +01:00
|
|
|
}
|
|
|
|
|
2018-03-31 23:49:00 +02:00
|
|
|
/**
|
|
|
|
* Patches Electron's BrowserWindow so all windows have Node integration enabled.
|
|
|
|
* This needs to be called only once before the main window is created (or BrowserWindow is put in a variable).
|
|
|
|
* Basically BetterDiscord needs to load before discord_desktop_core.
|
|
|
|
*/
|
2018-03-31 23:44:24 +02:00
|
|
|
static patchBrowserWindow() {
|
|
|
|
const electron_path = require.resolve('electron');
|
|
|
|
const browser_window_path = require.resolve(path.resolve(electron_path, '..', '..', 'browser-window.js'));
|
|
|
|
const browser_window_module = require.cache[browser_window_path];
|
|
|
|
|
|
|
|
browser_window_module.exports = PatchedBrowserWindow;
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:02:29 +01:00
|
|
|
}
|
|
|
|
|
2018-03-31 23:44:24 +02:00
|
|
|
BetterDiscord.patchBrowserWindow();
|