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-01-11 13:40:31 +01:00
|
|
|
const path = require('path');
|
2018-02-11 15:59:55 +01:00
|
|
|
const sass = require('node-sass');
|
2018-01-10 23:15:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DEVELOPMENT VARIABLES
|
|
|
|
*/
|
2018-01-11 13:49:19 +01:00
|
|
|
const clientScriptPath = path.resolve(__dirname, '..', '..', 'client', 'dist').replace(/\\/g, '/');
|
|
|
|
|
2018-01-10 23:15:31 +01:00
|
|
|
const __DEV = {
|
2018-01-10 23:41:57 +01:00
|
|
|
TESTING: false,
|
2018-01-11 13:40:31 +01:00
|
|
|
clientScriptPath: `${clientScriptPath}/betterdiscord.client.js`
|
2018-01-10 23:15:31 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 16:19:11 +01:00
|
|
|
const __dataPath = path.resolve(__dirname, '..', '..', 'tests', 'data');
|
2018-01-11 13:37:52 +01:00
|
|
|
const __pluginPath = path.resolve(__dirname, '..', '..', 'tests', 'plugins');
|
|
|
|
const __themePath = path.resolve(__dirname, '..', '..', 'tests', 'themes');
|
2018-02-13 23:24:04 +01:00
|
|
|
const __modulePath = path.resolve(__dirname, '..', '..', 'tests', 'modules');
|
2018-01-11 13:37:52 +01:00
|
|
|
|
2018-01-17 12:28:52 +01:00
|
|
|
const { Utils, FileUtils, BDIpc, Config, WindowUtils, CSSEditor } = require('./modules');
|
2018-02-03 00:42:12 +01:00
|
|
|
const { BrowserWindow, dialog } = require('electron');
|
2018-01-10 17:02:29 +01:00
|
|
|
|
|
|
|
const Common = {};
|
|
|
|
|
|
|
|
const dummyArgs = {
|
2018-02-14 23:54:20 +01:00
|
|
|
'version': '2.0.0a',
|
2018-01-10 17:02:29 +01:00
|
|
|
'paths': [
|
2018-01-16 06:00:39 +01:00
|
|
|
{ 'id': 'base', 'path': 'basePath' },
|
2018-02-12 16:19:11 +01:00
|
|
|
{ 'id': 'data', 'path': __dataPath },
|
2018-01-16 06:00:39 +01:00
|
|
|
{ 'id': 'plugins', 'path': __pluginPath },
|
2018-02-13 23:24:04 +01:00
|
|
|
{ 'id': 'themes', 'path': __themePath },
|
|
|
|
{ 'id': 'modules', 'path': __modulePath }
|
2018-01-10 17:02:29 +01:00
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2018-01-11 13:37:52 +01:00
|
|
|
console.log(dummyArgs);
|
|
|
|
|
|
|
|
|
2018-01-10 17:02:29 +01: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() {
|
|
|
|
BDIpc.on('bd-getConfig', o => {
|
|
|
|
o.reply(Common.Config.config);
|
|
|
|
});
|
|
|
|
|
2018-02-13 22:03:48 +01:00
|
|
|
BDIpc.on('bd-sendToDiscord', event => this.bd.windowUtils.send(event.args.channel, event.args.message));
|
|
|
|
|
|
|
|
BDIpc.on('bd-openCssEditor', o => this.bd.csseditor.openEditor(o));
|
|
|
|
// BDIpc.on('bd-setScss', o => this.bd.csseditor.setSCSS(o.args.scss));
|
|
|
|
BDIpc.on('bd-sendToCssEditor', o => this.bd.csseditor.send(o.args.channel, o.args.data));
|
2018-01-17 12:28:52 +01:00
|
|
|
|
2018-01-10 17:02:29 +01:00
|
|
|
BDIpc.on('bd-readFile', this.readFile);
|
|
|
|
BDIpc.on('bd-readJson', o => this.readFile(o, true));
|
2018-02-03 00:42:12 +01:00
|
|
|
|
|
|
|
BDIpc.on('bd-native-open', o => {
|
|
|
|
dialog.showOpenDialog(BrowserWindow.fromWebContents(o.ipcEvent.sender), o.args, filenames => {
|
|
|
|
o.reply(filenames);
|
|
|
|
});
|
|
|
|
});
|
2018-02-11 15:59:55 +01:00
|
|
|
|
|
|
|
BDIpc.on('bd-compileSass', o => {
|
2018-02-15 20:27:44 +01:00
|
|
|
if (!o.args.path && !o.args.data) return o.reply('');
|
2018-02-15 20:14:08 +01:00
|
|
|
if (typeof o.args.path === 'string' && typeof o.args.data === 'string') {
|
2018-02-13 20:56:01 +01:00
|
|
|
o.args.data = `${o.args.data} @import '${o.args.path}';`;
|
|
|
|
o.args.path = undefined;
|
|
|
|
}
|
2018-02-11 15:59:55 +01:00
|
|
|
|
2018-02-13 20:56:01 +01:00
|
|
|
sass.render(o.args, (err, result) => {
|
2018-02-11 15:59:55 +01:00
|
|
|
if (err) {
|
|
|
|
o.reply({ err });
|
|
|
|
return;
|
|
|
|
}
|
2018-03-03 02:43:54 +01:00
|
|
|
o.reply(result);
|
2018-02-11 15:59:55 +01:00
|
|
|
});
|
|
|
|
});
|
2018-01-10 17:02:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async readFile(o, json) {
|
|
|
|
const { path } = o.args;
|
|
|
|
try {
|
|
|
|
const readFile = json ? await FileUtils.readJsonFromFile(path) : await FileUtils.readFile(path);
|
|
|
|
o.reply(readFile);
|
|
|
|
} catch (err) {
|
|
|
|
o.reply(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 07:00:21 +01:00
|
|
|
async send(channel, message) {
|
|
|
|
BDIpc.send(channel, message);
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:02:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class BetterDiscord {
|
|
|
|
|
|
|
|
constructor(args) {
|
2018-02-13 22:03:48 +01:00
|
|
|
if (BetterDiscord.loaded) {
|
|
|
|
// Creating two BetterDiscord objects???
|
|
|
|
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-01-10 17:02:29 +01:00
|
|
|
Common.Config = new Config(args || dummyArgs);
|
2018-02-13 22:03:48 +01:00
|
|
|
this.comms = new Comms(this);
|
2018-01-10 23:15:31 +01:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
const window = await this.waitForWindow();
|
|
|
|
this.windowUtils = new WindowUtils({ window });
|
2018-01-10 23:41:57 +01:00
|
|
|
|
2018-02-13 22:03:48 +01:00
|
|
|
this.csseditor = new CSSEditor(this);
|
|
|
|
|
2018-01-12 02:06:43 +01:00
|
|
|
//Log some events for now
|
2018-01-14 07:00:21 +01:00
|
|
|
//this.windowUtils.webContents.on('did-start-loading', e => this.windowUtils.executeJavascript(`console.info('did-start-loading');`));
|
|
|
|
//this.windowUtils.webContents.on('did-stop-loading', e => this.windowUtils.executeJavascript(`console.info('did-stop-loading');`));
|
|
|
|
//this.windowUtils.webContents.on('did-get-response-details', e => this.ignite(this.windowUtils.window));
|
|
|
|
//this.windowUtils.webContents.on('page-favicon-updated', e => this.windowUtils.executeJavascript(`console.info('page-favicon-updated');`));
|
|
|
|
//this.windowUtils.webContents.on('will-navigate', e => this.windowUtils.executeJavascript(`console.info('will-navigate');`));
|
|
|
|
//this.windowUtils.webContents.on('did-navigate', e => this.windowUtils.executeJavascript(`console.info('did-navigate');`));
|
|
|
|
//this.windowUtils.webContents.on('did-navigate-in-page', e => this.windowUtils.executeJavascript(`console.info('did-navigate-in-page');`));
|
|
|
|
//this.windowUtils.webContents.on('did-finish-load', e => this.injectScripts(true));
|
|
|
|
|
|
|
|
this.windowUtils.events('did-get-response-details', () => this.ignite(this.windowUtils.window));
|
|
|
|
this.windowUtils.events('did-finish-load', e => this.injectScripts(true));
|
|
|
|
|
|
|
|
this.windowUtils.events('did-navigate-in-page', (event, url, isMainFrame) => {
|
|
|
|
this.windowUtils.send('did-navigate-in-page', { event, url, isMainFrame });
|
|
|
|
});
|
|
|
|
|
2018-01-10 23:15:31 +01:00
|
|
|
setTimeout(() => {
|
2018-01-12 02:06:43 +01:00
|
|
|
if (__DEV) { 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-01-10 23:15:31 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const defer = setInterval(() => {
|
|
|
|
const windows = BrowserWindow.getAllWindows();
|
2018-01-12 02:06:43 +01:00
|
|
|
|
|
|
|
if (windows.length > 0) {
|
|
|
|
windows.forEach(window => {
|
|
|
|
self.ignite(window);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-10 23:15:31 +01:00
|
|
|
if (__DEV && __DEV.TESTING && windows.length > 0) {
|
|
|
|
resolve(windows[0]);
|
|
|
|
clearInterval(defer);
|
|
|
|
return;
|
2018-01-10 23:41:57 +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-01-12 02:06:43 +01:00
|
|
|
ignite(window) {
|
|
|
|
//Hook things that Discord removes from global. These will be removed again in the client script
|
|
|
|
const sp = path.resolve(__dirname, 'sparkplug.js').replace(/\\/g, '/');
|
|
|
|
window.webContents.executeJavaScript(`require("${sp}");`);
|
|
|
|
}
|
|
|
|
|
2018-01-11 13:37:52 +01:00
|
|
|
injectScripts(reload = false) {
|
2018-01-12 02:06:43 +01:00
|
|
|
console.log(`RELOAD? ${reload}`);
|
2018-01-10 23:41:57 +01:00
|
|
|
if (__DEV) {
|
|
|
|
this.windowUtils.injectScript(__DEV.clientScriptPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:02:29 +01:00
|
|
|
get fileUtils() { return FileUtils; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
BetterDiscord
|
2018-02-13 20:56:01 +01:00
|
|
|
};
|