(()=>{ url_domain =(data)=> { let a = document.createElement('a'); a.href = data; return a.hostname; }; for (let script of document.getElementsByTagName('script')) { if (script.src.indexOf('messenger')>=0) { let url = script.src; window.messangerSrc = url_domain(url); break; } } })(); class ChatMessenger { asyncLoadScripts (origin) { this.loadCSS(`${origin}/messenger/css/messenger.css?cache=` + Date.now()); this.loadCSS(`//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css`); console.log('origin', `${origin}js/mediasoup3/easy-mediasoup.bundle.min.js`); let files = [ `//cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.slim.js`, '//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js', '//cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js', `${origin}js/mediasoup3/easy-mediasoup.bundle.min.js`, `${origin}js/mediasoup3/mediasSoup3.js`, `https://cdn.jsdelivr.net/npm/lsx-emojipicker@1.1.2/jquery.lsxemojipicker.min.js`, `https://cdn.jsdelivr.net/npm/hark@1.2.3/hark.bundle.js` ]; if(!window.jQuery) { files.unshift('//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js'); } let checkStateAndCall = (path, callback) => { let _success = false; return function () { if (!_success && (!this.readyState || (this.readyState === 'complete'))) { _success = true; callback(); } }; }; let loadNext = () => { // chain element if (!files.length) { this.init(); return; } let path = files.shift(); let scriptElm = document.createElement('script'); scriptElm.type = 'text/javascript'; scriptElm.async = true; scriptElm.src = path; scriptElm.onload = scriptElm.onreadystatechange = checkStateAndCall(path, loadNext); // load next file in chain when let headElm = document.head || document.getElementsByTagName('head')[0]; headElm.appendChild(scriptElm); }; loadNext(); // start a chain }; ago(date) { let plurial; let seconds = Math.floor((new Date() - date) / 1000); let interval = Math.floor(seconds / 31536000); if (interval > 0) { if (interval>1) { return eval(this.traductions.yearsAgo); } else { return eval(this.traductions.yearAgo); } } interval = Math.floor(seconds / 2592000); if (interval > 0) { if (interval>1) { return eval(this.traductions.monthsAgo); } else { return eval(this.traductions.monthAgo); } } interval = Math.floor(seconds / 86400); if (interval > 0) { if (interval>1) { return eval(this.traductions.daysAgo); } else { return eval(this.traductions.dayAgo); } } interval = Math.floor(seconds / 3600); if (interval > 0) { if (interval>1) { return eval(this.traductions.hoursAgo); } else { return eval(this.traductions.hourAgo); } } interval = Math.floor(seconds / 60); if (interval > 0) { if (interval>1) { return eval(this.traductions.minutesAgo); } else { return eval(this.traductions.minuteAgo); } } return 'now'; }; isEmpty (obj){ return Object.keys(obj).length === 0 && obj.constructor === Object; }; getDateAgo (date) { let res = new Date(date).getTime(); return ago(res); }; loadCSS (src) { let head = document.getElementsByTagName('HEAD')[0]; let link = document.createElement('link'); link.rel = 'stylesheet'; link.type = 'text/css'; link.href = src; head.appendChild(link); }; async getWebmasterid (url) { let res = await $.ajax({ url: this.ajax, type: 'POST', data: {a:'getWebmasterid', url:url} }); return parseInt(res); }; constructor(myUser = {}, notification = {}, callBackAddUser = false, callBackRemoveUser = false, traductions={}) { this.traductions = { yourMessage: "Your message", minuteAgo: "`${interval} min ago.`", minutesAgo: "`${interval} mins ago.`", hourAgo: "`${interval} hour ago.`", hoursAgo: "`${interval} hours ago.`", dayAgo: "`${interval} day ago.`", daysAgo: "`${interval} days ago.`", monthAgo: "`${interval} month ago.`", monthsAgo: "`${interval} months ago.`", yearAgo: "`${interval} year ago.`", yearsAgo: "`${interval} years ago.`", }; this.traductions = { ...traductions, ...this.traductions}; if (!window.messangerSrc) { window.messangerSrc = 'html5-chat.com'; } let origin = `//${window.messangerSrc}/`; if (myUser.ajax) { this.ajax = myUser.ajax; } else { this.ajax = origin + 'ajax.php'; } if (!myUser.el) { myUser.el = 'button[data-userid], div[data-userid]'; } if (!myUser.imagePath) { myUser.imagePath = ''; } this.myUser = myUser; this.users = new Map(); this.mutedUsers = new Map(); this.container = 'messengerContainer'; this.notification = notification; this.callBackAddUser = callBackAddUser; this.callBackRemoveUser = callBackRemoveUser; this.socket = {}; this.asyncLoadScripts(origin); }; pickRandomAvatar () { let rnd = Math.round(Math.random()*10) + 1; return `https://html5-chat.com/img/avatars/${rnd}.svg`; }; async getConfig (webmasterid) { let res = await $.ajax({ url: this.ajax, type: 'POST', data: {a:'getConfig', webmasterid:webmasterid} }); return JSON.parse(res); }; async getDefaultRoom (webmasterid) { let res = await $.ajax({ url: this.ajax, type: 'POST', data: {a:'getDefaultRoom', webmasterid:webmasterid} }); return JSON.parse(res); }; getMuted () { $.post(this.ajax, {a:'getMutedUsersAsArray', webmasterid:this.myUser.webmasterid, userid:this.myUser.id}, (jsonMuted)=> { if (jsonMuted) { jsonMuted = JSON.parse(jsonMuted); jsonMuted.forEach((muted) => { this.mutedUsers.set(muted.muteduserid, muted); let temp = `#messengerNotificatorContent div.messengerNotificatorUserItem[data-userid=${muted.muteduserid}]`; $(temp).addClass('muted'); }); } this.restoreWindows(); }); }; getUsersWhoSentMeMessages () { this.socket.emit('getUsersWhoSentMeMessages', (messages)=>{ if (!messages) return; messages = JSON.parse(messages); messages.forEach((message) => { let extra = JSON.parse(message.extras); let id = parseInt(message.fromid); let status = (this.users.get(id))?'messengerOnline':'messengerOffline'; let user = {id: id, username:extra.from, avatar:extra.avatar, status:status}; this.addUser(user); }); }); }; async promptForUsername (){ return new Promise((resolve)=>{ let template = `