Mercurial Hosting > chat
diff src/site.js @ 3:2c63b10781e1
add login
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 24 Oct 2024 21:43:44 -0600 |
parents | |
children | dade6a560494 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/site.js Thu Oct 24 21:43:44 2024 -0600 @@ -0,0 +1,76 @@ +'use strict'; + +function ajax(url,postData,context) { + let request = new XMLHttpRequest(); + let method = postData ? 'POST' : 'GET'; + request.open( method, url ); + if( postData ) + request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); + request.onload = function() { + if( request.status !== 200 ) { + let err = 'ajax failed: ' + request.status; + if( request.responseText ) { + err += '\n' + request.responseText.trim(); + document.write('<pre>'+request.responseText+'</pre>'); + } + err += '\nurl = ' + url; + err += '\npage = ' + window.location; + ajax( '/error_log.js', 'err='+encodeURIComponent(err) ); + return; + } + try { + eval( request.responseText ); + } catch(e) { + console.log( request.responseText ); + window.err = '\najax-url = ' + url; + throw e; + } + }; + request.send(postData); +} + +window.onerror = function(msg, url, line, col, error) { + if( !url ) + return; + let err = msg; + err += '\nurl = ' + url; + if( url != window.location ) + err += '\npage = ' + window.location; + err += '\nline = '+line; + if( col ) + err += '\ncolumn = ' + col; + if( error ) { + if( error.stack ) + err += '\nstack = ' + error.stack; + if( error.cause ) + err += '\ncause= ' + error.cause; + if( error.fileName ) + err += '\nfileName= ' + error.fileName; + } + if( window.err ) { + err += window.err; + window.err = null; + } + ajax( '/error_log.js', 'err='+encodeURIComponent(err) ); +}; + +function ajaxForm(url,form) { + let post = ''; + for( let i=0; i<form.length; i++ ) { + let input = form[i]; + let name = input.name; + if( name === '' ) + continue; + let type = input.type; + if( (type==='radio' || type==='checkbox') && !input.checked ) + continue; + post += name + '=' + encodeURIComponent(input.value) + '&'; + } + ajax(url,post,{form:form}); +} + +function logout() { + document.cookie = 'user=; Max-Age=0; path=/;'; + document.cookie = 'password=; Max-Age=0; path=/;'; + location = '/'; +}