comparison src/chat.js @ 31:a0820965ba04

notify sound
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 09 Nov 2024 21:35:46 -0700
parents f4708943f29e
children 0f40501b0b56
comparison
equal deleted inserted replaced
30:f4708943f29e 31:a0820965ba04
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;
7 8
8 function evalEvent(event) { 9 function evalEvent(event) {
9 // console.log(event); 10 // console.log(event);
10 eval(event.data); 11 eval(event.data);
11 } 12 }
138 function added(html) { 139 function added(html) {
139 let input = document.querySelector('div[input]'); 140 let input = document.querySelector('div[input]');
140 input.insertAdjacentHTML('beforebegin',html); 141 input.insertAdjacentHTML('beforebegin',html);
141 fixPosts(); 142 fixPosts();
142 input.scrollIntoView({block: 'end'}); 143 input.scrollIntoView({block: 'end'});
143 if( !document.hasFocus() )
144 document.title = title + ' *';
145 } 144 }
146 145
147 function getChats(chatId,updated) { 146 function getChats(chatId,updated) {
148 let first = document.querySelector('div[chat]'); 147 let first = document.querySelector('div[chat]');
149 if( !first || first.getAttribute('chat') != chatId ) { 148 if( !first || first.getAttribute('chat') != chatId ) {
150 // console.log('getChats'); 149 // console.log('getChats');
151 ajax('get_chats.js'); 150 ajax('get_chats.js');
152 } 151 }
153 if( updated ) 152 if( updated )
154 lastUpdate = updated; 153 lastUpdate = updated;
154 if( !document.hasFocus() && !hasUnseen ) {
155 document.title = title + ' *';
156 notify();
157 hasUnseen = true;
158 }
155 } 159 }
156 160
157 function gotChats(html) { 161 function gotChats(html) {
158 document.querySelector('div[chats]').innerHTML = html; 162 document.querySelector('div[chats]').innerHTML = html;
159 if( currentChatId ) { 163 if( currentChatId ) {
168 } 172 }
169 } 173 }
170 174
171 window.onfocus = function() { 175 window.onfocus = function() {
172 document.title = title; 176 document.title = title;
177 hasUnseen = false;
173 }; 178 };
174 179
175 let urlRegex = /(^|\s)(https?:\/\/\S+)/g; 180 let urlRegex = /(^|\s)(https?:\/\/\S+)/g;
176 181
177 function urlsToLinks(text) { 182 function urlsToLinks(text) {
209 lastUpdate = updated; 214 lastUpdate = updated;
210 currentChatId = null; 215 currentChatId = null;
211 document.querySelector('div[posts]').innerHTML = ''; 216 document.querySelector('div[posts]').innerHTML = '';
212 ajax('get_chats.js'); 217 ajax('get_chats.js');
213 } 218 }
219
220 let sound = new Audio('/images/notify.mp3');
221 function notify() {
222 sound.play();
223 }
224