BetterDiscordAddons/Plugins/RemoveNicknames/RemoveNicknames.plugin.js

216 lines
12 KiB
JavaScript
Raw Normal View History

2019-09-20 22:32:52 +02:00
//META{"name":"RemoveNicknames","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/RemoveNicknames","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/RemoveNicknames/RemoveNicknames.plugin.js"}*//
2018-10-11 10:21:26 +02:00
class RemoveNicknames {
getName () {return "RemoveNicknames";}
2019-09-26 17:41:52 +02:00
getVersion () {return "1.2.7";}
getAuthor () {return "DevilBro";}
getDescription () {return "Replace all nicknames with the actual accountnames.";}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
constructor () {
2019-02-24 20:27:07 +01:00
this.changelog = {
2019-09-26 17:41:52 +02:00
"added":[["Ignore Elements","Added an option list that let's you costumize in which places nicknames are being removed"]]
2019-02-24 20:27:07 +01:00
};
2019-09-04 12:34:02 +02:00
2019-11-14 17:56:26 +01:00
this.patchedModules = {
after: {
"MemberListItem":"componentDidMount",
"MessageUsername":"componentDidMount",
"TypingUsers":"componentDidUpdate",
"Clickable":"componentDidMount",
"StandardSidebarView":"componentWillUnmount"
}
};
2019-09-04 12:34:02 +02:00
}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
initConstructor () {
2018-10-11 10:21:26 +02:00
this.defaults = {
settings: {
2019-09-26 17:41:52 +02:00
replaceOwn: {value:false, inner:false, description:"Replace your own name:"},
replaceBots: {value:true, inner:false, description:"Replace the nickname of bots:"},
addNickname: {value:false, inner:false, description:"Add nickname as parentheses:"},
swapPositions: {value:false, inner:false, description:"Swap the position of username and nickname:"},
changeInChatWindow: {value:true, inner:true, description:"Messages"},
changeInMentions: {value:true, inner:true, description:"Mentions"},
changeInVoiceChat: {value:true, inner:true, description:"Voice Channels"},
changeInMemberList: {value:true, inner:true, description:"Member List"},
changeInTyping: {value:true, inner:true, description:"Typing List"},
changeInAutoComplete: {value:true, inner:true, description:"Autocomplete Menu"}
2018-10-11 10:21:26 +02:00
}
};
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
getSettingsPanel () {
2020-01-17 19:50:31 +01:00
if (!window.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return;
2019-10-22 23:04:35 +02:00
let settings = BDFDB.DataUtils.get(this, "settings");
2019-10-09 14:18:28 +02:00
var settingshtml = `<div class="${this.name}-settings BDFDB-settings"><div class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.titlesize18 + BDFDB.disCNS.height24 + BDFDB.disCNS.weightnormal + BDFDB.disCN.marginbottom8}">${this.name}</div><div class="BDFDB-settings-inner">`;
2018-10-11 10:21:26 +02:00
for (let key in settings) {
2019-10-09 14:18:28 +02:00
if (!this.defaults.settings[key].inner) settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 1 1 auto;"><h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.titlesize16 + 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="settings ${key}" class="${BDFDB.disCNS.switchinnerenabled + BDFDB.disCN.switchinner} settings-switch"${settings[key] ? " checked" : ""}></div></div>`;
2018-10-11 10:21:26 +02:00
}
2019-10-09 14:18:28 +02:00
settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 1 1 auto;"><h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.titlesize16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style="flex: 0 0 auto;">Remove Nicknames in:</h3></div><div class="BDFDB-settings-inner-list">`;
2019-09-26 17:41:52 +02:00
for (let key in settings) {
2019-10-09 14:18:28 +02:00
if (this.defaults.settings[key].inner) settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.horizontal + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 1 1 auto;"><h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.titlesize16 + 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="settings ${key}" class="${BDFDB.disCNS.switchinnerenabled + BDFDB.disCN.switchinner} settings-switch"${settings[key] ? " checked" : ""}></div></div>`;
2019-09-26 17:41:52 +02:00
}
settingshtml += `</div>`;
2018-10-11 10:21:26 +02:00
settingshtml += `</div></div>`;
2019-01-26 22:45:19 +01:00
2019-10-23 11:10:01 +02:00
let settingspanel = BDFDB.DOMUtils.create(settingshtml);
2018-10-11 10:21:26 +02:00
2019-02-03 22:11:35 +01:00
BDFDB.initElements(settingspanel, this);
;
2018-10-11 10:21:26 +02:00
return settingspanel;
}
//legacy
load () {}
start () {
2020-01-17 19:50:31 +01:00
if (!window.BDFDB) window.BDFDB = {myPlugins:{}};
if (window.BDFDB && window.BDFDB.myPlugins && typeof window.BDFDB.myPlugins == "object") window.BDFDB.myPlugins[this.getName()] = this;
2020-01-21 12:56:26 +01:00
let libraryScript = document.querySelector("head script#BDFDBLibraryScript");
2019-05-26 13:55:26 +02:00
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
2019-02-03 22:11:35 +01:00
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
2019-05-26 13:55:26 +02:00
libraryScript.setAttribute("id", "BDFDBLibraryScript");
2019-02-03 22:11:35 +01:00
libraryScript.setAttribute("type", "text/javascript");
2019-10-18 10:56:41 +02:00
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js");
2019-02-03 22:11:35 +01:00
libraryScript.setAttribute("date", performance.now());
2020-01-14 00:06:07 +01:00
libraryScript.addEventListener("load", _ => {this.initialize();});
2019-02-03 22:11:35 +01:00
document.head.appendChild(libraryScript);
2019-05-26 13:55:26 +02:00
}
2020-01-17 19:50:31 +01:00
else if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
2020-01-14 00:06:07 +01:00
this.startTimeout = setTimeout(_ => {
2019-11-01 10:27:07 +01:00
try {return this.initialize();}
catch (err) {console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not initiate plugin! " + err);}
}, 30000);
2018-10-11 10:21:26 +02:00
}
initialize () {
2020-01-17 19:50:31 +01:00
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-01-22 11:05:54 +01:00
if (this.started) return;
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.init(this);
2019-01-26 22:45:19 +01:00
this.reseting = false;
2019-10-22 18:55:25 +02:00
BDFDB.ModuleUtils.forceAllUpdates(this);
2018-10-11 10:21:26 +02:00
}
2019-11-01 10:14:50 +01:00
else console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not load BD functions!");
2018-10-11 10:21:26 +02:00
}
stop () {
2020-01-17 19:50:31 +01:00
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-10-22 11:37:23 +02:00
this.stopping = true;
this.reseting = true;
2019-01-26 22:45:19 +01:00
2019-10-22 18:55:25 +02:00
BDFDB.ModuleUtils.forceAllUpdates(this);
2019-01-26 22:45:19 +01:00
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.clear(this);
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
// begin of own functions
2019-01-26 22:45:19 +01:00
2019-09-11 12:14:43 +02:00
processMemberListItem (instance, wrapper, returnvalue) {
2019-10-22 18:55:25 +02:00
let user = BDFDB.ReactUtils.getValue(instance, "props.user");
2019-07-18 12:18:54 +02:00
if (user) {
let username = wrapper.querySelector(BDFDB.dotCN.memberusername);
2019-10-23 11:10:01 +02:00
if (username) BDFDB.DOMUtils.setText(username, this.getNewName(user, wrapper));
2019-03-04 13:43:21 +01:00
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2019-09-11 12:14:43 +02:00
processMessageUsername (instance, wrapper, returnvalue) {
2019-10-22 18:55:25 +02:00
let message = BDFDB.ReactUtils.getValue(instance, "props.message");
if (message) {
let username = wrapper.querySelector(BDFDB.dotCN.messageusername);
2019-10-23 11:10:01 +02:00
if (username) BDFDB.DOMUtils.setText(username, this.getNewName(message.author, wrapper));
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2019-09-11 12:14:43 +02:00
processTypingUsers (instance, wrapper, returnvalue) {
2019-10-22 18:55:25 +02:00
let users = !instance.props.typingUsers ? [] : Object.keys(instance.props.typingUsers).filter(id => id != BDFDB.UserUtils.me.id).filter(id => !BDFDB.LibraryModules.FriendUtils.isBlocked(id)).map(id => BDFDB.LibraryModules.UserStore.getUser(id)).filter(id => id != null);
wrapper.querySelectorAll("strong").forEach((username, i) => {
2019-10-23 11:10:01 +02:00
if (users[i] && username) BDFDB.DOMUtils.setText(username, this.getNewName(users[i]));
});
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2019-09-11 12:14:43 +02:00
processClickable (instance, wrapper, returnvalue) {
if (!wrapper || !instance.props || !instance.props.className) return;
if (instance.props.tag == "a" && instance.props.className.indexOf(BDFDB.disCN.anchorunderlineonhover) > -1) {
2019-10-23 11:10:01 +02:00
if (BDFDB.DOMUtils.containsClass(wrapper.parentElement, BDFDB.disCN.messagesystemcontent) && wrapper.parentElement.querySelector("a") == wrapper) {
2019-10-22 19:38:25 +02:00
let message = BDFDB.ReactUtils.findValue(wrapper.parentElement, "message", {up:true});
2019-03-22 20:49:46 +01:00
if (message) {
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.setText(wrapper, this.getNewName(message.author, wrapper));
2019-03-22 20:49:46 +01:00
if (message.mentions.length == 1) {
2019-09-11 12:14:43 +02:00
let seconduser = BDFDB.LibraryModules.UserStore.getUser(message.mentions[0]);
2019-03-22 20:49:46 +01:00
let secondwrapper = wrapper.parentElement.querySelectorAll("a")[1];
2019-10-23 11:10:01 +02:00
if (seconduser && secondwrapper) BDFDB.DOMUtils.setText(secondwrapper, this.getNewName(seconduser, wrapper));
2019-03-22 20:49:46 +01:00
}
}
}
}
else if (instance.props.tag == "span" && instance.props.className.indexOf(BDFDB.disCN.mention) > -1) {
2019-10-22 18:55:25 +02:00
let render = BDFDB.ReactUtils.getValue(instance, "_reactInternalFiber.return.return.stateNode.props.render");
2019-05-26 14:29:29 +02:00
if (typeof render == "function") {
var props = render().props;
2019-10-23 11:10:01 +02:00
if (props && props.user) BDFDB.DOMUtils.setText(wrapper, "@" + this.getNewName(props.user, wrapper));
else if (props && props.userId) BDFDB.DOMUtils.setText(wrapper, "@" + this.getNewName(BDFDB.LibraryModules.UserStore.getUser(props.userId), wrapper));
2019-05-26 14:29:29 +02:00
}
}
else if (instance.props.tag == "div" && instance.props.className.indexOf(BDFDB.disCN.voiceuser) > -1) {
2019-10-22 18:55:25 +02:00
let user = BDFDB.ReactUtils.getValue(instance, "_reactInternalFiber.return.memoizedProps.user");
if (user) {
let username = wrapper.querySelector(BDFDB.dotCN.voicename);
2019-10-23 11:10:01 +02:00
if (username) BDFDB.DOMUtils.setText(username, this.getNewName(user, username));
2018-10-11 10:21:26 +02:00
}
}
else if (instance.props.tag == "div" && instance.props.className.indexOf(BDFDB.disCN.autocompleterow) > -1) {
2019-10-22 18:55:25 +02:00
let user = BDFDB.ReactUtils.getValue(instance, "_reactInternalFiber.return.memoizedProps.user");
if (user) {
let username = wrapper.querySelector(BDFDB.dotCN.marginleft8);
2019-10-23 11:10:01 +02:00
if (username) BDFDB.DOMUtils.setText(username, this.getNewName(user, username));
}
}
}
2019-01-26 22:45:19 +01:00
2019-09-11 12:14:43 +02:00
processStandardSidebarView (instance, wrapper, returnvalue) {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
2019-10-22 18:55:25 +02:00
BDFDB.ModuleUtils.forceAllUpdates(this);
}
2018-10-11 10:21:26 +02:00
}
2019-09-03 12:37:14 +02:00
2019-09-26 17:41:52 +02:00
getNewName (info, wrapper) {
2019-09-03 12:37:14 +02:00
if (!info) return null;
2019-10-22 19:49:57 +02:00
let settings = BDFDB.DataUtils.get(this, "settings");
2019-09-11 12:14:43 +02:00
let member = BDFDB.LibraryModules.MemberStore.getMember(BDFDB.LibraryModules.LastGuildStore.getGuildId(), info.id) || {};
2019-10-23 11:10:01 +02:00
let EditUsersData = (BDFDB.BDUtils.isPluginEnabled("EditUsers") ? BDFDB.DataUtils.load("EditUsers", "users") : null, info.id) || {};
2019-10-22 18:55:25 +02:00
if (this.reseting || !member.nick || info.id == BDFDB.UserUtils.me.id && !settings.replaceOwn || info.bot && !settings.replaceBots || this.ignoreElement(wrapper)) return EditUsersData.name || member.nick || info.username;
2019-09-03 12:37:14 +02:00
var username = EditUsersData.name || info.username;
return settings.addNickname ? (settings.swapPositions ? (member.nick + " (" + username + ")") : (username + " (" + member.nick + ")")) : username;
}
2019-09-26 17:41:52 +02:00
ignoreElement (id, wrapper) {
2019-10-22 19:49:57 +02:00
let allenabled = true, settings = BDFDB.ObjectUtils.filter(BDFDB.DataUtils.get(this, "settings"), key => {return this.defaults.settings[key].inner;}, true);
2019-09-26 17:41:52 +02:00
for (let i in settings) if (!settings[i]) {
allenabled = false;
break;
}
if (allenabled) return false;
let key = null;
2019-10-23 11:10:01 +02:00
if (!BDFDB.DOMUtils.containsClass(wrapper, BDFDB.disCN.mention) && BDFDB.DOMUtils.getParent(BDFDB.dotCN.messagegroup, wrapper)) key = "changeInChatWindow";
else if (BDFDB.DOMUtils.containsClass(wrapper, BDFDB.disCN.mention)) key = "changeInMentions";
else if (BDFDB.DOMUtils.getParent(BDFDB.dotCN.voiceuser, wrapper)) key = "changeInVoiceChat";
else if (BDFDB.DOMUtils.getParent(BDFDB.dotCN.members, wrapper)) key = "changeInMemberList";
else if (BDFDB.DOMUtils.getParent(BDFDB.dotCN.typing, wrapper)) key = "changeInTyping";
else if (BDFDB.DOMUtils.getParent(BDFDB.dotCN.autocomplete, wrapper)) key = "changeInAutoComplete";
2019-09-26 17:41:52 +02:00
if (!key || settings[key]) return true;
return false;
}
2018-10-11 10:21:26 +02:00
}