Fix username chat color if null

This commit is contained in:
Thomas Forgione 2020-10-16 11:13:26 +02:00
parent 62e77f0354
commit 7246ac6911
2 changed files with 32 additions and 1 deletions

View File

@ -22,6 +22,9 @@ client.connect();
// Called every time a message comes in
function onMessageHandler (target, context, message, self) {
console.log({
target, context, message
});
io.emit("message", {
target,
context,

View File

@ -24,7 +24,7 @@ class Message {
let name = document.createElement('span');
name.className = "text name";
name.style.color = readableColor(this.context.color || "#ff0000");
name.style.color = readableColor(this.context.color || findColor(this.context["display-name"]));
name.innerHTML = escapeHtml(this.context["display-name"]);
badges.appendChild(name);
@ -155,6 +155,34 @@ function readableColor(color) {
}
}
function findColor(_username) {
const colors = [
"#ff0000",
"#0000ff",
"#008000",
"#b22222",
"#ff7f50",
"#99cd32",
"#ff4400",
"#2e8b56",
"#daa520",
"#d2691e",
"#5f9ea0",
"#1e8fff",
"#ff69b4",
"#892be2",
"#00ff80"
];
let username = _username.toLowerCase();
let c = 0;
for (let i = 0; i < username.length; i++) {
c += username.charCodeAt(i);
}
return colors[c % colors.length];
}
function replaceAll(input, from, to) {
const fromLen = from.length;
let output = "";