This repository has been archived by the owner on Aug 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatusApi.js
83 lines (63 loc) · 1.81 KB
/
StatusApi.js
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
/**
* Created by alejandrorioscalera
* 12/05/2018
*
* -- SOCIAL NETWORKS --
*
* GitHub: https://github.com/clonalejandro or @clonalejandro
* Website: https://clonalejandro.me/
* Twitter: https://twitter.com/clonalejandro11/ or @clonalejandro11
* Keybase: https://keybase.io/clonalejandro/
*
* -- LICENSE --
*
* All rights reserved for clonalejandro ©clonastatus 2017 / 2018
*/
class StatusApi {
/** SMALL CONSTRUCTORS **/
constructor(search){
this.domain = serializeDomain(`https://server.clonalejandro.me/api/status/?domain=${search.domain}&port=${search.port}`);
this.init();
}
/** REST **/
/**
* This function manage the main process
*/
init(){
this.getRequest((err, data) => {
if (err) console.log(err);
else console.log(data);
})
}
/**
* This function returns a request for get the main api
* @param callback
* @returns {XMLHttpRequest}
*/
getRequest(callback){
const request = new XMLHttpRequest();
request.open('GET', this.domain, true);
request.responseType = 'json';
request.onload = () => {
if (request.status === 200) callback(null, request.response);
else callback(request.status, request.response);
};
request.send();
return request;
}
}
/** OTHERS **/
/**
* This function serialize the domain string
* @param domain
* @returns {string}
*/
function serializeDomain(domain) {
const uri = new URL(document.URL);
let _domain = "?domain=" + uri.searchParams.get("domain");
const port = "&port=" + uri.searchParams.get("port");
//Clean the domain parameter
_domain = _domain.replace("https://", "");
_domain = _domain.replace("http://", "");
return domain + _domain + port;
}