annotate src/site.js @ 42:0c1b820fff34

use push
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 08 Nov 2022 14:02:28 -0700
parents 1ce75c5ab0f7
children 289718f121e4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 function ajax(url,postData) {
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 let request = new XMLHttpRequest();
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 let method = postData ? 'POST' : 'GET';
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 request.open( method, url );
34
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
6 if( postData )
7
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 request.onload = function() {
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 if( request.status !== 200 ) {
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 window.console && console.log( 'ajax failed: ' + request.status );
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 if( request.responseText )
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 document.write('<pre>'+request.responseText+'</pre>');
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 return;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 }
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 eval( request.responseText );
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 };
34
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
17 request.send(postData);
7
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 }
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 window.onerror = function(msg, url, line, col, error) {
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 if (!url)
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 return;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 let err = msg;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 err += '\nurl = ' + url;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 if (url != window.location)
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 err += '\npage = ' + window.location;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 err += '\nline = '+line;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 if (col)
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 err += '\ncolumn = ' + col;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 if (error && error.stack)
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 err += '\nstack = ' + error.stack;
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
0472897e790d add javascript
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 };
34
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
34
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
35 function upload(input,callback) {
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
36 let file = input.files[0];
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
37 input.value = null;
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
38 let request = new XMLHttpRequest();
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
39 let url = 'https://upload.uploadcare.com/base/';
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
40 request.open( 'POST', url );
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
41 request.onload = function() {
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
42 if( request.status !== 200 ) {
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
43 let err = 'ajax failed: ' + request.status;
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
44 if( request.responseText ) {
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
45 err += '\n' + request.responseText;
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
46 document.write('<pre>'+request.responseText+'</pre>');
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
47 }
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
48 console.log(err);
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
49 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
50 return;
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
51 }
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
52 let response = JSON.parse(request.responseText);
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
53 let filename = file.name;
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
54 let url = 'https://ucarecdn.com/' + response.file + '/' + filename;
35
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
55 callback(input,url,filename);
34
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
56 };
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
57 let formData = new FormData();
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
58 formData.append( 'UPLOADCARE_PUB_KEY', 'fe3d30f3088a50941d45' );
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
59 formData.append( 'file', file );
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
60 request.send(formData);
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
61 }
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
62
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
63 function fileButtonClick(button) {
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
64 button.parentNode.querySelector('input[type="file"]').click();
c8d47981c74f upload using ajax
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
65 }
42
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
66
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
67 function fixTextarea(textarea) {
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
68 let height = textarea.scrollHeight;
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
69 if( height > textarea.clientHeight ) {
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
70 textarea.style.height = height + "px";
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
71 }
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
72 }