MarseyWorld/files/assets/js/chat.js

187 lines
48 KiB
JavaScript
Raw Normal View History

2022-08-26 16:34:38 +00:00
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).io=e()}(this,(function(){"use strict";function t(e){return t="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(e)}function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(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 r(t,e,r){return e&&n(t.prototype,e),r&&n(t,r),t}function i(){return i=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t},i.apply(this,arguments)}function o(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&a(t,e)}function s(t){return s=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},s(t)}function a(t,e){return a=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},a(t,e)}function c(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}function u(t,e,n){return u=c()?Reflect.construct:function(t,e,n){var r=[null];r.push.apply(r,e);var i=new(Function.bind.apply(t,r));return n&&a(i,n.prototype),i},u.apply(null,arguments)}function h(t){var e="function"==typeof Map?new Map:void 0;return h=function(t){if(null===t||(n=t,-1===Function.toString.call(n).indexOf("[native code]")))return t;var n;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,r)}function r(){return u(t,arguments,s(this).constructor)}return r.prototype=Object.create(t.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),a(r,t)},h(t)}function f(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function l(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return f(t)}function p(t){var e=c();return function(){var n,r=s(t);if(e){var i=s(this).constructor;n=Reflect.construct(r,arguments,i)}else n=r.apply(this,arguments);return l(this,n)}}function d(t,e,n){return d="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(t,e,n){var r=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=s(t)););return t}(t,e);if(r){var i=Object.getOwnPropertyDescriptor(r,e);return i.get?i.get.call(n):i.value}},d(t,e,n||t)}function y(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function v(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return y(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?y(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var r=0,i=function(){};return{s:i,n:function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,s=!0,a=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){
2022-07-16 21:00:02 +00:00
2022-08-26 16:11:01 +00:00
const socket=io({transports: ["websocket"]})
2022-07-16 21:00:02 +00:00
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=3009`)
2022-07-16 21:00:02 +00:00
alert=true;
}
setTimeout(flash, 500)
}
else {
icon.href = escapeHTML(`/assets/images/${site_name}/icon.webp?v=3009`)
2022-07-16 21:00:02 +00:00
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');
};
2022-08-15 05:19:41 +00:00
notifs = notifs + 1;
if (notifs == 1) {
setTimeout(flash, 500);
}
2022-07-16 21:00:02 +00:00
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')
2022-08-25 23:31:31 +00:00
document.getElementsByClassName('time')[0].classList.add('d-none')
2022-07-16 21:00:02 +00:00
}
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']
2022-08-25 23:24:29 +00:00
document.getElementsByClassName('userlink')[0].style.color = '#' + json['namecolor']
2022-08-25 23:30:05 +00:00
document.getElementsByClassName('time')[0].classList.remove('d-none')
2022-07-16 21:00:02 +00:00
}
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
2022-08-25 23:34:20 +00:00
try {username = t.parentElement.previousElementSibling.previousElementSibling.innerHTML}
2022-08-26 14:53:56 +00:00
catch(e) {username = t.parentElement.previousElementSibling.innerHTML}
2022-07-16 21:00:02 +00:00
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-14 13:38:08 +00:00
let online = ''
2022-08-13 11:07:57 +00:00
let online2 = '<b>Users in chat 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:36 +00:00
online += `<li><a href="/@${u}" class="text-lg">@${u}</a></li>`
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-08-14 03:51:25 +00:00
document.getElementById('online3').innerHTML = online
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');
}
})
2022-08-26 17:57:31 +00:00
function scroll_chat() {
2022-08-26 18:29:05 +00:00
setTimeout(function () {
box.scrollTo(0, box.scrollHeight)
2022-08-26 18:32:25 +00:00
}, 0200);
2022-08-26 17:57:31 +00:00
}
2022-08-26 18:29:05 +00:00
scroll_chat()
box.scrollTo(0, box.scrollHeight)