Fix username chat color if null
This commit is contained in:
parent
62e77f0354
commit
7246ac6911
3
index.js
3
index.js
|
@ -22,6 +22,9 @@ client.connect();
|
||||||
|
|
||||||
// Called every time a message comes in
|
// Called every time a message comes in
|
||||||
function onMessageHandler (target, context, message, self) {
|
function onMessageHandler (target, context, message, self) {
|
||||||
|
console.log({
|
||||||
|
target, context, message
|
||||||
|
});
|
||||||
io.emit("message", {
|
io.emit("message", {
|
||||||
target,
|
target,
|
||||||
context,
|
context,
|
||||||
|
|
|
@ -24,7 +24,7 @@ class Message {
|
||||||
|
|
||||||
let name = document.createElement('span');
|
let name = document.createElement('span');
|
||||||
name.className = "text name";
|
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"]);
|
name.innerHTML = escapeHtml(this.context["display-name"]);
|
||||||
badges.appendChild(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) {
|
function replaceAll(input, from, to) {
|
||||||
const fromLen = from.length;
|
const fromLen = from.length;
|
||||||
let output = "";
|
let output = "";
|
||||||
|
|
Loading…
Reference in New Issue