TopRoleEverywhere now uses patching instead of observing

This commit is contained in:
Mirco Wittrien 2019-01-02 11:51:56 +01:00
parent 430ac26541
commit ace409df4b
1 changed files with 107 additions and 180 deletions

View File

@ -2,6 +2,12 @@
class TopRoleEverywhere { class TopRoleEverywhere {
initConstructor () { initConstructor () {
this.patchModules = {
"NameTag":"componentDidMount",
"Popout":"componentDidMount",
"StandardSidebarView":"componentWillUnmount"
};
this.css = ` this.css = `
.TRE-tag { .TRE-tag {
border-radius: 3px; border-radius: 3px;
@ -13,7 +19,9 @@ class TopRoleEverywhere {
height: 15px; height: 15px;
line-height: 13px; line-height: 13px;
margin-left: 6px; margin-left: 6px;
overflow: hidden;
padding: 1px 2px; padding: 1px 2px;
text-overflow: ellipsis;
text-transform: uppercase; text-transform: uppercase;
text-indent: 0px !important; text-indent: 0px !important;
vertical-align: top; vertical-align: top;
@ -35,8 +43,8 @@ class TopRoleEverywhere {
includeColorless: {value:false, description:"Include colorless roles."}, includeColorless: {value:false, description:"Include colorless roles."},
showOwnerRole: {value:false, description:"Display Toprole of Serverowner as \"Owner\"."}, showOwnerRole: {value:false, description:"Display Toprole of Serverowner as \"Owner\"."},
disableForBots: {value:false, description:"Disable Toprole for Bots."}, disableForBots: {value:false, description:"Disable Toprole for Bots."},
addUserID: {value:false, description:"Add the UserID as a Tag to the Chat Window."}, addUserID: {value:false, description:"Add the info.id as a Tag to the Chat Window."},
darkIdTag: {value:false, description:"Use a dark version for the UserID-Tag."} darkIdTag: {value:false, description:"Use a dark version for the info.id-Tag."}
} }
}; };
} }
@ -45,20 +53,20 @@ class TopRoleEverywhere {
getDescription () {return "Adds the highest role of a user as a tag.";} getDescription () {return "Adds the highest role of a user as a tag.";}
getVersion () {return "2.7.4";} getVersion () {return "2.7.5";}
getAuthor () {return "DevilBro";} getAuthor () {return "DevilBro";}
getSettingsPanel () { getSettingsPanel () {
if (!this.started || typeof BDFDB !== "object") return; if (!this.started || typeof BDFDB !== "object") return;
var settings = BDFDB.getAllData(this, "settings"); let settings = BDFDB.getAllData(this, "settings");
var settingshtml = `<div class="${this.getName()}-settings DevilBro-settings"><div class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.title + BDFDB.disCNS.size18 + BDFDB.disCNS.height24 + BDFDB.disCNS.weightnormal + BDFDB.disCN.marginbottom8}">${this.getName()}</div><div class="DevilBro-settings-inner">`; let settingshtml = `<div class="${this.getName()}-settings DevilBro-settings"><div class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.title + BDFDB.disCNS.size18 + BDFDB.disCNS.height24 + BDFDB.disCNS.weightnormal + BDFDB.disCN.marginbottom8}">${this.getName()}</div><div class="DevilBro-settings-inner">`;
for (let key in settings) { for (let key in settings) {
settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.flex2 + BDFDB.disCNS.horizontal + BDFDB.disCNS.horizontal2 + BDFDB.disCNS.directionrow + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 1 1 auto;"><h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.title + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.size16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style="flex: 1 1 auto;">${this.defaults.settings[key].description}</h3><div class="${BDFDB.disCNS.flexchild + BDFDB.disCNS.switchenabled + BDFDB.disCNS.switch + BDFDB.disCNS.switchvalue + BDFDB.disCNS.switchsizedefault + BDFDB.disCNS.switchsize + BDFDB.disCN.switchthemedefault}" style="flex: 0 0 auto;"><input type="checkbox" value="${key}" class="${BDFDB.disCNS.switchinnerenabled + BDFDB.disCN.switchinner}"${settings[key] ? " checked" : ""}></div></div>`; settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.flex2 + BDFDB.disCNS.horizontal + BDFDB.disCNS.horizontal2 + BDFDB.disCNS.directionrow + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 1 1 auto;"><h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.title + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.size16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style="flex: 1 1 auto;">${this.defaults.settings[key].description}</h3><div class="${BDFDB.disCNS.flexchild + BDFDB.disCNS.switchenabled + BDFDB.disCNS.switch + BDFDB.disCNS.switchvalue + BDFDB.disCNS.switchsizedefault + BDFDB.disCNS.switchsize + BDFDB.disCN.switchthemedefault}" style="flex: 0 0 auto;"><input type="checkbox" value="${key}" class="${BDFDB.disCNS.switchinnerenabled + BDFDB.disCN.switchinner}"${settings[key] ? " checked" : ""}></div></div>`;
} }
settingshtml += `</div></div>`; settingshtml += `</div></div>`;
var settingspanel = $(settingshtml)[0]; let settingspanel = $(settingshtml)[0];
BDFDB.initElements(settingspanel); BDFDB.initElements(settingspanel);
@ -72,7 +80,7 @@ class TopRoleEverywhere {
load () {} load () {}
start () { start () {
var libraryScript = null; let libraryScript = null;
if (typeof BDFDB !== "object" || typeof BDFDB.isLibraryOutdated !== "function" || BDFDB.isLibraryOutdated()) { if (typeof BDFDB !== "object" || typeof BDFDB.isLibraryOutdated !== "function" || BDFDB.isLibraryOutdated()) {
libraryScript = document.querySelector('head script[src="https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"]'); libraryScript = document.querySelector('head script[src="https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"]');
if (libraryScript) libraryScript.remove(); if (libraryScript) libraryScript.remove();
@ -94,57 +102,7 @@ class TopRoleEverywhere {
this.GuildStore = BDFDB.WebModules.findByProperties("getGuild"); this.GuildStore = BDFDB.WebModules.findByProperties("getGuild");
this.UserGuildState = BDFDB.WebModules.findByProperties("getGuildId", "getLastSelectedGuildId"); this.UserGuildState = BDFDB.WebModules.findByProperties("getGuildId", "getLastSelectedGuildId");
var observer = null; BDFDB.WebModules.forceAllUpdates(this);
observer = new MutationObserver((changes, _) => {
changes.forEach(
(change, i) => {
if (change.addedNodes) {
change.addedNodes.forEach((node) => {
if (node.tagName && node.querySelector(BDFDB.dotCN.memberusername) && BDFDB.getData("showInMemberList", this, "settings")) {
this.addRoleTag(node, "list", false);
}
});
}
}
);
});
BDFDB.addObserver(this, BDFDB.dotCN.memberswrap, {name:"userListObserver",instance:observer}, {childList:true, subtree:true});
observer = new MutationObserver((changes, _) => {
changes.forEach(
(change, i) => {
if (change.addedNodes) {
change.addedNodes.forEach((node) => {
if (BDFDB.getData("showInChat", this, "settings")) {
if (node.tagName && node.querySelector(BDFDB.dotCN.messageusername)) {
this.addRoleTag(node, "chat", BDFDB.getDiscordMode() == "compact");
}
}
});
}
}
);
});
BDFDB.addObserver(this, BDFDB.dotCN.messages, {name:"chatWindowObserver",instance:observer}, {childList:true, subtree:true});
observer = new MutationObserver((changes, _) => {
changes.forEach(
(change, i) => {
if (change.removedNodes) {
change.removedNodes.forEach((node) => {
if (node && $(node).attr("layer-id") == "user-settings" && this.updateTags) {
this.updateDetails = false;
this.addDetails(document);
}
});
}
}
);
});
BDFDB.addObserver(this, BDFDB.dotCN.layers, {name:"settingsWindowObserver",instance:observer}, {childList:true});
this.loadRoleTags();
} }
else { else {
console.error(this.getName() + ": Fatal Error: Could not load BD functions!"); console.error(this.getName() + ": Fatal Error: Could not load BD functions!");
@ -153,93 +111,70 @@ class TopRoleEverywhere {
stop () { stop () {
if (typeof BDFDB === "object") { if (typeof BDFDB === "object") {
document.querySelectorAll(".TRE-tag").forEach(node=>{node.remove();}); BDFDB.removeEles(".TRE-tag");
BDFDB.unloadMessage(this); BDFDB.unloadMessage(this);
} }
} }
onSwitch () {
if (typeof BDFDB === "object") {
BDFDB.addObserver(this, BDFDB.dotCN.memberswrap, {name:"userListObserver"}, {childList:true, subtree:true});
BDFDB.addObserver(this, BDFDB.dotCN.messages, {name:"chatWindowObserver"}, {childList:true, subtree:true});
this.loadRoleTags();
}
}
// begin of own functions // begin of own functions
updateSettings (settingspanel) { updateSettings (settingspanel) {
var settings = {}; let settings = {};
for (var input of settingspanel.querySelectorAll(BDFDB.dotCN.switchinner)) { for (let input of settingspanel.querySelectorAll(BDFDB.dotCN.switchinner)) {
settings[input.value] = input.checked; settings[input.value] = input.checked;
} }
this.updateTags = true; this.updateTags = true;
BDFDB.saveAllData(settings, this, "settings"); BDFDB.saveAllData(settings, this, "settings");
} }
loadRoleTags() { processNameTag (instance, wrapper) {
document.querySelectorAll(".TRE-tag").forEach(node=>{node.remove();}); if (instance.props && wrapper.classList && wrapper.classList.contains(BDFDB.disCN.membernametag) && BDFDB.getData("showInMemberList", this, "settings")) {
if (!BDFDB.getSelectedServer()) return; this.addRoleTag(instance.props.user, wrapper.querySelector(BDFDB.dotCN.memberusername), "list");
var settings = BDFDB.getAllData(this, "settings");
if (settings.showInMemberList) {
for (let user of document.querySelectorAll(BDFDB.dotCN.member)) {
this.addRoleTag(user, "list", false);
}
}
if (settings.showInChat) {
for (let messagegroup of document.querySelectorAll(BDFDB.dotCN.messagegroupcozy)) {
this.addRoleTag(messagegroup, "chat", false);
}
for (let messagegroup of document.querySelectorAll(BDFDB.dotCN.messagegroupcompact)) {
for (let message of messagegroup.querySelectorAll(BDFDB.dotCN.messagemarkup)) {
this.addRoleTag(message, "chat", true);
}
}
} }
} }
addRoleTag (wrapper, type, compact) { processPopout (instance, wrapper) {
if (!wrapper || !BDFDB.getSelectedServer()) return; let fiber = instance._reactInternalFiber;
var guild = this.GuildStore.getGuild(this.UserGuildState.getGuildId()); if (fiber.return && fiber.return.memoizedProps && fiber.return.memoizedProps.message) {
var member = wrapper.querySelector(BDFDB.dotCN.memberusername); let username = wrapper.querySelector(BDFDB.dotCN.messageusername);
if (!member) member = wrapper.querySelector(BDFDB.dotCN.messageusername); if (username && BDFDB.getData("showInChat", this, "settings")) this.addRoleTag(fiber.return.memoizedProps.message.author, username, "chat");
if (member) member = member.parentElement;
if (compact) wrapper = $(BDFDB.dotCN.messagegroup).has(wrapper)[0];
if (member && member.tagName && !member.querySelector(".TRE-tag")) {
var settings = BDFDB.getAllData(this, "settings");
var userInfo = compact ? BDFDB.getKeyInformation({"node":wrapper,"key":"message"}).author : BDFDB.getKeyInformation({"node":wrapper,"key":"user"});
if (!userInfo || (userInfo.bot && settings.disableForBots)) return;
var userID = userInfo.id;
var role = this.GuildPerms.getHighestRole(guild, userID);
if ((role && (role.colorString || settings.includeColorless)) || userID == 278543574059057154) {
var roleColor = role && role.colorString ? BDFDB.colorCONVERT(role.colorString, "RGBCOMP") : [255,255,255];
var roleName = role ? role.name : "";
var totalwidth, oldwidth, newwidth, maxwidth;
if (type == "list") {
totalwidth = member.style.width
oldwidth = wrapper.querySelector(BDFDB.dotCN.memberinner).style.width;
if (oldwidth && totalwidth) {
totalwidth = parseInt(totalwidth.replace("px",""));
oldwidth = parseInt(oldwidth.replace("px",""));
} }
} }
var tag = $(this.tagMarkup)[0];
member.appendChild(tag);
var borderColor = "rgba(" + roleColor[0] + ", " + roleColor[1] + ", " + roleColor[2] + ", 0.5)"; processStandardSidebarView (instance, wrapper) {
var textColor = "rgb(" + roleColor[0] + ", " + roleColor[1] + ", " + roleColor[2] + ")"; if (this.updateTags) {
var bgColor = "rgba(" + roleColor[0] + ", " + roleColor[1] + ", " + roleColor[2] + ", 0.1)"; this.updateTags = false;
var bgInner = "none"; BDFDB.removeEles(".TRE-tag");
var roleText = roleName; BDFDB.WebModules.forceAllUpdates(this);
}
}
addRoleTag (info, username, type) {
if (!info || !username || username.querySelector(".TRE-tag")) return;
let guild = this.GuildStore.getGuild(this.UserGuildState.getGuildId());
let settings = BDFDB.getAllData(this, "settings");
if (!guild || info.bot && settings.disableForBots) return;
let role = this.GuildPerms.getHighestRole(guild, info.id);
if ((role && (role.colorString || settings.includeColorless)) || info.id == 278543574059057154) {
let roleColor = role && role.colorString ? BDFDB.colorCONVERT(role.colorString, "RGBCOMP") : [255,255,255];
let roleName = role ? role.name : "";
let oldwidth;
if (type == "list") oldwidth = username.getBoundingClientRect().width;
let tag = $(this.tagMarkup)[0];
username.parentElement.appendChild(tag);
let borderColor = "rgba(" + roleColor[0] + ", " + roleColor[1] + ", " + roleColor[2] + ", 0.5)";
let textColor = "rgb(" + roleColor[0] + ", " + roleColor[1] + ", " + roleColor[2] + ")";
let bgColor = "rgba(" + roleColor[0] + ", " + roleColor[1] + ", " + roleColor[2] + ", 0.1)";
let bgInner = "none";
let roleText = roleName;
if (settings.useOtherStyle) { if (settings.useOtherStyle) {
borderColor = "transparent"; borderColor = "transparent";
bgColor = "rgba(" + roleColor[0] + ", " + roleColor[1] + ", " + roleColor[2] + ", 1)"; bgColor = "rgba(" + roleColor[0] + ", " + roleColor[1] + ", " + roleColor[2] + ", 1)";
textColor = roleColor[0] > 180 && roleColor[1] > 180 && roleColor[2] > 180 ? "black" : "white"; textColor = roleColor[0] > 180 && roleColor[1] > 180 && roleColor[2] > 180 ? "black" : "white";
} }
if (userID == 278543574059057154) { if (info.id == 278543574059057154) {
bgColor = "linear-gradient(to right, rgba(255,0,0,0.1), rgba(255,127,0,0.1) , rgba(255,255,0,0.1), rgba(127,255,0,0.1), rgba(0,255,0,0.1), rgba(0,255,127,0.1), rgba(0,255,255,0.1), rgba(0,127,255,0.1), rgba(0,0,255,0.1), rgba(127,0,255,0.1), rgba(255,0,255,0.1), rgba(255,0,127,0.1))"; bgColor = "linear-gradient(to right, rgba(255,0,0,0.1), rgba(255,127,0,0.1) , rgba(255,255,0,0.1), rgba(127,255,0,0.1), rgba(0,255,0,0.1), rgba(0,255,127,0.1), rgba(0,255,255,0.1), rgba(0,127,255,0.1), rgba(0,0,255,0.1), rgba(127,0,255,0.1), rgba(255,0,255,0.1), rgba(255,0,127,0.1))";
bgInner = "linear-gradient(to right, rgba(255,0,0,1), rgba(255,127,0,1) , rgba(255,255,0,1), rgba(127,255,0,1), rgba(0,255,0,1), rgba(0,255,127,1), rgba(0,255,255,1), rgba(0,127,255,1), rgba(0,0,255,1), rgba(127,0,255,1), rgba(255,0,255,1), rgba(255,0,127,1))"; bgInner = "linear-gradient(to right, rgba(255,0,0,1), rgba(255,127,0,1) , rgba(255,255,0,1), rgba(127,255,0,1), rgba(0,255,0,1), rgba(0,255,127,1), rgba(0,255,255,1), rgba(0,127,255,1), rgba(0,0,255,1), rgba(127,0,255,1), rgba(255,0,255,1), rgba(255,0,127,1))";
borderColor = "rgba(255, 0, 255, 0.5)"; borderColor = "rgba(255, 0, 255, 0.5)";
@ -251,38 +186,31 @@ class TopRoleEverywhere {
textColor = "white"; textColor = "white";
} }
} }
else if (settings.showOwnerRole && userID == guild.ownerId) { else if (settings.showOwnerRole && info.id == guild.ownerId) {
roleText = "Owner"; roleText = "Owner";
tag.classList.add("owner-tag"); tag.classList.add("owner-tag");
} }
tag.classList.add(type + "-tag"); tag.classList.add(type + "-tag");
tag.style.setProperty("border", "1px solid " + borderColor); tag.style.setProperty("border", "1px solid " + borderColor);
tag.style.setProperty("background", bgColor); tag.style.setProperty("background", bgColor);
var inner = tag.querySelector(".role-inner"); let inner = tag.querySelector(".role-inner");
inner.style.setProperty("color", textColor); inner.style.setProperty("color", textColor);
inner.style.setProperty("background-image", bgInner); inner.style.setProperty("background-image", bgInner);
inner.style.setProperty("-webkit-background-clip", "text"); inner.style.setProperty("-webkit-background-clip", "text");
inner.textContent = roleText; inner.textContent = roleText;
if (oldwidth && totalwidth) { if (oldwidth && oldwidth < 100 && username.getBoundingClientRect().width < 100) {
newwidth = member.querySelector(BDFDB.dotCN.memberinner).style.width; tag.style.setProperty("max-width", (BDFDB.getParentEle(BDFDB.dotCN.memberinner, username).getBoundingClientRect().width - oldwidth - 15) + "px");
if (newwidth) {
newwidth = parseInt(newwidth.replace("px",""));
if (newwidth < 100 && oldwidth < 100) {
maxwidth = totalwidth - oldwidth - 15;
tag.style.setProperty("max-width", maxwidth+"px");
}
}
} }
} }
if (type == "chat" && settings.addUserID) { if (type == "chat" && settings.addUserID) {
var idtag = $(this.tagMarkup)[0]; let idtag = $(this.tagMarkup)[0];
member.appendChild(idtag); username.parentElement.appendChild(idtag);
var idColor = settings.darkIdTag ? [33,33,33] : [222,222,222]; let idColor = settings.darkIdTag ? [33,33,33] : [222,222,222];
var borderColorID = "rgba(" + idColor[0] + ", " + idColor[1] + ", " + idColor[2] + ", 0.5)"; let borderColorID = "rgba(" + idColor[0] + ", " + idColor[1] + ", " + idColor[2] + ", 0.5)";
var textColorID = "rgb(" + idColor[0] + ", " + idColor[1] + ", " + idColor[2] + ")"; let textColorID = "rgb(" + idColor[0] + ", " + idColor[1] + ", " + idColor[2] + ")";
var bgColorID = "rgba(" + idColor[0] + ", " + idColor[1] + ", " + idColor[2] + ", 0.1)"; let bgColorID = "rgba(" + idColor[0] + ", " + idColor[1] + ", " + idColor[2] + ", 0.1)";
var bgInnerID = "none"; let bgInnerID = "none";
if (settings.useOtherStyle) { if (settings.useOtherStyle) {
borderColorID = "transparent"; borderColorID = "transparent";
bgColorID = "rgba(" + idColor[0] + ", " + idColor[1] + ", " + idColor[2] + ", 1)"; bgColorID = "rgba(" + idColor[0] + ", " + idColor[1] + ", " + idColor[2] + ", 1)";
@ -291,12 +219,11 @@ class TopRoleEverywhere {
idtag.classList.add("id-tag"); idtag.classList.add("id-tag");
idtag.style.setProperty("border", "1px solid " + borderColorID); idtag.style.setProperty("border", "1px solid " + borderColorID);
idtag.style.setProperty("background", bgColorID); idtag.style.setProperty("background", bgColorID);
var idinner = idtag.querySelector(".role-inner"); let idinner = idtag.querySelector(".role-inner");
idinner.style.setProperty("color", textColorID); idinner.style.setProperty("color", textColorID);
idinner.style.setProperty("background-image", bgInnerID); idinner.style.setProperty("background-image", bgInnerID);
idinner.style.setProperty("-webkit-background-clip", "text"); idinner.style.setProperty("-webkit-background-clip", "text");
idinner.textContent = userID; idinner.textContent = info.id;
}
} }
} }
} }