BetterDiscordApp-v2/core/src/main.js

101 lines
2.5 KiB
JavaScript
Raw Normal View History

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
* LICENSE file in the root directory of this source tree.
*/
2018-01-10 23:15:31 +01:00
/**
* DEVELOPMENT VARIABLES
*/
const __DEV = {
TESTING: true,
clietScriptPath: "G:/Github/JsSucks/BetterDiscordApp/client/dist/betterdiscord.client.js"
}
const { Utils, FileUtils, BDIpc, Config, WindowUtils } = require('./modules');
const { BrowserWindow } = require('electron');
2018-01-10 17:02:29 +01:00
const Common = {};
const dummyArgs = {
'version': '0.3.1',
'paths': [
{ 'base': 'basePath' },
{ 'plugins': 'pluginsPath' },
{ 'themes': 'themesPath' }
]
};
class Comms {
constructor() {
this.initListeners();
}
initListeners() {
BDIpc.on('bd-getConfig', o => {
o.reply(Common.Config.config);
});
BDIpc.on('bd-readFile', this.readFile);
BDIpc.on('bd-readJson', o => this.readFile(o, true));
}
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);
}
}
}
class BetterDiscord {
constructor(args) {
Common.Config = new Config(args || dummyArgs);
this.comms = new Comms();
2018-01-10 23:15:31 +01:00
this.init();
}
async init() {
const window = await this.waitForWindow();
this.windowUtils = new WindowUtils({ window });
setTimeout(() => {
if (__DEV) {
this.windowUtils.injectScript(__DEV.clietScriptPath);
}
}, 500);
}
async waitForWindow() {
return new Promise((resolve, reject) => {
const defer = setInterval(() => {
const windows = BrowserWindow.getAllWindows();
if (__DEV && __DEV.TESTING && windows.length > 0) {
resolve(windows[0]);
clearInterval(defer);
return;
}else if (false) { //TODO Check for Discord loading finished
resolve(windows[0]);
clearInterval(defer);
}
}, 100);
});
2018-01-10 17:02:29 +01:00
}
get fileUtils() { return FileUtils; }
}
module.exports = {
BetterDiscord
}