comparison src/chat.js @ 19:435f474f07c7

urls
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 04 Nov 2024 19:23:38 -0700
parents 0721dcf222e1
children dade6a560494
comparison
equal deleted inserted replaced
18:0721dcf222e1 19:435f474f07c7
29 eventSource.onmessage = evalEvent; 29 eventSource.onmessage = evalEvent;
30 } 30 }
31 31
32 function gotChat(html) { 32 function gotChat(html) {
33 document.querySelector('div[posts]').innerHTML = html; 33 document.querySelector('div[posts]').innerHTML = html;
34 fixDates(); 34 fixPosts();
35 document.querySelector('div[input] textarea').focus(); 35 document.querySelector('div[input] textarea').focus();
36 document.querySelector('div[input]').scrollIntoView({block: 'end'}); 36 document.querySelector('div[input]').scrollIntoView({block: 'end'});
37 } 37 }
38 38
39 function fixTextarea(event) { 39 function fixTextarea(event) {
59 event.preventDefault(); 59 event.preventDefault();
60 addPost(); 60 addPost();
61 } 61 }
62 } 62 }
63 63
64 function fixDates() { 64 function fixPosts() {
65 let spans = document.querySelectorAll('span[when][fix]'); 65 let spans = document.querySelectorAll('span[when][fix]');
66 for( let span of spans ) { 66 for( let span of spans ) {
67 span.textContent = new Date(Number(span.textContent)).toLocaleString(); 67 span.textContent = new Date(Number(span.textContent)).toLocaleString();
68 span.removeAttribute('fix'); 68 span.removeAttribute('fix');
69 }
70 let divs = document.querySelectorAll('div[text][fix]');
71 for( let div of divs ) {
72 div.innerHTML = urlsToLinks(div.innerHTML);
73 div.removeAttribute('fix');
69 } 74 }
70 } 75 }
71 76
72 function deleteChat() { 77 function deleteChat() {
73 ajax(`delete_chat.js?chat=${currentChatId}`); 78 ajax(`delete_chat.js?chat=${currentChatId}`);
74 } 79 }
75 80
76 function added(html) { 81 function added(html) {
77 let input = document.querySelector('div[input]'); 82 let input = document.querySelector('div[input]');
78 input.insertAdjacentHTML('beforebegin',html); 83 input.insertAdjacentHTML('beforebegin',html);
79 fixDates(); 84 fixPosts();
80 input.scrollIntoView({block: 'end'}); 85 input.scrollIntoView({block: 'end'});
81 if( !document.hasFocus() ) 86 if( !document.hasFocus() )
82 document.title = title + ' *'; 87 document.title = title + ' *';
83 } 88 }
84 89
105 } 110 }
106 111
107 window.onfocus = function() { 112 window.onfocus = function() {
108 document.title = title; 113 document.title = title;
109 }; 114 };
115
116 let urlRegex = /(^|\s)(https?:\/\/\S+)/g;
117
118 function urlsToLinks(text) {
119 return text.replace( urlRegex, '$1<a href="$2">$2</a>' );
120 }