-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchat.js
59 lines (50 loc) · 1.95 KB
/
chat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
document.addEventListener('onLoad', function (obj) {
// obj will be empty for chat widget
// this will fire only once when the widget loads
});
let anim_types = [
"25s", // small messages under 100
"20s", // medium messages under 150
"15s" // over 150
];
let hardCapSize = 300; // DEFAULT and recommended! (this is tested and works!)
document.addEventListener('onEventReceived', function (obj) {
// obj will contain information about the event
console.trace(obj.detail.command?.toUpperCase())
if (obj?.detail?.command?.toUpperCase() !== "PRIVMSG") return;
console.log(obj);
// log the code
console.log(`%cOK ${obj.detail.body} -> ${obj.detail.messageId}`, 'color:blue;');
// get message ID
const mid = obj.detail.messageId;
// get message itself
const bod = obj.detail.body;
// get message length for future use
const mlen = bod.length;
// get message element
const el = document.querySelector(`[data-id="${mid}"]`);
// cap message so that it doesn't mess with the animation
if (bod.length > hardCapSize) {
el.remove();
// log it for debugging
return console.log(`%cMAX LENGTH FOR ${mid} EXCEEDED: ${hardCapSize}`, 'color:red;border:2px dotted red;padding:5px;');
}
// default speed
let speed = anim_types[0];
// speed calculations
if (mlen < 100) {
speed = anim_types[0];
} else if (mlen < 150) {
speed = anim_types[1];
} else if (mlen <= 300) {
speed = anim_types[2];
}
// set element animations etc...
el.style.animation = `LRMove ${speed} linear, RLMove ${speed} linear ${speed} forwards`;
el.style.WebkitAnimation = `LRMove ${speed} linear, RLMove ${speed} linear ${speed} forwards`;
el.style.position = 'absolute';
el.style.right = '0px';
el.style.width = window.innerWidth
el.style.color = obj.detail?.tags?.color;
el.style.bottom = Math.floor(Math.random() * window.innerHeight);
});