BetterDiscordApp-v2/client/src/index.js

142 lines
5.0 KiB
JavaScript
Raw Normal View History

2018-01-10 21:48:10 +01:00
/**
* BetterDiscord Client Core
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
2018-01-10 21:48:10 +01:00
* All rights reserved.
* https://betterdiscord.net
2018-01-10 21:48:10 +01:00
*
* 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 21:48:10 +01:00
*/
2018-08-22 09:53:20 +02:00
import { DOM, BdUI, BdMenu, Modals, Reflection, Toasts, Notifications, BdContextMenu, DiscordContextMenu } from 'ui';
2018-01-29 18:34:31 +01:00
import BdCss from './styles/index.scss';
2018-08-12 17:10:22 +02:00
import { Events, CssEditor, Globals, Settings, Database, Updater, ModuleManager, PluginManager, ThemeManager, ExtModuleManager, Vendor, WebpackModules, Patcher, MonkeyPatch, ReactComponents, ReactHelpers, ReactAutoPatcher, DiscordApi, BdWebApi, Connectivity, Cache } from 'modules';
import { ClientLogger as Logger, ClientIPC, Utils } from 'common';
import { BuiltinManager, EmoteModule, ReactDevtoolsModule, VueDevtoolsModule, TrackingProtection, E2EE } from 'builtin';
import electron from 'electron';
2018-03-21 16:57:10 +01:00
import path from 'path';
2018-08-20 17:30:41 +02:00
import { setTimeout } from 'timers';
2018-03-22 17:25:06 +01:00
const tests = typeof PRODUCTION === 'undefined';
2018-03-14 10:11:08 +01:00
const ignoreExternal = false;
2018-01-11 00:46:05 +01:00
2018-01-10 21:48:10 +01:00
class BetterDiscord {
2018-01-31 14:35:22 +01:00
2018-01-10 21:48:10 +01:00
constructor() {
2018-03-22 03:19:25 +01:00
Logger.file = tests ? path.resolve(__dirname, '..', '..', 'tests', 'log.txt') : path.join(__dirname, 'log.txt');
2018-06-27 00:25:43 +02:00
Logger.trimLogFile();
2018-03-21 16:57:10 +01:00
Logger.log('main', 'BetterDiscord starting');
this._bd = {
2018-08-22 09:53:20 +02:00
DOM, BdUI, BdMenu, Modals, Reflection, Toasts, Notifications, BdContextMenu, DiscordContextMenu,
Events, CssEditor, Globals, Settings, Database, Updater,
ModuleManager, PluginManager, ThemeManager, ExtModuleManager,
Vendor,
WebpackModules, Patcher, MonkeyPatch, ReactComponents, ReactHelpers, ReactAutoPatcher, DiscordApi,
EmoteModule,
2018-08-06 12:31:49 +02:00
BdWebApi,
Connectivity,
2018-08-12 17:10:22 +02:00
Cache,
Logger, ClientIPC, Utils,
plugins: PluginManager.localContent,
themes: ThemeManager.localContent,
extmodules: ExtModuleManager.localContent,
__filename, __dirname,
module: Globals.require.cache[__filename],
require: Globals.require,
webpack_require: __webpack_require__, // eslint-disable-line no-undef
get discord_require() { return WebpackModules.require }
};
const developermode = Settings.getSetting('core', 'advanced', 'developer-mode');
if (developermode.value) window._bd = this._bd;
developermode.on('setting-updated', event => {
if (event.value) window._bd = this._bd;
else if (window._bd) delete window._bd;
});
const debuggerkeybind = Settings.getSetting('core', 'advanced', 'debugger-keybind');
debuggerkeybind.on('keybind-activated', () => {
const currentWindow = electron.remote.getCurrentWindow();
if (currentWindow.isDevToolsOpened()) return eval('debugger;');
currentWindow.openDevTools();
setTimeout(() => eval('debugger;'), 1000);
});
2018-01-29 18:34:31 +01:00
DOM.injectStyle(BdCss, 'bdmain');
2018-03-12 16:48:29 +01:00
this.globalReady = this.globalReady.bind(this);
Events.on('global-ready', this.globalReady);
Globals.initg();
2018-01-29 18:56:48 +01:00
}
globalReady() {
BdUI.initUiEvents();
this.vueInstance = BdUI.injectUi();
this.init();
}
2018-01-30 16:59:27 +01:00
async init() {
2018-03-07 09:12:44 +01:00
try {
await Database.init();
await Settings.loadSettings();
await ModuleManager.initModules();
BuiltinManager.initAll();
if (tests) this.initTests();
if (!ignoreExternal) {
await ExtModuleManager.loadAllModules(true);
await PluginManager.loadAllPlugins(true);
await ThemeManager.loadAllThemes(true);
}
2018-03-07 09:12:44 +01:00
Events.emit('ready');
Events.emit('discord-ready');
2018-08-20 17:30:41 +02:00
if (!Settings.get('core', 'advanced', 'ignore-content-manager-errors'))
Modals.showContentManagerErrors();
} catch (err) {
Logger.err('main', ['FAILED TO LOAD!', err]);
}
}
2018-08-22 11:08:56 +02:00
initTests() {
function showDummyNotif() { // eslint-disable-line no-inner-declarations
Notifications.add('Dummy Notification', [
2018-08-22 11:08:56 +02:00
{
text: 'Show Again', onClick: function () {
setTimeout(showDummyNotif, 5000);
return true;
}
},
{
text: 'Ignore', onClick: function () {
return true;
}
2018-08-22 11:08:56 +02:00
}
]);
2018-03-07 09:12:44 +01:00
}
showDummyNotif();
2018-01-30 16:59:27 +01:00
DiscordContextMenu.add([
{
text: 'Hello',
onClick: () => { Toasts.info('Hello!'); }
}
]);
2018-01-25 12:03:12 +01:00
}
2018-01-10 21:48:10 +01:00
}
2018-01-11 00:07:35 +01:00
if (window.BetterDiscord) {
2018-01-11 07:02:11 +01:00
Logger.log('main', 'Attempting to inject again?');
2018-01-11 00:07:35 +01:00
} else {
let instance;
Events.on('autopatcher', () => instance = new BetterDiscord());
ReactAutoPatcher.autoPatch().then(() => Events.emit('autopatcher'));
}