annotate src/site.js @ 5:aa1920665f98

discord login
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 23 Oct 2023 00:43:41 -0600
parents 8896ffd7b152
children 88f425d261b3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 'use strict';
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 function ajax(url,postData,context) {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 let request = new XMLHttpRequest();
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 let method = postData ? 'POST' : 'GET';
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 request.open( method, url );
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 if( postData )
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 request.onload = function() {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 if( request.status !== 200 ) {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 console.log( 'ajax failed: ' + request.status );
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 if( request.responseText )
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 document.write('<pre>'+request.responseText+'</pre>');
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 return;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 }
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 try {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 eval( request.responseText );
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 } catch(e) {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 console.log( request.responseText );
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 window.err = '\najax-url = ' + url;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 throw e;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 }
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 };
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 request.send(postData);
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 }
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 window.onerror = function(msg, url, line, col, error) {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 if (!url)
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 return;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 let err = msg;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 err += '\nurl = ' + url;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 if (url != window.location)
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 err += '\npage = ' + window.location;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 err += '\nline = '+line;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 if (col)
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 err += '\ncolumn = ' + col;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 if (error && error.stack)
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 err += '\nstack = ' + error.stack;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 if( window.err ) {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 err += window.err;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 window.err = null;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 }
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 };
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 function logout() {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 document.cookie = 'user=; Max-Age=0; path=/;';
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 location = '/';
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 }