//META{"name":"ChatAliases","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ChatAliases","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/ChatAliases/ChatAliases.plugin.js"}*//
class ChatAliases {
getName () {return "ChatAliases";}
getVersion () {return "2.0.0";}
getAuthor () {return "DevilBro";}
getDescription () {return "Allows the user to configure their own chat-aliases which will automatically be replaced before the message is being sent.";}
constructor () {
this.changelog = {
"fixed":[["File Aliases","Fixed the crash occuring when trying to send a file via an alias"]]
};
this.patchModules = {
"ChannelTextArea":"componentDidMount",
"StandardSidebarView":"componentWillUnmount"
};
}
initConstructor () {
this.defaults = {
configs: {
case: {value:false, description:"Handle the wordvalue case sensitive"},
exact: {value:true, description:"Handle the wordvalue as an exact word and not as part of a word"},
autoc: {value:true, description:"Add this alias in the autocomplete menu (not for Regex)"},
regex: {value:false, description:"Handle the wordvalue as a regex string"},
file: {value:false, description:"Handle the replacevalue as a filepath"}
},
settings: {
addContextMenu: {value:true, description:"Add a ContextMenu entry to faster add new Aliases:"},
addAutoComplete: {value:true, description:"Add an Autocomplete-Menu for Non-Regex Aliases:"}
},
amounts: {
minAliasLength: {value:2, min:1, description:"Minimal Character Length to open Autocomplete-Menu:"}
}
};
this.chataliasesContextEntryMarkup =
`
`;
this.chataliasesAddModalMarkup =
`${this.name}
`;
for (let key in settings) {
settingshtml += `
`;
}
for (let key in amounts) {
settingshtml += `
`;
}
settingshtml += `
`;
settingshtml += `
`;
for (let word in this.aliases) {
settingshtml += `
`;
}
settingshtml += `
`;
settingshtml += `
Remove all added words. Reset
`;
var infoHidden = BDFDB.loadData("hideInfo", this, "hideInfo");
settingshtml += `
`;
settingshtml += `
Case: Will replace words while comparing lowercase/uppercase. apple => apple, not APPLE or AppLe
Not Case: Will replace words while ignoring lowercase/uppercase. apple => apple, APPLE and AppLe
Exact: Will replace words that are exactly the replaceword. apple to pear => applepie stays applepie
Not Exact: Will replace words anywhere they appear. apple to pear => applepieapple to pearpiepear
Autoc: Will appear in the Autocomplete Menu (if enabled).
Regex: Will treat the entered wordvalue as a regular expression.
Help File: If the replacevalue is a filepath it will try to upload the file located at the filepath.
`;
settingshtml += `
`;
let settingspanel = BDFDB.htmlToElement(settingshtml);
BDFDB.initElements(settingspanel, this);
BDFDB.addEventListener(this, settingspanel, "keypress", ".wordInputs", e => {if (e.which == 13) this.updateContainer(settingspanel, e.currentTarget);});
BDFDB.addEventListener(this, settingspanel, "keyup", BDFDB.dotCN.gamenameinput, e => {this.updateWord(e.currentTarget);});
BDFDB.addEventListener(this, settingspanel, "click", ".btn-addword, .remove-word, .remove-all", e => {this.updateContainer(settingspanel, e.currentTarget);});
BDFDB.addEventListener(this, settingspanel, "click", BDFDB.dotCN.checkboxinput, e => {this.updateConfig(e.currentTarget);});
BDFDB.addEventListener(this, settingspanel, "click", ".toggle-info", e => {this.toggleInfo(e.currentTarget);});
return settingspanel;
}
//legacy
load () {}
start () {
if (!global.BDFDB) global.BDFDB = {myPlugins:{}};
if (global.BDFDB && global.BDFDB.myPlugins && typeof global.BDFDB.myPlugins == "object") global.BDFDB.myPlugins[this.getName()] = this;
var libraryScript = document.querySelector('head script#BDFDBLibraryScript');
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
libraryScript.setAttribute("id", "BDFDBLibraryScript");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js");
libraryScript.setAttribute("date", performance.now());
libraryScript.addEventListener("load", () => {this.initialize();});
document.head.appendChild(libraryScript);
this.libLoadTimeout = setTimeout(() => {
libraryScript.remove();
BDFDB.LibraryRequires.request("https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js", (error, response, body) => {
if (body) {
libraryScript = document.createElement("script");
libraryScript.setAttribute("id", "BDFDBLibraryScript");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("date", performance.now());
libraryScript.innerText = body;
document.head.appendChild(libraryScript);
}
this.initialize();
});
}, 15000);
}
else if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
this.startTimeout = setTimeout(() => {this.initialize();}, 30000);
}
initialize () {
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
if (this.started) return;
BDFDB.loadMessage(this);
this.aliases = BDFDB.loadAllData(this, "words");
BDFDB.addEventListener(document, "click", e => {
if (!e.target.tagName === "TEXTAREA") BDFDB.removeEles(".autocompleteAliases", ".autocompleteAliasesRow");
});
BDFDB.WebModules.forceAllUpdates(this);
}
else {
console.error(`%c[${this.getName()}]%c`, 'color: #3a71c1; font-weight: 700;', '', 'Fatal Error: Could not load BD functions!');
}
}
stop () {
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
BDFDB.removeEles(".autocompleteAliases", ".autocompleteAliasesRow");
BDFDB.unloadMessage(this);
}
}
// begin of own functions
updateContainer (settingspanel, ele) {
var wordvalue = null, replacevalue = null, action = ele.getAttribute("action");
var update = () => {
BDFDB.saveAllData(this.aliases, this, "words");
var containerhtml = ``;
for (let word in this.aliases) {
containerhtml += `
`;
}
settingspanel.querySelector(".alias-list").innerHTML = containerhtml;
BDFDB.initElements(settingspanel, this);
};
if (action == "add") {
var wordinput = settingspanel.querySelector("#input-wordvalue");
var replaceinput = settingspanel.querySelector("#input-replacevalue");
if (wordinput.value && wordinput.value.trim().length > 0 && replaceinput.value && replaceinput.value.trim().length > 0) {
this.saveWord(wordinput.value.trim(), replaceinput.value.trim(), settingspanel.querySelector("#input-file"));
wordinput.value = null;
replaceinput.value = null;
update();
}
}
else if (action == "remove") {
wordvalue = ele.getAttribute("word");
if (wordvalue) {
delete this.aliases[wordvalue];
update();
}
}
else if (action == "removeall") {
BDFDB.openConfirmModal(this, "Are you sure you want to remove all added Words from your list?", () => {
this.aliases = {};
update();
});
}
}
saveWord (wordvalue, replacevalue, fileselection, configs = BDFDB.getAllData(this, "configs")) {
if (!wordvalue || !replacevalue || !fileselection) return;
var filedata = null;
if (fileselection.files && fileselection.files[0] && BDFDB.LibraryRequires.fs.existsSync(replacevalue)) {
filedata = JSON.stringify({
data: BDFDB.LibraryRequires.fs.readFileSync(replacevalue).toString("base64"),
name: fileselection.files[0].name,
type: fileselection.files[0].type
});
}
this.aliases[wordvalue] = {
replace: replacevalue,
filedata: filedata,
case: configs.case,
exact: wordvalue.indexOf(" ") > -1 ? false : configs.exact,
autoc: configs.regex ? false : configs.autoc,
regex: configs.regex,
file: filedata != null
};
}
updateWord (ele) {
clearTimeout(ele.updateTimeout);
ele.updateTimeout = setTimeout(() => {
var card = ele.parentElement.parentElement;
var oldwordvalue = ele.getAttribute("word");
if (oldwordvalue && this.aliases[oldwordvalue]) {
var wordinput = card.querySelector(".word-name");
var replaceinput = card.querySelector(".replace-name");
var removebutton = card.querySelector(".remove-word");
var newwordvalue = wordinput.value;
var newreplacevalue = replaceinput.value;
wordinput.setAttribute("word", newwordvalue);
wordinput.setAttribute("value", newwordvalue);
replaceinput.setAttribute("word", newwordvalue);
replaceinput.setAttribute("value", newreplacevalue);
removebutton.setAttribute("word", newwordvalue);
this.aliases[newwordvalue] = this.aliases[oldwordvalue];
this.aliases[newwordvalue].replace = newreplacevalue;
if (newwordvalue != oldwordvalue) delete this.aliases[oldwordvalue];
BDFDB.saveAllData(this.aliases, this, "words");
}
},500);
}
updateConfig (ele) {
var wordvalue = ele.getAttribute("word");
var config = ele.getAttribute("config");
if (wordvalue && this.aliases[wordvalue] && config) {
this.aliases[wordvalue][config] = ele.checked;
BDFDB.saveAllData(this.aliases, this, "words");
}
}
toggleInfo (ele) {
BDFDB.toggleClass(ele.querySelector("svg"), BDFDB.disCN.directionright);
BDFDB.toggleEles(ele.nextElementSibling);
BDFDB.saveData("hideInfo", BDFDB.isEleHidden(ele.nextElementSibling), this, "hideInfo");
}
onNativeContextMenu (instance, menu) {
if (instance.props && instance.props.value && instance.props.value.trim() && !menu.querySelector(".chataliases-item")) {
if ((instance.props.type == "NATIVE_TEXT" || instance.props.type == "CHANNEL_TEXT_AREA") && BDFDB.getData("addContextMenu", this, "settings")) this.appendItem(menu, instance.props.value.trim());
}
}
onMessageContextMenu (instance, menu) {
if (instance.props && instance.props.message && instance.props.channel && instance.props.target && !menu.querySelector(".chataliases-item")) {
let text = document.getSelection().toString().trim();
if (text && BDFDB.getData("addContextMenu", this, "settings")) this.appendItem(menu, text);
}
}
appendItem (menu, text) {
let chataliasesContextEntry = BDFDB.htmlToElement(this.chataliasesContextEntryMarkup);
let devgroup = BDFDB.getContextMenuDevGroup(menu);
if (devgroup) devgroup.parentElement.insertBefore(chataliasesContextEntry, devgroup);
else menu.appendChild(chataliasesContextEntry, menu);
chataliasesContextEntry.querySelector(".chataliases-item").addEventListener("click", () => {
BDFDB.closeContextMenu(menu);
this.openAddModal(text);
});
}
processStandardSidebarView (instance, wrapper) {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
BDFDB.WebModules.forceAllUpdates(this);
}
}
processChannelTextArea (instance, wrapper) {
if (instance.props && instance.props.channel && instance.props.type) {
var textarea = wrapper.querySelector("textarea");
if (!textarea) return;
var settings = BDFDB.getAllData(this, "settings");
BDFDB.addEventListener(this, textarea, "input", () => {
if (this.format) {
this.format = false;
textarea.focus();
textarea.selectionStart = 0;
textarea.selectionEnd = textarea.value.length;
if (document.activeElement == textarea) {
var messageInput = this.formatText(textarea.value);
if (messageInput) {
if (messageInput.text != null) {
document.execCommand("insertText", false, messageInput.text ? messageInput.text + " " : "");
}
if (messageInput.files.length > 0 && (instance.props.channel.type == 1 || BDFDB.isUserAllowedTo("ATTACH_FILES"))) {
BDFDB.LibraryModules.UploadUtils.instantBatchUpload(instance.props.channel.id, messageInput.files);
}
}
}
}
});
BDFDB.addEventListener(this, textarea, "keydown", e => {
let autocompletemenu = textarea.parentElement.querySelector(BDFDB.dotCN.autocomplete);
if (autocompletemenu && (e.which == 9 || e.which == 13)) {
if (BDFDB.containsClass(autocompletemenu.querySelector(BDFDB.dotCN.autocompleteselected).parentElement, "autocompleteAliasesRow")) {
BDFDB.stopEvent(e);
this.swapWordWithAlias(textarea);
}
}
else if (autocompletemenu && (e.which == 38 || e.which == 40)) {
let autocompleteitems = autocompletemenu.querySelectorAll(BDFDB.dotCN.autocompleteselectable + ":not(.autocompleteAliasesSelector)");
let selected = autocompletemenu.querySelector(BDFDB.dotCN.autocompleteselected);
if (BDFDB.containsClass(selected, "autocompleteAliasesSelector") || autocompleteitems[e.which == 38 ? 0 : (autocompleteitems.length-1)] == selected) {
BDFDB.stopEvent(e);
let next = this.getNextSelection(autocompletemenu, null, e.which == 38 ? false : true);
BDFDB.removeClass(selected, BDFDB.disCN.autocompleteselected);
BDFDB.addClass(selected, BDFDB.disCN.autocompleteselector);
BDFDB.addClass(next, BDFDB.disCN.autocompleteselected);
}
}
else if (textarea.value && !e.shiftKey && e.which == 13 && !autocompletemenu && textarea.value.indexOf("s/") != 0) {
this.format = true;
textarea.dispatchEvent(new Event("input"));
}
else if (!e.ctrlKey && e.which != 16 && settings.addAutoComplete && textarea.selectionStart == textarea.selectionEnd && textarea.selectionEnd == textarea.value.length) {
clearTimeout(textarea.ChatAliasAutocompleteTimeout);
textarea.ChatAliasAutocompleteTimeout = setTimeout(() => {this.addAutoCompleteMenu(textarea);},100);
}
if (!e.ctrlKey && e.which != 38 && e.which != 40 && !(e.which == 39 && textarea.selectionStart == textarea.selectionEnd && textarea.selectionEnd == textarea.value.length)) BDFDB.removeEles(".autocompleteAliases", ".autocompleteAliasesRow");
});
BDFDB.addEventListener(this, textarea, "click", e => {
if (settings.addAutoComplete && textarea.selectionStart == textarea.selectionEnd && textarea.selectionEnd == textarea.value.length) setImmediate(() => {this.addAutoCompleteMenu(textarea);});
});
}
}
addAutoCompleteMenu (textarea) {
if (!textarea.value || textarea.parentElement.querySelector(".autocompleteAliasesRow")) return;
let words = textarea.value.split(/\s/);
let lastword = words[words.length-1].trim();
if (words.length == 1 && BDFDB.isPluginEnabled("WriteUpperCase")) {
let first = lastword.charAt(0);
if (first === first.toUpperCase() && lastword.toLowerCase().indexOf("http") == 0) {
lastword = lastword.charAt(0).toLowerCase() + lastword.slice(1);
}
else if (first === first.toLowerCase() && first !== first.toUpperCase() && lastword.toLowerCase().indexOf("http") != 0) {
lastword = lastword.charAt(0).toUpperCase() + lastword.slice(1);
}
}
if (lastword && BDFDB.getData("minAliasLength", this, "amounts") <= lastword.length) {
let matchedaliases = {};
for (let word in this.aliases) {
let aliasdata = this.aliases[word];
if (!aliasdata.regex && aliasdata.autoc) {
if (aliasdata.exact) {
if (aliasdata.case && word.indexOf(lastword) == 0) matchedaliases[word] = aliasdata;
else if (!aliasdata.case && word.toLowerCase().indexOf(lastword.toLowerCase()) == 0) matchedaliases[word] = aliasdata;
}
else {
if (aliasdata.case && word.indexOf(lastword) > -1) matchedaliases[word] = aliasdata;
else if (!aliasdata.case && word.toLowerCase().indexOf(lastword.toLowerCase()) > -1) matchedaliases[word] = aliasdata;
}
}
}
if (!BDFDB.isObjectEmpty(matchedaliases)) {
let autocompletemenu = textarea.parentElement.querySelector(BDFDB.dotCNS.autocomplete + BDFDB.dotCN.autocompleteinner), amount = 15;
if (!autocompletemenu) {
autocompletemenu = BDFDB.htmlToElement(`
`);
textarea.parentElement.appendChild(autocompletemenu);
autocompletemenu = autocompletemenu.firstElementChild;
}
else {
amount -= autocompletemenu.querySelectorAll(BDFDB.dotCN.autocompleteselectable).length;
}
let autocompleterowheader = BDFDB.htmlToElement(`
Aliases: ${BDFDB.encodeToHTML(lastword)}
`);
autocompletemenu.appendChild(autocompleterowheader);
BDFDB.addEventListener(this, autocompletemenu, "mouseenter", BDFDB.dotCN.autocompleteselectable, e => {
var selected = autocompletemenu.querySelectorAll(BDFDB.dotCN.autocompleteselected);
BDFDB.removeClass(selected, BDFDB.disCN.autocompleteselected);
BDFDB.addClass(selected, BDFDB.disCN.autocompleteselector);
BDFDB.addClass(e.currentTarget, BDFDB.disCN.autocompleteselected);
});
for (let word in matchedaliases) {
if (amount-- < 1) break;
let autocompleterow = BDFDB.htmlToElement(`
${BDFDB.encodeToHTML(word)}
${BDFDB.encodeToHTML(matchedaliases[word].replace)}
`);
autocompleterow.querySelector(BDFDB.dotCN.autocompleteselectable).addEventListener("click", () => {this.swapWordWithAlias(textarea);});
autocompletemenu.appendChild(autocompleterow);
}
if (!autocompletemenu.querySelector(BDFDB.dotCN.autocompleteselected)) {
BDFDB.addClass(autocompletemenu.querySelector(".autocompleteAliasesRow " + BDFDB.dotCN.autocompleteselectable), BDFDB.disCN.autocompleteselected);
}
}
}
}
getNextSelection (menu, selected, forward) {
selected = selected ? selected : menu.querySelector(BDFDB.dotCN.autocompleteselected).parentElement;
let next, sibling = forward ? selected.nextElementSibling : selected.previousElementSibling;
if (sibling) {
next = sibling.querySelector(BDFDB.dotCN.autocompleteselectable);
}
else {
let items = menu.querySelectorAll(BDFDB.dotCN.autocompleteselectable);
next = forward ? items[0] : items[items.length-1];
}
return next ? next : this.getNextSelection(menu, sibling, forward);
}
swapWordWithAlias (textarea) {
let aliasword = textarea.parentElement.querySelector(".autocompleteAliasesRow " + BDFDB.dotCN.autocompleteselected + " .aliasword").innerText;
let lastword = textarea.parentElement.querySelector(".autocompleteAliasesRow .lastword").innerText;
if (aliasword && lastword) {
BDFDB.removeEles(".autocompleteAliases", ".autocompleteAliasesRow");
textarea.focus();
textarea.selectionStart = textarea.value.length - lastword.length;
textarea.selectionEnd = textarea.value.length;
document.execCommand("insertText", false, aliasword);
textarea.selectionStart = textarea.value.length;
textarea.selectionEnd = textarea.value.length;
}
}
formatText (text) {
text = text.replace(/([\n\t\r])/g, " $1 ");
var newText = [], files = [], wordAliases = {}, multiAliases = {};
for (let word in this.aliases) {
if (!this.aliases[word].regex && word.indexOf(" ") == -1) wordAliases[word] = this.aliases[word];
else multiAliases[word] = this.aliases[word];
}
for (let word of text.trim().split(" ")) {
newText.push(this.useAliases(word, wordAliases, files, true));
}
newText = newText.length == 1 ? newText[0] : newText.join(" ");
newText = newText.replace(/ ([\n\t\r]) /g, "$1");
newText = this.useAliases(newText, multiAliases, files, false);
return {text:newText, files};
}
useAliases (string, aliases, files, singleword) {
for (let word in aliases) {
let aliasdata = aliases[word];
let escpAlias = aliasdata.regex ? word : BDFDB.regEscape(word);
let result = true, replaced = false, tempstring1 = string, tempstring2 = "";
let regstring = aliasdata.exact ? "^" + escpAlias + "$" : escpAlias;
while (result != null) {
result = new RegExp(regstring, (aliasdata.case ? "" : "i") + (aliasdata.exact ? "" : "g")).exec(tempstring1);
if (result) {
replaced = true;
let replace = aliasdata.file ? "" : BDFDB.insertNRST(aliasdata.replace);
if (result.length > 1) for (var i = 1; i < result.length; i++) replace = replace.replace(new RegExp("\\\\" + i + "|\\$" + i, "g"), result[i]);
tempstring2 += tempstring1.slice(0, result.index + result[0].length).replace(result[0], replace);
tempstring1 = tempstring1.slice(result.index + result[0].length);
if (aliasdata.file && typeof aliasdata.filedata == "string") {
var filedata = JSON.parse(aliasdata.filedata);
files.push(new File([Uint8Array.from(atob(filedata.data), c => c.charCodeAt(0))], filedata.name, {type:filedata.type}));
}
if (aliasdata.regex && regstring.indexOf("^") == 0) result = null;
}
if (!result) tempstring2 += tempstring1;
}
if (replaced) {
string = tempstring2;
if (singleword) break;
}
}
return string;
}
openAddModal (wordvalue) {
let chataliasesAddModal = BDFDB.htmlToElement(this.chataliasesAddModalMarkup);
let wordvalueinput = chataliasesAddModal.querySelector("#input-wordvalue");
let replacevalueinput = chataliasesAddModal.querySelector("#input-replacevalue");
let addbutton = chataliasesAddModal.querySelector(".btn-add");
wordvalueinput.value = wordvalue || "";
BDFDB.appendModal(chataliasesAddModal);
let checkInputs = () => {
let validinputs = [wordvalueinput, replacevalueinput];
let invalidinputs = [];
let type = "";
if (!wordvalueinput.value.trim()) {
BDFDB.removeFromArray(validinputs, wordvalueinput);
invalidinputs.push(wordvalueinput);
type += "Wordvalue";
}
if (!replacevalueinput.value.trim()) {
BDFDB.removeFromArray(validinputs, replacevalueinput);
invalidinputs.push(replacevalueinput);
type += ((type ? " and " : "") + "Replacevalue");
}
if (type) addDisabledTooltip(invalidinputs, type);
else {
addbutton.disabled = false;
addbutton.style.removeProperty("pointer-events");
BDFDB.removeEles(".chataliases-disabled-tooltip");
}
BDFDB.removeClass(validinputs, "invalid");
};
let addDisabledTooltip = (invalidinputs, type) => {
BDFDB.removeEles(".chataliases-disabled-tooltip");
addbutton.disabled = true;
BDFDB.addClass(invalidinputs, "invalid");
addbutton.style.setProperty("pointer-events", "none", "important");
BDFDB.createTooltip("Choose a " + type, addbutton, {type: "right", color: "red", selector: "chataliases-disabled-tooltip"});
};
wordvalueinput.addEventListener("input", checkInputs);
replacevalueinput.addEventListener("input", checkInputs);
BDFDB.addChildEventListener(chataliasesAddModal, "click", BDFDB.dotCNC.backdrop + BDFDB.dotCNC.modalclose + ".btn-add", () => {
BDFDB.removeEles(".chataliases-disabled-tooltip");
});
addbutton.addEventListener("click", e => {
let configs = {};
for (let key in this.defaults.configs) {
let configinput = chataliasesAddModal.querySelector("#input-config" + key);
if (configinput) configs[key] = configinput.checked;
}
this.saveWord(wordvalueinput.value.trim(), replacevalueinput.value.trim(), chataliasesAddModal.querySelector("#input-file"), configs);
BDFDB.saveAllData(this.aliases, this, "words");
});
wordvalueinput.focus();
setTimeout(checkInputs, 500);
}
}