add important component scanner through reflection

This commit is contained in:
Jiiks 2018-03-14 09:12:00 +02:00
parent ce9a4c6b3d
commit 6473ddaf9f
2 changed files with 40 additions and 3 deletions

View File

@ -2,6 +2,7 @@ import Patcher from './patcher';
import WebpackModules from './webpackmodules';
import DiscordApi from './discordapi';
import { EmoteModule } from 'builtin';
import { Reflection } from 'ui';
class Filters {
static get byPrototypeFields() {
@ -267,7 +268,7 @@ export class ReactAutoPatcher {
}
static async patchMessage() {
this.Message.component = await ReactComponents.getComponent('Message');
this.Message.component = await ReactComponents.getComponent('Message', true, { selector: '.message', displayName: 'Message' });
this.Message.component.on('render', ({ component, retVal, p }) => {
const { message } = component.props;
const { id, colorString, bot, author, attachments, embeds } = message;
@ -349,9 +350,30 @@ export class ReactComponents {
return c;
}
static async getComponent(name) {
static async getComponent(name, important, importantArgs) {
const have = this.components.find(c => c.id === name);
if (have) return have;
if (important) {
const importantInterval = setInterval(() => {
if (this.components.find(c => c.id === name)) {
console.info(`Important component ${name} already found`);
clearInterval(importantInterval);
return;
}
const select = document.querySelector(importantArgs.selector);
if (!select) return;
const reflect = Reflection(select);
if (!reflect.component) {
clearInterval(important);
console.error(`FAILED TO GET IMPORTANT COMPONENT ${name} WITH REFLECTION FROM`, select);
return;
}
if (!reflect.component.displayName) reflect.component.displayName = name;
console.info(`Found important component ${name} with reflection.`);
this.push(reflect.component);
clearInterval(importantInterval);
}, 50);
}
const listener = this.listeners.find(l => l.id === name);
if (!listener) this.listeners.push({
id: name,

View File

@ -82,7 +82,19 @@ class Reflection {
}
static getState(node) {
return this.reactInternalInstance(node).return.stateNode.state;
try {
return this.reactInternalInstance(node).return.stateNode.state;
} catch (err) {
return null;
}
}
static getComponent(node) {
try {
return this.reactInternalInstance(node).return.type;
} catch (err) {
return null;
}
}
}
@ -101,6 +113,9 @@ export default function (node) {
get reactInternalInstance() {
return Reflection.reactInternalInstance(this.node);
}
get component() {
return Reflection.getComponent(this.node);
}
prop(propName) {
const split = propName.split('.');
const first = Reflection.findProp(this.node, split[0]);