-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetHTML.dev.html
More file actions
92 lines (77 loc) · 2.31 KB
/
getHTML.dev.html
File metadata and controls
92 lines (77 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<script>
/**
* Get HTML asynchronously
* @param {String} url The URL to get HTML from
* @param {Function} callback A callback funtion. Pass in "response" variable to use returned HTML.
*/
var getHTML = function ( url, callback ) {
// Feature detection
if ( !window.XMLHttpRequest ) return;
// Create new request
var xhr = new XMLHttpRequest();
// Setup callback
xhr.onload = function() {
if ( callback && typeof( callback ) === 'function' ) {
callback( this.responseXML );
}
}
// Get the HTML
xhr.open( 'GET', url );
xhr.responseType = 'document';
xhr.send();
};
var postHTML = function ( url, params = {}, callback ) {
if ( !window.XMLHttpRequest ) return;
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if ( callback && typeof( callback ) === 'function' ) {
callback( this.responseXML );
}
}
var data = new FormData();
const keys = Object.keys(params);
keys.forEach((key, index) => {
data.append( key, params[key] );
});
xhr.open( 'POST', url, true );
xhr.responseType = 'document';
xhr.send(data);
};
const request = ( url, params = {}, method = 'GET' ) => {
let options = {
method
};
if ( 'GET' === method ) {
url += '?' + ( new URLSearchParams( params ) ).toString();
} else {
options.body = JSON.stringify( params );
}
//return fetch( url, options ).then( response => response.json() );
return fetch( url, options ).then( response => response.text() );
};
const do_get = ( url, params ) => request( url, params, 'GET' );
const do_post = ( url, params ) => request( url, params, 'POST' );
function greyout() {
let pos = document.getElementById('greyout').getBoundingClientRect();
document.getElementById('greyout').style.bottom += window.scrollY;
document.getElementById('greyout').style.display = 'block';
document.getElementById('mainbody').style.overflow = 'hidden';
}
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send();
};
function commify(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
</script>