Mercurial Hosting > chat
comparison src/chat.js @ 40:7ea33179592a
email notification
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Thu, 27 Feb 2025 16:44:20 -0700 |
| parents | 471b13e6ce2c |
| children | e09747b199e9 |
comparison
equal
deleted
inserted
replaced
| 39:471b13e6ce2c | 40:7ea33179592a |
|---|---|
| 2 | 2 |
| 3 let title = document.title; | 3 let title = document.title; |
| 4 let currentChatId = null; | 4 let currentChatId = null; |
| 5 let eventSource; | 5 let eventSource; |
| 6 let lastUpdate; | 6 let lastUpdate; |
| 7 let hasUnseen = false; | |
| 8 let userId; | 7 let userId; |
| 9 | 8 |
| 10 function evalEvent(event) { | 9 function evalEvent(event) { |
| 11 // console.log(event); | 10 // console.log(event); |
| 12 eval(event.data); | 11 eval(event.data); |
| 151 function added(html) { | 150 function added(html) { |
| 152 let input = document.querySelector('div[input]'); | 151 let input = document.querySelector('div[input]'); |
| 153 input.insertAdjacentHTML('beforebegin',html); | 152 input.insertAdjacentHTML('beforebegin',html); |
| 154 fixPosts(); | 153 fixPosts(); |
| 155 input.scrollIntoView({block: 'end'}); | 154 input.scrollIntoView({block: 'end'}); |
| 155 if( document.hasFocus() ) | |
| 156 ajax('active.js'); | |
| 156 } | 157 } |
| 157 | 158 |
| 158 function getChats(chatId,updated) { | 159 function getChats(chatId,updated) { |
| 159 let first = document.querySelector('div[chat]'); | 160 let first = document.querySelector('div[chat]'); |
| 160 if( !first || first.getAttribute('chat') != chatId ) { | 161 if( !first || first.getAttribute('chat') != chatId ) { |
| 161 // console.log('getChats'); | 162 // console.log('getChats'); |
| 162 ajax('get_chats.js'); | 163 ajax('get_chats.js'); |
| 163 } | 164 } |
| 164 if( updated ) | 165 if( updated ) |
| 165 lastUpdate = updated; | 166 lastUpdate = updated; |
| 166 if( !document.hasFocus() && !hasUnseen ) { | 167 if( !document.hasFocus() ) { |
| 167 document.title = title + ' *'; | 168 document.title = title + ' *'; |
| 168 notify(); | |
| 169 hasUnseen = true; | |
| 170 } | 169 } |
| 171 } | 170 } |
| 172 | 171 |
| 173 function gotChats(html) { | 172 function gotChats(html) { |
| 174 document.querySelector('div[chats]').innerHTML = html; | 173 document.querySelector('div[chats]').innerHTML = html; |
| 185 } | 184 } |
| 186 | 185 |
| 187 window.onfocus = function() { | 186 window.onfocus = function() { |
| 188 // console.log('onfocus'); | 187 // console.log('onfocus'); |
| 189 document.title = title; | 188 document.title = title; |
| 190 hasUnseen = false; | 189 ajax('active.js'); |
| 191 }; | 190 }; |
| 192 | 191 |
| 193 let urlRegex = /(^|\s)(https?:\/\/\S+)/g; | 192 let urlRegex = /(^|\s)(https?:\/\/\S+)/g; |
| 194 | 193 |
| 195 function urlsToLinks(text) { | 194 function urlsToLinks(text) { |
| 196 return text.replace( urlRegex, '$1<a href="$2">$2</a>' ); | 195 return text.replace( urlRegex, '$1<a target="_blank" href="$2">$2</a>' ); |
| 197 } | 196 } |
| 198 | 197 |
| 199 let currentPulldown = null; | 198 let currentPulldown = null; |
| 200 let newPulldown = null; | 199 let newPulldown = null; |
| 201 | 200 |
| 222 setInterval(function(){ | 221 setInterval(function(){ |
| 223 showOnline(); | 222 showOnline(); |
| 224 ajax(`heartbeat.js?last_update=${lastUpdate}`); | 223 ajax(`heartbeat.js?last_update=${lastUpdate}`); |
| 225 }, 10000 ); | 224 }, 10000 ); |
| 226 | 225 |
| 227 let sound = new Audio('/images/notify.mp3'); | |
| 228 function notify() { | |
| 229 sound.play(); | |
| 230 } | |
| 231 | |
| 232 let online = {}; | 226 let online = {}; |
| 233 | 227 |
| 234 function setOnline(userId) { | 228 function setOnline(userId) { |
| 235 online[userId] = Date.now(); | 229 online[userId] = Date.now(); |
| 236 } | 230 } |
