-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
332 lines (310 loc) · 9.45 KB
/
index.html
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Grin Blockchain Explorer on IPFS</title>
<script language="javascript" src="https://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript" src="https://cdn.rawgit.com/caldwell/renderjson/master/renderjson.js"></script>
</head>
<body>
<header>
<h2>Grin Blockchain Explorer on IPFS</h2>
</header>
<nav>
<p>
<a href="javascript:prevPage()" id="btn_prev">Previous</a>
<a href="javascript:nextPage()" id="btn_next">Next</a>
--
<a href="javascript:firstPage()" id="btn_first">First</a>
<a href="javascript:lastPage()" id="btn_last">Last</a>
--
Jump To: <input name="jumpTo" type="text" size="10" id="jumpTo" class="jumpTo"/>
<a href="javascript:jumpToPage()" id="btn_jtp">Go</a>
</p>
</nav>
<div class="forkWarning" id="forkWarning"></div>
<div class="heightData" id="heightData"></div>
<div class="acceptedBlockData" id="acceptedBlockData"></div>
<div class="fork1BlockData" id="fork1BlockData"></div>
<footer>
<p>Please help improve this explorer UI: <A href="https://github.com/bladedoyle/grin_ipfs_explorer">github</a></p>
</footer>
</body>
<script>
var base_url = window.location.origin + "/ipns/k51qzi5uqu5di6j7rwnp4b2bb98nh1dwypwaymemx044kwnap8d0p1qmghhjpf" // + window.location.pathname.split( '/' )[2]
var pageCount = getNumBlocks();
var current_page = pageCount;
var cache = {};
function prevPage()
{
if(current_page > 0) {
current_page--;
newPage();
}
}
function nextPage()
{
if(current_page < pageCount) {
current_page++;
newPage();
}
}
function firstPage()
{
if(current_page > 0) {
current_page = 0;
newPage();
}
}
function lastPage()
{
if(current_page < pageCount) {
current_page = pageCount;
newPage();
}
}
function jumpToPage()
{
var page = document.getElementsByClassName("jumpTo")[0].value;
if(!page) {
document.getElementsByClassName("jumpTo")[0].value = current_page
return;
}
page_int = parseInt(page)
if(isNaN(page_int) || page_int < 0 || page_int > pageCount) {
document.getElementsByClassName("jumpTo")[0].value = current_page
return;
}
current_page = parseInt(page)
newPage()
}
function newPage()
{
// Validate page number
if (current_page < 0) current_page = 0;
if (current_page > pageCount) current_page = pageCount;
// Manage the cache
var p;
for(p=current_page-2; p<current_page+2; p++) {
getBlockData(p);
getBlockData(p+"_fork1");
}
renderPage()
}
function renderPage()
{
var btn_next = document.getElementById("btn_next");
var btn_prev = document.getElementById("btn_prev");
var fork_warning_e = document.getElementById("forkWarning")
var height_e = document.getElementById("heightData")
var accepted_block_e = document.getElementById("acceptedBlockData")
var fork1_block_e = document.getElementById("fork1BlockData");
// Init
document.getElementsByClassName("jumpTo")[0].value = "";
fork_warning_e.innerHTML = "";
height_e.innerHTML = "";
accepted_block_e.innerHTML = "";
fork1_block_e.innerHTML = "";
// Current Height Info
height_e.innerHTML += "Viewing Block Data for height <b>" + current_page + "</b> (of " +pageCount + "):";
// Fetch this blocks data
var block_data = getCachedBlockData(current_page)
if( block_data == null) {
block_data = "Loading data for block: " + current_page
}
var block_render = renderjson.set_show_to_level(2)(block_data);
var block_fork1_data = getCachedBlockData(current_page+"_fork1");
var block_fork1_render;
if(block_fork1_data != null) {
// The _fork is the accepted block, the original block data is the fork
block_fork1_render = block_render;
block_render = renderjson.set_show_to_level(2)(block_fork1_data);
fork_warning_e.innerHTML += "<b>Warning:</b> There was a blockchain reorg resulting in multiple blocks at this height"
fork_warning_e.style.display = "block"
accepted_block_e.innerHTML += "Accepted Block Data:";
} else {
fork_warning_e.style.display = "none"
}
accepted_block_e.appendChild(block_render);
if(block_fork1_data != null) {
fork1_block_e.innerHTML += "Replaced Block Data:";
fork1_block_e.appendChild(block_fork1_render);
fork1_block_e.style.display = "block"
} else {
fork1_block_e.style.display = "none"
}
// Manage the buttons - grey out invlid direction from current block
if (current_page == 0) {
btn_prev.style.color = "#707070";
btn_first.style.color = "#707070";
} else {
btn_prev.style.color = "#0000EE";
btn_first.style.color = "#0000EE";
}
if (current_page == pageCount) {
btn_next.style.color = "#707070";
btn_last.style.color = "#707070";
} else {
btn_next.style.color = "#0000EE";
btn_last.style.color = "#0000EE";
}
}
// Get content of url as json
function getJson(url){
try {
// rate limit
let fork = false;
if(url.includes("_fork")) {
fork=true;
}
fetch(base_url+'/'+url).then((response) => {
// Got good response
console.log(response);
if(response.ok) {
console.log("ok");
return(response.json());
} else {
console.log("not ok");
return(null);
}
}).then((block_json) => {
console.log("then")
//console.log("block data: "+block_json)
console.log("url: "+url)
console.log("----")
height = url.split('/').pop()
cache[height] = block_json;
height_int = parseInt(height);
if(height_int == current_page) {
console.log("Rendering");
renderPage();
} else {
console.log("skipping render");
}
}).catch((error) => {
console.log('There has been a problem with your fetch operation: ' + error.message);
});
}
catch(err) {
console.log("should not get here" );
return null;
}
}
// Get num blocks
function getNumBlocks() {
var request = new XMLHttpRequest();
try {
request.open('GET', base_url+'/height', false);
request.send(null);
var height_data = JSON.parse(request.responseText);
return parseInt(height_data["height"]);
}
catch(err) {
return null;
}
}
// Get Block Data From Cache
function getCachedBlockData(height) {
if(height < 0 || height > pageCount) {
return null;
}
if(height in cache) {
console.log("found "+height+" in the cache")
return cache[height]
} else {
return null;
}
}
// Get block by height as JSON
function getBlockData(height) {
if(height < 0 || height > pageCount) {
return null;
}
if(height in cache) {
console.log("found "+height+" in the cache")
return cache[height]
} else {
console.log("Fetching "+height);
//console.log("cache: "+JSON.stringify(cache,null,'\t'));
var dir = Math.trunc(parseInt(height) / 10000)
getJson(dir + '/' + height)
return null
}
}
window.onload = function() {
newPage();
};
</script>
<style>
/* Style the json block data */
.renderjson a { text-decoration: none; }
.renderjson .disclosure { color: crimson;
font-size: 150%; }
.renderjson .syntax { color: grey; }
.renderjson .string { color: #020f99; }
.renderjson .number { color: #029911; }
.renderjson .boolean { color: #660066; }
.renderjson .key { color: #401e1e; }
.renderjson .keyword { color: lightgoldenrodyellow; }
.renderjson .object.syntax { color: lightseagreen; }
.renderjson .array.syntax { color: lightsalmon; }
/* Style the header */
header {
background-color: #666600;
padding: 10px;
text-align: center;
font-size: 20px;
color: white;
}
/* Style the navigation bar */
nav {
background: #FFFF99;
text-align: center;
font-size: 18px;
color: black;
padding: 3px;
}
/* Style the height data */
.heightData {
background-color: #666600;
padding: 10px;
text-align: center;
font-size: 15px;
color: white;
}
/* Style the fork warning */
.forkWarning {
background-color: #ffd6b0;
padding: 10px;
text-align: center;
font-size: 15px;
color: black;
}
/* Style the accepted block data */
.acceptedBlockData {
background-color: #FFFF99;
padding: 10px;
text-align: left;
font-size: 13px;
color: black;
}
/* Style the forked block data */
.fork1BlockData {
background-color: #CCCC00;
padding: 10px;
text-align: left;
font-size: 10px;
color: black;
}
/* Style the footer */
footer {
background-color: #666600;
padding: 10px;
text-align: center;
font-size: 13px;
color: white;
}
</style>
</html>