New message event

This commit is contained in:
Jiiks 2018-03-07 07:04:52 +02:00
parent 605e869413
commit aa3226ce43
3 changed files with 28 additions and 6 deletions

View File

@ -21,7 +21,12 @@ export default class {
static observe() {
Events.on('server-switch', this.injectAll.bind(this));
Events.on('channel-switch', this.injectAll.bind(this));
Events.on('new-message', e => console.log(e)); // TODO
Events.on('discord:MESSAGE_CREATE', e => {
// Assume that it's the last one for now since the event doesn't give the element
const query = document.querySelectorAll('.markup:not(.mutable)');
if (!query) return;
this.injectMarkup(query[query.length - 1], true);
}); // TODO
}
static injectAll() {
@ -60,6 +65,7 @@ export default class {
if (cn.className && cn.className.includes('mutable')) cc = cn;
}
if (cc) sibling.parentElement.removeChild(cc);
if (markup === true) markup = this.cloneMarkup(sibling);
markup.clone = this.injectEmotes(markup.clone);
sibling.parentElement.insertBefore(markup.clone, sibling);
sibling.classList.add('shadow');

View File

@ -28,7 +28,7 @@ class BetterDiscord {
window.bdmodals = Modals;
window.bdlogs = Logger;
window.emotes = EmoteModule;
EmoteModule.observe();
DOM.injectStyle(BdCss, 'bdmain');
Events.on('global-ready', this.globalReady.bind(this));
}

View File

@ -11,6 +11,7 @@
import EventListener from './eventlistener';
import { Utils } from 'common';
import Events from './events';
import WebpackModules from './webpackmodules';
import {
MESSAGE_CREATE
@ -23,6 +24,10 @@ import {
*/
export default class extends EventListener {
init() {
this.hook();
}
bindings() {
this.hook = this.hook.bind(this);
}
@ -33,9 +38,19 @@ export default class extends EventListener {
];
}
hook() {}
hook() {
const self = this;
const orig = this.eventsModule.prototype.emit;
this.eventsModule.prototype.emit = function (...args) {
orig.call(this, ...args);
self.wsc = this;
self.emit(...args);
}
}
get eventsModule() {}
get eventsModule() {
return WebpackModules.getModuleByPrototypes(['setMaxListeners', 'emit']);
}
/**
* Discord emit overload
@ -58,7 +73,7 @@ export default class extends EventListener {
dispatch(e, d) {
Events.emit('raw-event', { type: e, data: d });
let evt = null;
switch (e) {
case this.actions.READ:
Events.emit('discord-ready');
@ -74,7 +89,7 @@ export default class extends EventListener {
});
break;
case this.actions.MESSAGE_CREATE:
Events.emit('discord-event', { type: e, data: new MESSAGE_CREATE(d) });
evt = { type: e, data: new MESSAGE_CREATE(d) };
break;
case 'k':
Events.emit('discord-event', {
@ -86,6 +101,7 @@ export default class extends EventListener {
break;
}
if (evt !== null) Events.emit(`discord:${evt.type}`, evt);
}
/**