BetterDiscordApp-rauenzi/src/modules/componentpatcher.js

89 lines
4.7 KiB
JavaScript
Raw Normal View History

2019-05-30 17:44:05 +02:00
import WebpackModules from "./webpackmodules";
import DiscordModules from "./discordmodules";
import Utilities from "./utilities";
import Patcher from "./patcher";
2019-05-30 07:06:17 +02:00
import BDLogo from "../ui/icons/bdlogo";
2019-05-28 23:27:25 +02:00
2019-06-24 21:47:24 +02:00
export default new class ComponentPatcher {
2019-05-28 20:19:48 +02:00
initialize() {
Utilities.suppressErrors(this.patchSocial.bind(this), "BD Social Patch")();
Utilities.suppressErrors(this.patchGuildPills.bind(this), "BD Guild Pills Patch")();
Utilities.suppressErrors(this.patchGuildListItems.bind(this), "BD Guild List Items Patch")();
Utilities.suppressErrors(this.patchGuildSeparator.bind(this), "BD Guild Separator Patch")();
2019-05-28 20:19:48 +02:00
}
2019-05-30 07:06:17 +02:00
patchSocial() {
if (this.socialPatch) return;
const TabBar = WebpackModules.getByDisplayName("TabBar");
const Anchor = WebpackModules.getByDisplayName("Anchor");
2019-05-30 07:06:17 +02:00
if (!TabBar || !Anchor) return;
this.socialPatch = Patcher.after("ThemeHelper", TabBar.prototype, "render", (_, __, returnValue) => {
const children = returnValue.props.children;
2019-05-30 07:06:17 +02:00
if (!children || !children.length) return;
if (children[children.length - 2].type.displayName !== "Separator") return;
if (!children[children.length - 1].type.toString().includes("socialLinks")) return;
const original = children[children.length - 1].type;
const newOne = function() {
const returnVal = original(...arguments);
returnVal.props.children.push(DiscordModules.React.createElement(Anchor, {className: "bd-social-link", href: "https://github.com/rauenzi/BetterDiscordApp", rel: "author", title: "BandagedBD", target: "_blank"},
DiscordModules.React.createElement(BDLogo, {size: "16px", className: "bd-social-logo"})
2019-05-30 07:06:17 +02:00
));
return returnVal;
};
children[children.length - 1].type = newOne;
});
2019-05-28 20:19:48 +02:00
}
patchGuildListItems() {
if (this.guildListItemsPatch) return;
const listItemClass = DiscordModules.GuildClasses.listItem.split(" ")[0];
const blobClass = DiscordModules.GuildClasses.blobContainer.split(" ")[0];
const reactInstance = Utilities.getReactInstance(document.querySelector(`.${listItemClass} .${blobClass}`).parentElement);
2019-05-28 20:19:48 +02:00
const GuildComponent = reactInstance.return.type;
if (!GuildComponent) return;
this.guildListItemsPatch = Patcher.after("ThemeHelper", GuildComponent.prototype, "render", (thisObject, _, returnValue) => {
2020-02-28 01:00:12 +01:00
if (!returnValue || !thisObject) return;
const guildData = thisObject.props;
2019-05-28 20:19:48 +02:00
returnValue.props.className += " bd-guild";
if (guildData.unread) returnValue.props.className += " bd-unread";
if (guildData.selected) returnValue.props.className += " bd-selected";
if (guildData.audio) returnValue.props.className += " bd-audio";
if (guildData.video) returnValue.props.className += " bd-video";
if (guildData.badge) returnValue.props.className += " bd-badge";
if (guildData.animatable) returnValue.props.className += " bd-animatable";
return returnValue;
});
2019-05-28 20:19:48 +02:00
}
patchGuildPills() {
if (this.guildPillPatch) return;
const guildPill = WebpackModules.getModule(m => m.default && !m.default.displayName && m.default.toString && m.default.toString().includes("translate3d"));
2019-05-28 20:19:48 +02:00
if (!guildPill) return;
this.guildPillPatch = Patcher.after("ThemeHelper", guildPill, "default", (_, args, returnValue) => {
const props = args[0];
if (props.unread) returnValue.props.className += " bd-unread";
if (props.selected) returnValue.props.className += " bd-selected";
if (props.hovered) returnValue.props.className += " bd-hovered";
return returnValue;
});
2019-05-28 20:19:48 +02:00
}
patchGuildSeparator() {
if (this.guildSeparatorPatch) return;
const Guilds = WebpackModules.getByDisplayName("Guilds");
const guildComponents = WebpackModules.getByProps("renderListItem");
2019-05-28 20:19:48 +02:00
if (!guildComponents || !Guilds) return;
const GuildSeparator = function() {
const returnValue = guildComponents.Separator(...arguments);
returnValue.props.className += " bd-guild-separator";
return returnValue;
};
this.guildSeparatorPatch = Patcher.after("ThemeHelper", Guilds.prototype, "render", (_, __, returnValue) => {
const Separator = Utilities.findInReactTree(returnValue, m => m.type && !m.type.displayName && typeof(m.type) == "function" && Utilities.isEmpty(m.props));
if (!Separator) return;
Separator.type = GuildSeparator;
});
2019-05-28 20:19:48 +02:00
}
2019-05-28 23:27:25 +02:00
};