forked from rDrama/rDrama
1
0
Fork 0
rDrama/files/assets/js/chat.js

171 lines
68 KiB
JavaScript
Raw Normal View History

2022-07-16 21:00:02 +00:00
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.io=e():t.io=e()}(self,(function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=18)}([function(t,e,n){var r=n(24),o=n(25),i=String.fromCharCode(30);t.exports={protocol:4,encodePacket:r,encodePayload:function(t,e){var n=t.length,o=new Array(n),s=0;t.forEach((function(t,c){r(t,!1,(function(t){o[c]=t,++s===n&&e(o.join(i))}))}))},decodePacket:o,decodePayload:function(t,e){for(var n=t.split(i),r=[],s=0;s<n.length;s++){var c=o(n[s],e);if(r.push(c),"error"===c.type)break}return r}}},function(t,e,n){function r(t){if(t)return function(t){for(var e in r.prototype)t[e]=r.prototype[e];return t}(t)}t.exports=r,r.prototype.on=r.prototype.addEventListener=function(t,e){return this._callbacks=this._callbacks||{},(this._callbacks["$"+t]=this._callbacks["$"+t]||[]).push(e),this},r.prototype.once=function(t,e){function n(){this.off(t,n),e.apply(this,arguments)}return n.fn=e,this.on(t,n),this},r.prototype.off=r.prototype.removeListener=r.prototype.removeAllListeners=r.prototype.removeEventListener=function(t,e){if(this._callbacks=this._callbacks||{},0==arguments.length)return this._callbacks={},this;var n,r=this._callbacks["$"+t];if(!r)return this;if(1==arguments.length)return delete this._callbacks["$"+t],this;for(var o=0;o<r.length;o++)if((n=r[o])===e||n.fn===e){r.splice(o,1);break}return 0===r.length&&delete this._callbacks["$"+t],this},r.prototype.emit=function(t){this._callbacks=this._callbacks||{};for(var e=new Array(arguments.length-1),n=this._callbacks["$"+t],r=1;r<arguments.length;r++)e[r-1]=arguments[r];if(n){r=0;for(var o=(n=n.slice(0)).length;r<o;++r)n[r].apply(this,e)}return this},r.prototype.listeners=function(t){return this._callbacks=this._callbacks||{},this._callbacks["$"+t]||[]},r.prototype.hasListeners=function(t){return!!this.listeners(t).length}},function(t,e){t.exports="undefined"!=typeof self?self:"undefined"!=typeof window?window:Function("return this")()},function(t,e,n){function r(t){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function o(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function i(t,e){return(i=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function s(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}();return function(){var n,r=a(t);if(e){var o=a(this).constructor;n=Reflect.construct(r,arguments,o)}else n=r.apply(this,arguments);return c(this,n)}}function c(t,e){return!e||"object"!==r(e)&&"function"!=typeof e?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):e}function a(t){return(a=Object.setPrototypeOf?Obj
const socket=io()
const chatline = document.getElementsByClassName('chat-line')[0]
const box = document.getElementById('chat-window')
const textbox = document.getElementById('input-text')
const icon = document.getElementById('favicon')
const vid = document.getElementById('vid').value
const site_name = document.getElementById('site_name').value
const slurreplacer = document.getElementById('slurreplacer').value
let notifs = 0;
let focused = true;
let is_typing = false;
let alert=true;
function flash(){
let title = document.getElementsByTagName('title')[0]
if (notifs >= 1 && !focused){
title.innerHTML = `[+${notifs}] Chat`;
if (alert) {
icon.href = escapeHTML(`/assets/images/${site_name}/alert.webp?v=3`)
alert=false;
}
else {
icon.href = escapeHTML(`/assets/images/${site_name}/icon.webp?v=3000`)
alert=true;
}
setTimeout(flash, 500)
}
else {
icon.href = escapeHTML(`/assets/images/${site_name}/icon.webp?v=3000`)
notifs = 0
title.innerHTML = 'Chat';
}
if (is_typing) {
is_typing = false
socket.emit('typing', false);
}
}
socket.on('speak', function(json) {
let text = json['text']
let text_html
if (slurreplacer == 'True') text_html = json['text_censored']
else text_html = json['text_html']
if (text_html.includes(`<a href="/id/${vid}">`)){
chatline.classList.add('chat-mention');
}
else {
chatline.classList.remove('chat-mention');
};
let users = document.getElementsByClassName('userlink');
let last_user = users[users.length-1].innerHTML;
let scrolled_down = (box.scrollHeight - box.scrollTop <= window.innerHeight)
if (last_user == json['username']) {
document.getElementsByClassName('chat-line')[0].classList.remove('diff')
document.getElementsByClassName('userlink')[0].classList.add('d-none')
document.getElementsByClassName('avatar')[0].classList.add('d-none')
}
else {
document.getElementsByClassName('chat-line')[0].classList.add('diff')
document.getElementsByClassName('userlink')[0].classList.remove('d-none')
document.getElementsByClassName('avatar')[0].classList.remove('d-none')
document.getElementsByClassName('avatar')[0].src = json['avatar']
document.getElementsByClassName('userlink')[0].href = '/@' + json['username']
document.getElementsByClassName('userlink')[0].style.color = '#' + json['namecolor']
}
document.getElementsByClassName('userlink')[0].innerHTML = json['username']
document.getElementsByClassName('text')[0].innerHTML = escapeHTML(text)
document.getElementsByClassName('chat-message')[0].innerHTML = text_html.replace(/data-src/g, 'src').replace(/data-cfsrc/g, 'src').replace(/style="display:none;visibility:hidden;"/g, '')
let line = document.getElementsByClassName('chat-line')[0].cloneNode(true)
bs_trigger(line)
box.append(line)
if (scrolled_down) box.scrollTo(0, box.scrollHeight)
})
function send() {
text = textbox.value.trim()
if (text)
{
socket.emit('speak', text);
textbox.value = ''
is_typing = false
socket.emit('typing', false);
}
autoExpand(textbox);
}
function quote(t) {
text = t.previousElementSibling.innerHTML.replace(/&gt;/g, "> ").replace(/\n/g, "\n> ").replace(/\*/g,"\\*")
let username
try {username = t.parentElement.previousElementSibling.innerHTML}
catch(e) {username = t.parentElement.parentElement.previousElementSibling.innerHTML}
textbox.value = '> ' + text + '\n@' + username + ' '
textbox.focus()
autoExpand(textbox);
}
textbox.addEventListener("keyup", function(e) {
if (!e.shiftKey && e.key === 'Enter') {
e.preventDefault();
send()
}
})
socket.on('online', function(data){
document.getElementsByClassName('board-chat-count')[0].innerHTML = data.length
2022-08-13 09:14:21 +00:00
let online = '<h5>Users online right now</h5>'
2022-08-13 09:12:18 +00:00
let online2 = '<b>Users online right now</b>'
2022-07-16 21:00:02 +00:00
for (const u of data)
2022-08-13 07:26:00 +00:00
{
2022-08-13 09:15:02 +00:00
online += `<ol><a href="/@${u}" class="text-lg">@${u}</a></ol>`
2022-08-13 09:11:56 +00:00
online2 += `<br>@${u}`
2022-08-13 07:26:00 +00:00
}
2022-07-16 21:00:02 +00:00
document.getElementById('online').innerHTML = online
2022-08-13 07:23:23 +00:00
document.getElementById('online2').setAttribute("data-bs-original-title", online2);
2022-07-16 21:00:02 +00:00
})
window.addEventListener('blur', function(){
focused=false
})
window.addEventListener('focus', function(){
focused=true
})
textbox.addEventListener("input", function() {
text = textbox.value
if (!text && is_typing==true){
is_typing=false;
socket.emit('typing', false);
}
else if (text && is_typing==false) {
is_typing=true;
socket.emit('typing', true);
}
})
socket.on('typing', function (users){
if (users.length==0){
document.getElementById('typing-indicator').innerHTML = '';
document.getElementById('loading-indicator').classList.add('d-none');
}
else if (users.length==1){
document.getElementById('typing-indicator').innerHTML = '<b>'+users[0]+"</b> is typing...";
document.getElementById('loading-indicator').classList.remove('d-none');
}
else if (users.length==2){
document.getElementById('typing-indicator').innerHTML = '<b>'+users[0]+"</b> and <b>"+users[1]+"</b> are typing...";
document.getElementById('loading-indicator').classList.remove('d-none');
}
else {
document.getElementById('typing-indicator').innerHTML = '<b>'+users[0]+"</b>, <b>"+users[1]+"</b>, and <b>"+users[2]+"</b> are typing...";
document.getElementById('loading-indicator').classList.remove('d-none');
}
})
box.scrollTo(0, box.scrollHeight)