-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
189 lines (179 loc) · 8.06 KB
/
scripts.js
File metadata and controls
189 lines (179 loc) · 8.06 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Loaded Eventlistener
window.addEventListener('load', () => {
// Call Service Worker Register
registerSW();
});
// Register the Service Worker
async function registerSW() {
// Is Browser Compatibale with Service Workers?
if ('serviceWorker' in navigator) {
try {
// Load Service Worker Script
await navigator
.serviceWorker
.register('service_worker.js');
}
catch (e) {
// ERROR CATCH SERVICE WORKER
console.log('SW registration failed');
}
}
}
function loadDUCOData(UserName) {
// Hide Dashboard
document.getElementById("dashboard").style.display = "none";
// Show Spin load indicator
document.getElementById("spinner").style.display = "block";
// DUCO Balance URL
const balance_url = 'https://server.duinocoin.com/balances/' + UserName;
// DUCO Miners URL
const miners_url = 'https://server.duinocoin.com/miners/' + UserName;
// DUCO Chain Statistics
const stats_url = 'https://server.duinocoin.com/statistics';
// Getting Json from REST-API
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();
};
// Getting User Balance
getJSON(balance_url,
function (err, data) {
if (err !== null) {
// Called when resived no Data.
alert('Something went wrong: ' + err);
} else {
var user_html = "";
// If data is including message then it's a error message likely account isn't available
if (data.message) {
user_html += '<div class="w3-row"><div class="w3-rest">' + data.message + '</div></div><div class="w3-bar hr"></div>';
} else {
// UNIX to date time
var date = new Date(data.result.stake_date * 1000);
// Generating UI stuff
user_html += '<div class="w3-row">';
user_html += '<div class="w3-half w3-container">Username: ' + data.result.username;
user_html += '<br>Balance: ' + data.result.balance + " ᕲ DUCO";
user_html += '<br>Staked: ' + data.result.stake_amount + " ᕲ DUCO</div>";
user_html += '<div class="w3-half w3-container">Verified: ' + data.result.verified;
user_html += '<br>End stake date: ' + date + "</div>";
user_html += '</div>';
}
// Adding UI stuff to user DIV
document.getElementById("user").innerHTML = user_html;
}
});
// Getting Users Active Miners
getJSON(miners_url,
function (err, data) {
if (err !== null) {
// Called when resived no Data.
alert('Something went wrong: ' + err);
} else {
var miners_html = "";
// Count for onclick "Accordions" Opening and adding a uniqe ID
var miner_count = 0;
if (data.message) {
// If data is including message then it's a error message likely account isn't available
miners_html += '<div class="w3-row"><div class="w3-col inactive_dot"></div><div class="w3-rest">' + data.message + '</div></div>';
} else {
// HTML UI Stuff
miners_html += '<div class="w3-row"><div class="w3-col active_dot"></div><div class="w3-rest">Mining active</div></div>';
miners_html += "<ul class='w3-ul'>";
// Loop in inner JSON Array for Miners
data.result.forEach(item => {
// HTML UI stuff
miners_html += '<button onclick="open_Miner(' + miner_count + ')" class="w3-button w3-block w3-left-align miner_hover">' + item.identifier + '</button><div id="' + miner_count + '" class="w3-animate-bottom w3-container w3-hide w3-row"><div class="w3-half w3-container"><p>Hashrate: ' + item.hashrate + '</p><p>Software: ' + item.software + '</p></div><div class="w3-half w3-container"><p>Accepted: ' + item.accepted + '</p><p>Rejected: ' + item.rejected + '</p></div></div>';
// Adding +1 for onclick "Accordions" Opening and adding a uniqe ID
miner_count++;
});
miners_html += '</ul>';
}
// Adding UI stuff to miners DIV
document.getElementById("miners").innerHTML = miners_html;
}
});
// Getting Chain statistics
getJSON(stats_url,
function (err, data) {
if (err !== null) {
// Called when resived no Data.
alert('Something went wrong: ' + err);
} else {
var stat_html = "";
if (data.message) {
// If data is including message then it's a error message likely isn't available
stat_html += '<div class="w3-row"><div class="w3-rest">Error loading statistics</div></div><div class="w3-bar hr"></div>';
} else {
// Needs to Stringify the resived JSON has a weird format
const myObj = JSON.stringify(data);
// Parsing it to a array for better looping in the Json
const myObj2 = JSON.parse(myObj);
// HTML UI stuff
stat_html += '<ul class="w3-ul">';
// JSON Loop
for (var key in myObj2) {
// Ignoring more deeper Arrays (Work on it!)
if (myObj2[key] == '[object Object]') {
} else {
// HTML UI stuff getting data from the Loop key and data
stat_html += '<li>' + (key + ': ' + myObj2[key]) + '</li>';
}
}
stat_html += '</ul>'
}
// Adding UI stuff to statistics DIV
document.getElementById("statistics").innerHTML = stat_html;
// Show dashboard
document.getElementById("spinner").style.display = "none";
// Hide Spin load indicator
document.getElementById("dashboard").style.display = "block";
}
});
}
// Miner "Accordions" opening script uniqe with ID
function open_Miner(id) {
//Getting UI element
var x = document.getElementById(id);
// Is UI element visible?
if (x.className.indexOf("w3-show") == -1) {
// Show UI element
x.className += " w3-show";
} else {
// Remove UI element
x.className = x.className.replace(" w3-show", "");
}
}
// Getting Entered User Name from input
// Get Input Element By ID
const username_input = document.getElementById("user_name_input");
// Add Event Listener to Username Input ID
username_input.addEventListener("submit", inputSubmit);
// Listening for Keydown
username_input.onkeydown = function (e) {
// Submit with ENTER
if (e.keyCode == 13) {
// submit
inputSubmit();
}
};
// Input Submit function
function inputSubmit() {
if (username_input.value != "" || username_input.value == null) {
// Submit Username to the REST-API Function
loadDUCOData(username_input.value);
// Blur Username Input
username_input.blur();
// Deselect Username Input
loadDUCOData.selectionStart = username_input.selectionEnd;
} else { }
}