7
|
1
|
|
2 function ajax(url,postData) {
|
|
3 let request = new XMLHttpRequest();
|
|
4 let method = postData ? 'POST' : 'GET';
|
|
5 request.open( method, url );
|
34
|
6 if( postData )
|
7
|
7 request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
|
|
8 request.onload = function() {
|
|
9 if( request.status !== 200 ) {
|
|
10 window.console && console.log( 'ajax failed: ' + request.status );
|
|
11 if( request.responseText )
|
|
12 document.write('<pre>'+request.responseText+'</pre>');
|
|
13 return;
|
|
14 }
|
|
15 eval( request.responseText );
|
|
16 };
|
34
|
17 request.send(postData);
|
7
|
18 }
|
|
19
|
|
20 window.onerror = function(msg, url, line, col, error) {
|
|
21 if (!url)
|
|
22 return;
|
|
23 let err = msg;
|
|
24 err += '\nurl = ' + url;
|
|
25 if (url != window.location)
|
|
26 err += '\npage = ' + window.location;
|
|
27 err += '\nline = '+line;
|
|
28 if (col)
|
|
29 err += '\ncolumn = ' + col;
|
|
30 if (error && error.stack)
|
|
31 err += '\nstack = ' + error.stack;
|
|
32 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
|
|
33 };
|
34
|
34
|
|
35 function upload(input,callback) {
|
|
36 let file = input.files[0];
|
|
37 input.value = null;
|
|
38 let request = new XMLHttpRequest();
|
|
39 let url = 'https://upload.uploadcare.com/base/';
|
|
40 request.open( 'POST', url );
|
|
41 request.onload = function() {
|
|
42 if( request.status !== 200 ) {
|
|
43 let err = 'ajax failed: ' + request.status;
|
|
44 if( request.responseText ) {
|
|
45 err += '\n' + request.responseText;
|
|
46 document.write('<pre>'+request.responseText+'</pre>');
|
|
47 }
|
|
48 console.log(err);
|
|
49 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
|
|
50 return;
|
|
51 }
|
|
52 let response = JSON.parse(request.responseText);
|
|
53 let filename = file.name;
|
|
54 let url = 'https://ucarecdn.com/' + response.file + '/' + filename;
|
|
55 callback(url,filename);
|
|
56 };
|
|
57 let formData = new FormData();
|
|
58 formData.append( 'UPLOADCARE_PUB_KEY', 'fe3d30f3088a50941d45' );
|
|
59 formData.append( 'file', file );
|
|
60 request.send(formData);
|
|
61 }
|
|
62
|
|
63 function fileButtonClick(button) {
|
|
64 button.parentNode.querySelector('input[type="file"]').click();
|
|
65 }
|