From c92a293d3178183a2fc499bdc80ca991941abc3d Mon Sep 17 00:00:00 2001 From: Jiiks Date: Thu, 1 Feb 2018 04:36:47 +0200 Subject: [PATCH 1/2] Module manager --- client/src/index.js | 6 ++--- client/src/modules/eventlistener.js | 21 ++++++++++++++++++ client/src/modules/events.js | 1 + client/src/modules/modulemanager.js | 34 +++++++++++++++++++++++++++++ client/src/modules/modules.js | 1 + 5 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 client/src/modules/eventlistener.js create mode 100644 client/src/modules/modulemanager.js diff --git a/client/src/index.js b/client/src/index.js index 3939e608..55183a4d 100644 --- a/client/src/index.js +++ b/client/src/index.js @@ -8,15 +8,14 @@ * LICENSE file in the root directory of this source tree. */ -import { DOM, BdUI, ProfileBadges } from 'ui'; +import { DOM, BdUI } from 'ui'; import BdCss from './styles/index.scss'; -import { Events, CssEditor, Globals, PluginManager, ThemeManager } from 'modules'; +import { Events, CssEditor, Globals, PluginManager, ThemeManager, ModuleManager } from 'modules'; import { ClientLogger as Logger } from 'common'; class BetterDiscord { constructor() { - ProfileBadges.init(); // Not final way to do it DOM.injectStyle(BdCss, 'bdmain'); Events.on('global-ready', this.globalReady.bind(this)); } @@ -24,6 +23,7 @@ class BetterDiscord { async init() { await PluginManager.loadAllPlugins(); await ThemeManager.loadAllThemes(); + ModuleManager.initModules(); Events.emit('ready'); } diff --git a/client/src/modules/eventlistener.js b/client/src/modules/eventlistener.js new file mode 100644 index 00000000..d72a3a48 --- /dev/null +++ b/client/src/modules/eventlistener.js @@ -0,0 +1,21 @@ +/** + * BetterDiscord Event Listener Module + * Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks + * All rights reserved. + * 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. +*/ + +import Events from './events'; + +export default class { + + constructor() { + for (let event of this.events) { + Events.on(event.id, event.callback); + } + } + +} diff --git a/client/src/modules/events.js b/client/src/modules/events.js index 479bdaf4..c60f8ee3 100644 --- a/client/src/modules/events.js +++ b/client/src/modules/events.js @@ -23,4 +23,5 @@ export default class { static emit(...args) { emitter.emit(...args); } + } diff --git a/client/src/modules/modulemanager.js b/client/src/modules/modulemanager.js new file mode 100644 index 00000000..f414fa4d --- /dev/null +++ b/client/src/modules/modulemanager.js @@ -0,0 +1,34 @@ +/** + * BetterDiscord Module Manager + * Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks + * All rights reserved. + * 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. +*/ + +/*Module Manager initializes all modules when everything is ready*/ + +import { Events } from 'modules'; +import { ProfileBadges } from 'ui'; + +export default class { + + static get modules() { + return [ + ProfileBadges + ]; + } + + static initModules() { + for (let module of this.modules) { + try { + if (module.init && module.init instanceof Function) module.init(); + } catch (err) { + console.log(`Failed to initialize module: ${err}`); + } + } + } + +} diff --git a/client/src/modules/modules.js b/client/src/modules/modules.js index 28626124..e5811239 100644 --- a/client/src/modules/modules.js +++ b/client/src/modules/modules.js @@ -6,3 +6,4 @@ export { default as ThemeManager } from './thememanager'; export { default as Globals } from './globals'; export { default as Vendor } from './vendor'; export { default as WebpackModules } from './webpackmodules'; +export { default as ModuleManager } from './modulemanager'; \ No newline at end of file From 600eed9dbce2ff1c6144db6370e0d275c662171b Mon Sep 17 00:00:00 2001 From: Jiiks Date: Thu, 1 Feb 2018 05:00:28 +0200 Subject: [PATCH 2/2] Eventlistener module base --- client/src/modules/eventlistener.js | 7 ++++--- client/src/modules/module.js | 4 ++-- client/src/modules/modulemanager.js | 6 +++--- client/src/modules/modules.js | 3 ++- client/src/ui/bdui.js | 1 - client/src/ui/profilebadges.js | 24 +++++++++++++----------- 6 files changed, 24 insertions(+), 21 deletions(-) diff --git a/client/src/modules/eventlistener.js b/client/src/modules/eventlistener.js index d72a3a48..042a9697 100644 --- a/client/src/modules/eventlistener.js +++ b/client/src/modules/eventlistener.js @@ -8,12 +8,13 @@ * LICENSE file in the root directory of this source tree. */ +import Module from './module'; import Events from './events'; -export default class { +export default class extends Module { - constructor() { - for (let event of this.events) { + events() { + for (let event of this.eventBindings) { Events.on(event.id, event.callback); } } diff --git a/client/src/modules/module.js b/client/src/modules/module.js index c6750d7f..c786e366 100644 --- a/client/src/modules/module.js +++ b/client/src/modules/module.js @@ -20,10 +20,10 @@ export default class Module { args } this.setState = this.setState.bind(this); - this.init(); + this.initialize(); } - init() { + initialize() { if (this.bindings) this.bindings(); if (this.setInitialState) this.setInitialState(this.state); if (this.events) this.events(); diff --git a/client/src/modules/modulemanager.js b/client/src/modules/modulemanager.js index f414fa4d..c2ff7d14 100644 --- a/client/src/modules/modulemanager.js +++ b/client/src/modules/modulemanager.js @@ -16,9 +16,9 @@ import { ProfileBadges } from 'ui'; export default class { static get modules() { - return [ - ProfileBadges - ]; + return this._modules ? this._modules : (this._modules = [ + new ProfileBadges() + ]); } static initModules() { diff --git a/client/src/modules/modules.js b/client/src/modules/modules.js index e5811239..07419b56 100644 --- a/client/src/modules/modules.js +++ b/client/src/modules/modules.js @@ -6,4 +6,5 @@ export { default as ThemeManager } from './thememanager'; export { default as Globals } from './globals'; export { default as Vendor } from './vendor'; export { default as WebpackModules } from './webpackmodules'; -export { default as ModuleManager } from './modulemanager'; \ No newline at end of file +export { default as ModuleManager } from './modulemanager'; +export { default as EventListener } from './eventlistener'; \ No newline at end of file diff --git a/client/src/ui/bdui.js b/client/src/ui/bdui.js index dd6646dd..23ef2c8c 100644 --- a/client/src/ui/bdui.js +++ b/client/src/ui/bdui.js @@ -17,7 +17,6 @@ import { Utils } from 'common'; export default class { static initUiEvents() { - //this.profilePopupModule.open const defer = setInterval(() => { if (!this.profilePopupModule) return; clearInterval(defer); diff --git a/client/src/ui/profilebadges.js b/client/src/ui/profilebadges.js index 0aa9bf74..9c62df9f 100644 --- a/client/src/ui/profilebadges.js +++ b/client/src/ui/profilebadges.js @@ -8,18 +8,24 @@ * LICENSE file in the root directory of this source tree. */ +import { EventListener } from 'modules'; import DOM from './dom'; import { BdBadge } from './components/bd'; import VueInjector from './vueinjector'; -import { Events } from 'modules'; -export default class { +export default class extends EventListener { - static init() { - Events.on('ui-event', this.uiEvent.bind(this)); + bindings() { + this.uiEvent = this.uiEvent.bind(this); } - static uiEvent(e) { + get eventBindings() { + return [ + { id: 'ui-event', callback: this.uiEvent } + ]; + } + + uiEvent(e) { const { event, data } = e; if (event !== 'profile-popup-open') return; const { userid } = data; @@ -28,7 +34,7 @@ export default class { this.inject(userid); } - static inject(userid) { + inject(userid) { const c = this.contributors.find(c => c.id === userid); if (!c) return; @@ -52,11 +58,7 @@ export default class { }, 400); } - static filter(mutation) { - return mutation.target.firstChild && mutation.target.className.includes('modal'); - } - - static get contributors() { + get contributors() { return [ { 'id': '81388395867156480', 'webdev': true, 'developer': true, 'contributor': true }, // Jiiks { 'id': '98003542823944192', 'webdev': false, 'developer': true, 'contributor': true }, // Pohky