BetterDiscordApp-v2/client/src/index.js

118 lines
4.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-03-12 16:48:29 +01:00
import { DOM, BdUI, Modals, Reflection } from 'ui';
2018-01-29 18:34:31 +01:00
import BdCss from './styles/index.scss';
2018-03-12 16:48:29 +01:00
import { Patcher, Events, CssEditor, Globals, ExtModuleManager, PluginManager, ThemeManager, ModuleManager, WebpackModules, Settings, Database, ReactComponents, DiscordApi } from 'modules';
import { ClientLogger as Logger, ClientIPC, Utils } from 'common';
import { EmoteModule } from 'builtin';
2018-03-12 16:48:29 +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-12 16:50:09 +01:00
window._bd = {
DOM,
BdUI,
Modals,
Reflection,
Patcher,
Events,
CssEditor,
Globals,
ExtModuleManager,
PluginManager,
ThemeManager,
ModuleManager,
WebpackModules,
Settings,
Database,
ReactComponents,
DiscordApi,
Logger,
ClientIPC,
Utils,
EmoteModule
}
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
}
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();
Modals.showContentManagerErrors();
if (!ignoreExternal) {
await ExtModuleManager.loadAllModules(true);
await PluginManager.loadAllPlugins(true);
await ThemeManager.loadAllThemes(true);
}
if (!Settings.get('core', 'advanced', 'ignore-content-manager-errors'))
Modals.showContentManagerErrors();
2018-03-07 09:12:44 +01:00
Events.emit('ready');
Events.emit('discord-ready');
2018-03-10 04:05:12 +01:00
EmoteModule.observe();
2018-03-07 09:12:44 +01:00
} catch (err) {
Logger.err('main', ['FAILED TO LOAD!', err]);
2018-03-07 09:12:44 +01:00
}
2018-01-30 16:59:27 +01:00
}
2018-01-29 18:56:48 +01:00
globalReady() {
2018-01-31 16:45:25 +01:00
BdUI.initUiEvents();
2018-01-29 19:00:31 +01:00
this.vueInstance = BdUI.injectUi();
this.init();
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 {
2018-03-12 16:48:29 +01:00
let instance = null;
function init() {
instance = new BetterDiscord();
}
window.Patcher = Patcher;
Events.on('react-ensure', init);
function ensureReact() {
if (!window.webpackJsonp || !WebpackModules.getModuleByName('React')) return setTimeout(ensureReact, 10);
ReactComponents.getComponent('Message').then(Message => {
Events.emit('react-ensure');
Message.patchRender([{
selector: '.message',
method: 'replace',
fn: function (item) {
if (!this.props || !this.props.message) return item;
const { message } = this.props;
const { id, colorString, bot, author, attachments, embeds } = message;
item.props['data-message-id'] = id;
item.props['data-colourstring'] = colorString;
2018-03-12 16:52:24 +01:00
if (author && author.id) item.props['data-user-id'] = author.id;
2018-03-12 16:48:29 +01:00
if (bot || (author && author.bot)) item.props.className += ' bd-isBot';
if (attachments && attachments.length) item.props.className += ' bd-hasAttachments';
if (embeds && embeds.length) item.props.className += ' bd-hasEmbeds';
2018-03-12 16:50:43 +01:00
if (author && author.id === DiscordApi.currentUser.id) item.props.className += ' bd-isCurrentUser';
2018-03-12 16:48:29 +01:00
return item;
}
}]);
});
Patcher.superpatch('React', 'createElement', function (component, retVal) {
if (!component.displayName) return;
ReactComponents.push(component, retVal);
});
}
ensureReact();
}