BetterDiscordAddons/Plugins/CharCounter/CharCounter.plugin.js

191 lines
6.3 KiB
JavaScript
Raw Normal View History

2019-09-20 22:32:52 +02: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-10-30 14:57:15 +01:00
getVersion () {return "1.3.7";}
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
2019-09-04 12:34:02 +02:00
constructor () {
2019-02-01 10:24:14 +01:00
this.changelog = {
2019-10-30 14:57:15 +01:00
"improved":[["New Library Structure & React","Restructured my Library and switched to React rendering instead of DOM manipulation"]]
2019-02-01 10:24:14 +01:00
};
2019-09-04 12:34:02 +02:00
2018-12-27 08:47:43 +01:00
this.patchModules = {
2019-10-30 14:57:15 +01:00
"ChannelTextArea":"render",
"Note":"render",
"ChangeNickname":"render"
};
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 () {
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.dotCNS.typing + BDFDB.dotCN.cooldownwrapper} {
margin-right: 64px;
}
.charcounter-added {
position: relative !important;
}
2019-10-30 14:57:15 +01:00
.charcounter {
2018-10-11 10:21:26 +02:00
display: block;
position: absolute;
2019-09-04 12:34:02 +02:00
z-index: 1000;
2018-10-11 10:21:26 +02:00
pointer-events: none;
2019-05-29 18:32:10 +02:00
font-size: 15px;
2018-10-11 10:21:26 +02:00
}
2019-10-30 14:57:15 +01:00
.charcounter.normal {
right: 0;
2018-10-11 10:21:26 +02:00
bottom: -1.3em;
}
2019-10-30 14:57:15 +01:00
.charcounter.edit {
2018-10-11 10:21:26 +02:00
left: 0;
bottom: -1.3em;
}
2019-10-30 14:57:15 +01:00
.charcounter.form {
right: 0;
2018-10-11 10:21:26 +02:00
bottom: -1.0em;
}
2019-10-30 14:57:15 +01:00
.charcounter.nickname {
right: 0 !important;
top: 0 !important;
}
2019-10-30 14:57:15 +01:00
.charcounter.popout {
right: 3px !important;
2019-10-30 14:57:15 +01:00
bottom: -8px !important;
font-size: 10px !important;
}
2019-10-30 14:57:15 +01:00
.charcounter.profile {
right: 0 !important;
bottom: -10px !important;
font-size: 12px !important;
}
2019-10-30 14:57:15 +01:00
${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-05-26 13:55:26 +02:00
var libraryScript = document.querySelector('head script#BDFDBLibraryScript');
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
2018-10-11 10:21:26 +02:00
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
2019-05-26 13:55:26 +02:00
libraryScript.setAttribute("id", "BDFDBLibraryScript");
2018-10-11 10:21:26 +02: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-01-17 23:48:29 +01:00
libraryScript.setAttribute("date", performance.now());
2019-05-26 13:55:26 +02:00
libraryScript.addEventListener("load", () => {this.initialize();});
2018-10-11 10:21:26 +02:00
document.head.appendChild(libraryScript);
2019-05-26 13:55:26 +02:00
}
else if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
2019-11-01 10:27:07 +01:00
this.startTimeout = setTimeout(() => {
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 () {
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;
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.init(this);
2019-01-26 22:45:19 +01:00
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 () {
2019-01-17 23:48:29 +01:00
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-10-22 11:37:23 +02:00
this.stopping = true;
2019-10-30 14:57:15 +01:00
BDFDB.ModuleUtils.forceAllUpdates(this);
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-10-30 14:57:15 +01:00
processChannelTextArea (e) {
if (!this.stopping && e.instance.props && e.instance.props.type && this.maxLenghts[e.instance.props.type]) {
let [children, index] = BDFDB.ReactUtils.findChildren(e.returnvalue, {name: "TextAreaAutosize"});
if (index > -1) this.injectCounter(e.returnvalue, children, e.instance.props.type, BDFDB.dotCN.textarea, true);
}
}
2019-01-26 22:45:19 +01:00
2019-10-30 14:57:15 +01:00
processNote (e) {
if (!this.stopping) {
let [children, index] = BDFDB.ReactUtils.findChildren(e.returnvalue, {name: "TextAreaAutosize"});
if (index > -1) this.injectCounter(e.returnvalue, children, e.instance.props.className && e.instance.props.className.indexOf(BDFDB.disCN.usernotepopout) > -1 ? "popout" : "profile", "textarea");
}
2019-09-11 12:14:43 +02:00
}
2019-10-30 14:57:15 +01:00
processChangeNickname (e) {
if (!this.stopping) {
let [children, index] = BDFDB.ReactUtils.findChildren(e.returnvalue, {name: "TextInput"});
if (index > -1) this.injectCounter(e.returnvalue, children, "nickname", BDFDB.dotCN.input);
}
2019-09-11 12:14:43 +02:00
}
2019-10-30 14:57:15 +01:00
injectCounter (parent, children, type, refClass, parsing) {
if (!children) return;
parent.props.className += " charcounter-added";
children.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CharCounter, {
className: `charcounter ${type}`,
refClass: refClass,
parsing: parsing,
max: this.maxLenghts[type]
}));
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-10-30 14:57:15 +01:00
BDFDB.DOMUtils.remove(input.parentElement.querySelectorAll(".charcounter"));
2019-10-23 11:10:01 +02:00
var counter = BDFDB.DOMUtils.create(`<div id="charcounter" class="charcounter ${type}"></div>`);
2019-01-17 23:48:29 +01:00
input.parentElement.appendChild(counter);
2019-01-26 22:45:19 +01:00
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.addClass(input.parentElement.parentElement, "charcounter-added");
if (type == "nickname") input.setAttribute("maxlength", 32);
2019-10-22 18:55:25 +02:00
BDFDB.ListenerUtils.add(this, input, "keydown click change", e => {
2019-11-01 11:09:32 +01:00
BDFDB.TimeUtils.clear(input.charcountertimeout);
input.charcountertimeout = BDFDB.TimeUtils.timeout(() => {updateCounter();},100);
2019-01-17 23:48:29 +01:00
});
2019-10-22 18:55:25 +02:00
BDFDB.ListenerUtils.add(this, input, "mousedown", e => {
BDFDB.ListenerUtils.add(this, document, "mouseup", () => {
BDFDB.ListenerUtils.remove(this, document);
2019-11-01 11:09:32 +01:00
if (this.props.end - input.selectionStart) BDFDB.TimeUtils.timeout(() => {BDFDB.ListenerUtils.add(this, document, "click", () => {
2019-10-22 18:55:25 +02:00
var contexttype = BDFDB.ReactUtils.getValue(document.querySelector(BDFDB.dotCN.contextmenu), "return.stateNode.props.type");
2019-05-24 11:46:22 +02:00
if (!contexttype || !contexttype.startsWith("CHANNEL_TEXT_AREA")) {
input.selectionStart = 0;
2019-10-30 14:57:15 +01:00
this.props.end = 0;
2019-05-24 11:46:22 +02:00
updateCounter();
}
2019-11-01 11:09:32 +01:00
else BDFDB.TimeUtils.timeout(() => {updateCounter();},100);
2019-10-22 18:55:25 +02:00
BDFDB.ListenerUtils.remove(this, document);
2019-01-17 23:48:29 +01:00
});});
});
2019-11-01 11:09:32 +01:00
BDFDB.ListenerUtils.add(this, document, "mousemove", () => {BDFDB.TimeUtils.timeout(() => {updateCounter();},10);});
2019-01-17 23:48:29 +01:00
});
2019-01-26 22:45:19 +01:00
updateCounter();
}
2018-10-11 10:21:26 +02:00
}