add automa, new functions to ui and base reflection

This commit is contained in:
Jiiks 2018-03-08 10:40:29 +02:00
parent ff71042bb0
commit 43359d771c
4 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,80 @@
/**
* BetterDiscord Automated DOM Manipulations
* 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, WebpackModules } from 'modules';
import Reflection from './reflection';
import DOM from './dom';
class TempApi {
static get currentGuildId() {
try {
return WebpackModules.getModuleByName('SelectedGuildStore').getGuildId();
} catch (err) {
return 0;
}
}
static get currentChannelId() {
try {
return WebpackModules.getModuleByName('SelectedChannelStore').getChannelId();
} catch (err) {
return 0;
}
}
static get currentUserId() {
try {
return WebpackModules.getModuleByName('UserStore').getCurrentUser().id;
} catch (err) {
return 0;
}
}
}
export default class {
constructor() {
Events.on('server-switch', e => {
try {
this.appMount.setAttribute('guild-id', TempApi.currentGuildId);
this.appMount.setAttribute('channel-id', TempApi.currentChannelId);
this.setIds();
} catch (err) {
console.log(err);
}
});
Events.on('channel-switch', e => {
try {
this.appMount.setAttribute('guild-id', TempApi.currentGuildId);
this.appMount.setAttribute('channel-id', TempApi.currentChannelId);
this.setIds();
} catch (err) {
console.log(err);
}
});
}
setIds() {
for (let msg of document.querySelectorAll('.message')) {
if (msg.hasAttribute('message-id')) continue;
const message = Reflection.findProp(msg, 'message');
if (!message) continue;
const { id, author } = message;
if (!id || !author) continue;
const currentUser = author.id === TempApi.currentUserId;
DOM.setAttributes(msg, [{ name: 'message-id', value: message.id }]);
const msgGroup = msg.closest('.message-group');
if (!msgGroup) continue;
DOM.setAttributes(msgGroup, [{ name: 'author-id', value: author.id }, { name: 'author-is-currentuser', value: currentUser }]);
}
}
get appMount() {
return document.getElementById('app-mount');
}
}

View File

@ -14,10 +14,12 @@ import { BdSettingsWrapper } from './components';
import BdModals from './components/bd/BdModals.vue';
import { Events, WebpackModules } from 'modules';
import { Utils } from 'common';
import AutoManip from './automanip';
export default class {
static initUiEvents() {
this.autoManip = new AutoManip();
const defer = setInterval(() => {
if (!this.profilePopupModule) return;
clearInterval(defer);

View File

@ -158,4 +158,10 @@ export default class DOM {
style.appendChild(document.createTextNode(css));
return style;
}
static setAttributes(node, attributes) {
for (let attribute of attributes) {
node.setAttribute(attribute.name, attribute.value);
}
}
}

View File

@ -0,0 +1,56 @@
/**
* BetterDiscord Reflection 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.
*/
export default class {
static reactInternalInstance(node) {
if (!Object.keys(node) || !Object.keys(node).length) return null;
const riiKey = Object.keys(node).find(k => k.startsWith('__reactInternalInstance'));
return riiKey ? node[riiKey] : null;
}
static findProp(node, prop) {
const ii = this.reactInternalInstance(node);
if (!ii) return null;
const fir = this.findInReturn(ii, prop);
if (fir) return fir;
return null;
}
static findInReturn(internalInstance, prop) {
const r = internalInstance.return;
if (!r) return null;
const find = this.findMemoizedProp(r, prop);
if (find) return find;
return this.findMemoizedState(r, prop);
}
static findMemoizedProp(obj, prop) {
if (!obj.hasOwnProperty('memoizedProps')) return null;
obj = obj.memoizedProps;
return this.findPropIn(obj, prop);
}
static findMemoizedState(obj, prop) {
if (!obj.hasOwnProperty('memoizedState')) return null;
obj = obj.memoizedState;
return this.findPropIn(obj, prop);
}
static findPropIn(obj, prop) {
if (obj && !(obj instanceof Array) && obj instanceof Object && obj.hasOwnProperty(prop)) return obj[prop];
if (obj && obj instanceof Array) {
const found = obj.find(mp => {
if (mp.props && mp.props.hasOwnProperty(prop)) return true;
});
if (found) return found;
}
return null;
}
}