BetterDiscordAddons/Plugins/CharCounter/CharCounter.plugin.js

178 lines
6.1 KiB
JavaScript
Raw Normal View History

2019-01-31 17:06:48 +01:00
//META{"name":"CharCounter","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/CharCounter","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/CharCounter/CharCounter.plugin.js"}*//
2018-10-11 10:21:26 +02:00
class CharCounter {
2019-01-17 23:48:29 +01:00
getName () {return "CharCounter";}
2019-02-01 10:24:14 +01:00
getVersion () {return "1.3.3";}
2019-01-17 23:48:29 +01:00
getAuthor () {return "DevilBro";}
getDescription () {return "Adds a charcounter in the chat.";}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
initConstructor () {
2019-02-01 10:24:14 +01:00
this.changelog = {
"fixed":[["Parsed Length","The character counter show the parsed length (which determines if a message is over 2000 chars) again like it used to, instead of the literal length"]]
};
2018-12-27 08:47:43 +01:00
this.patchModules = {
"ChannelTextArea":"componentDidMount",
"Note":"componentDidMount",
"Modal":"componentDidMount"
};
2019-01-26 22:45:19 +01:00
this.maxLenghts = {
normal: 2000,
edit: 2000,
form: 2000,
nickname: 32,
popout: 256,
profile: 256
}
2018-10-11 10:21:26 +02:00
this.css = `
${BDFDB.dotCN.themelight} #charcounter {
color: #747f8d;
opacity: .7;
}
${BDFDB.dotCN.themedark} #charcounter {
color: #ccc;
opacity: .5;
}
${BDFDB.dotCNS.typing + BDFDB.dotCN.cooldownwrapper} {
margin-right: 64px;
}
.charcounter-added {
position: relative !important;
}
2018-10-11 10:21:26 +02:00
#charcounter {
display: block;
position: absolute;
z-index: 1000;
pointer-events: none;
}
#charcounter.normal {
right: 0;
2018-10-11 10:21:26 +02:00
bottom: -1.3em;
}
#charcounter.edit {
left: 0;
bottom: -1.3em;
}
#charcounter.form {
right: 0;
2018-10-11 10:21:26 +02:00
bottom: -1.0em;
}
#charcounter.nickname {
right: 0 !important;
top: 0 !important;
}
#charcounter.popout {
right: 3px !important;
bottom: 1px !important;
font-size: 10px !important;
}
#charcounter.profile {
right: -5px !important;
bottom: 3px !important;
font-size: 12px !important;
}
${BDFDB.dotCN.usernote} textarea:not(:focus) + #charcounter {
display: none;
2018-10-11 10:21:26 +02:00
}`;
}
//legacy
load () {}
start () {
2019-02-04 09:13:15 +01:00
if (!global.BDFDB) global.BDFDB = {myPlugins:{}};
if (global.BDFDB && global.BDFDB.myPlugins && typeof global.BDFDB.myPlugins == "object") global.BDFDB.myPlugins[this.getName()] = this;
2019-01-17 23:48:29 +01:00
var libraryScript = document.querySelector('head script[src="https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"]');
if (!libraryScript || performance.now() - libraryScript.getAttribute("date") > 600000) {
2018-10-11 10:21:26 +02:00
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js");
2019-01-17 23:48:29 +01:00
libraryScript.setAttribute("date", performance.now());
2019-01-30 21:23:49 +01:00
libraryScript.addEventListener("load", () => {if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();});
2018-10-11 10:21:26 +02:00
document.head.appendChild(libraryScript);
}
2019-01-17 23:48:29 +01:00
else if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
2018-10-11 10:21:26 +02:00
this.startTimeout = setTimeout(() => {this.initialize();}, 30000);
}
initialize () {
2019-01-17 23:48:29 +01:00
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-01-22 11:05:54 +01:00
if (this.started) return;
2018-10-11 10:21:26 +02:00
BDFDB.loadMessage(this);
2019-01-26 22:45:19 +01:00
2018-12-27 08:47:43 +01:00
BDFDB.WebModules.forceAllUpdates(this);
2018-10-11 10:21:26 +02:00
}
else {
2019-02-12 21:56:34 +01:00
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 () {
2019-01-17 23:48:29 +01:00
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2018-12-27 10:52:37 +01:00
BDFDB.removeEles(".charcounter");
BDFDB.removeClasses("charcounter-added");
2018-10-11 10:21:26 +02:00
BDFDB.unloadMessage(this);
}
}
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
processChannelTextArea (instance, wrapper) {
2019-02-01 10:24:14 +01:00
if (instance.props && instance.props.type && this.maxLenghts[instance.props.type]) this.appendCounter(wrapper.querySelector("textarea"), instance.props.type, true);
}
2019-01-26 22:45:19 +01:00
processNote (instance, wrapper) {
2019-02-01 10:24:14 +01:00
this.appendCounter(wrapper.firstElementChild, BDFDB.containsClass(wrapper, BDFDB.disCN.usernotepopout) ? "popout" : (BDFDB.containsClass(wrapper, BDFDB.disCN.usernoteprofile) ? "profile" : null), false);
}
2019-01-26 22:45:19 +01:00
processModal (instance, wrapper) {
if (instance.props && instance.props.tag == "form") {
let reset = wrapper.querySelector(BDFDB.dotCN.reset);
2019-02-01 10:24:14 +01:00
if (reset && BDFDB.getInnerText(reset.firstElementChild) == BDFDB.LanguageStrings.RESET_NICKNAME) this.appendCounter(wrapper.querySelector(BDFDB.dotCN.inputdefault), "nickname", false);
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2019-02-01 10:24:14 +01:00
appendCounter (input, type, parsing) {
if (!input || !type) return;
2019-01-21 16:34:16 +01:00
BDFDB.removeEles(input.parentElement.querySelectorAll("#charcounter"));
2019-01-17 23:48:29 +01:00
var counter = BDFDB.htmlToElement(`<div id="charcounter" class="charcounter ${type}"></div>`);
input.parentElement.appendChild(counter);
2019-01-26 22:45:19 +01:00
2019-02-01 10:24:14 +01:00
var updateCounter = () => {
var inputlength = parsing ? BDFDB.getParsedLength(input.value) : input.value.length;
var seleclength = input.selectionEnd - input.selectionStart == 0 ? 0 : (parsing ? BDFDB.getParsedLength(input.value.slice(input.selectionStart, input.selectionEnd)) : (input.selectionEnd - input.selectionStart));
seleclength = !seleclength ? 0 : (seleclength > inputlength ? inputlength - (inputlength - input.selectionEnd - input.selectionStart) : seleclength);
counter.innerText = inputlength + "/" + (this.maxLenghts[type] || 2000) + (!seleclength ? "" : " (" + seleclength + ")");
};
2019-01-26 22:45:19 +01:00
2019-01-17 23:48:29 +01:00
BDFDB.addClass(input.parentElement.parentElement, "charcounter-added");
if (type == "nickname") input.setAttribute("maxlength", 32);
2019-01-17 23:48:29 +01:00
BDFDB.addEventListener(this, input, "keydown click", e => {
clearTimeout(input.charcountertimeout);
input.charcountertimeout = setTimeout(() => {updateCounter();},100);
});
BDFDB.addEventListener(this, input, "mousedown", e => {
BDFDB.addEventListener(this, document, "mouseup", () => {
BDFDB.removeEventListener(this, document);
if (input.selectionEnd - input.selectionStart) setImmediate(() => {BDFDB.addEventListener(this, document, "click", () => {
input.selectionStart = 0;
input.selectionEnd = 0;
updateCounter();
BDFDB.removeEventListener(this, document);
});});
});
2019-01-17 23:48:29 +01:00
BDFDB.addEventListener(this, document, "mousemove", () => {setTimeout(() => {updateCounter();},10);});
});
2019-01-26 22:45:19 +01:00
updateCounter();
}
2018-10-11 10:21:26 +02:00
}