forked from yttrian/drect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrect.js
64 lines (52 loc) · 1.7 KB
/
drect.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
function DRect() {
this.serverEl = document.getElementById('server');
this.progressEl = document.getElementById('progress');
this.init()
}
DRect.prototype.init = function () {
var id = this.getID();
if (id === undefined) {
this.serverEl.classList.add('visible');
} else {
this.getData(id, this.populate);
}
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js', {scope: '/'}).then(function () {
console.log("Service Worker Registered");
});
}
};
DRect.prototype.getID = function () {
return location.hash.slice(1) || undefined;
};
DRect.prototype.getData = function (id, callback) {
var myself = this;
var url = 'https://discordapp.com/api/guilds/' + id.toString() + '/widget.json';
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
var responseJSON = JSON.parse(this.responseText);
if (this.status === 200) {
callback.call(myself, responseJSON);
} else {
callback.call(myself, {name: responseJSON.message || 'Invalid ID', instant_invite: null})
}
}
};
xhr.onerror = function () {
callback.call(myself, {name: "Connection Error", instant_invite: null});
};
xhr.open('GET', url, true);
xhr.send();
};
DRect.prototype.populate = function (data) {
this.serverEl.innerText = data.name;
this.serverEl.classList.add('visible');
if (data.instant_invite) {
this.progressEl.classList.add('full');
setTimeout(function () {
location.href = data.instant_invite;
}, 3e3);
}
};
drect = new DRect();