BetterDiscordApp-v2/client/src/index.js

143 lines
5.1 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-24 12:50:59 +02:00
import { DOM, BdUI, BdMenu, Modals, Toasts, Notifications, BdContextMenu, DiscordContextMenu } from 'ui';
2018-01-29 18:34:31 +01:00
import BdCss from './styles/index.scss';
import { Events, Globals, Settings, Database, Updater, ModuleManager, PluginManager, ThemeManager, ExtModuleManager, Vendor, Patcher, MonkeyPatch, ReactComponents, ReactHelpers, ReactAutoPatcher, DiscordApi, BdWebApi, Connectivity, Cache, Reflection, PackageInstaller } from 'modules';
2019-03-05 19:38:21 +01:00
import { ClientLogger as Logger, ClientIPC, Utils, Axi } 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';
2019-02-24 20:24:37 +01:00
const ignoreExternal = true;
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, Globals, Settings, Database, Updater,
2018-12-06 09:02:07 +01:00
ModuleManager, PluginManager, ThemeManager, ExtModuleManager, PackageInstaller,
Vendor,
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,
2019-03-05 19:38:21 +01:00
Logger, ClientIPC, Utils, Axi,
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 Reflection.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() {
2018-08-23 04:16:18 +02:00
let notifications = 0;
function showDummyNotif() { // eslint-disable-line no-inner-declarations
2018-08-23 04:16:18 +02:00
Notifications.add(notifications++ ? `Notification ${notifications}` : undefined, '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'));
}