-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathhomepage.js
83 lines (63 loc) · 2.53 KB
/
homepage.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
83
var noFocus = 0;
var ws;
var retry = 0;
decimals = 2;
function updateTimes() {
document.hasFocus ? noFocus = 0 : ++noFocus;
if (noFocus > 120 && ws) {
ws.close();
ws = null;
}
var now = new Date().getTime() / 1000;
$('td[data-time]').each(function(index) {
var diff = now - $(this).attr('data-time');
if (diff < 60) {
$(this).text('< 1 minute');
} else if (diff < 3600) {
var p = (parseInt(diff / 60) > 1) ? 's' : '';
$(this).text(parseInt(diff / 60) + ' minute'+p);
} else {
var p = (parseInt(diff / 3600) > 1) ? 's' : '';
$(this).text(parseInt(diff / 3600) + ' hour'+p+' ' + parseInt((diff % 3600) / 60) + ' minutes');
}
});
}
function connect() {
try {
ws = new WebSocket(getWebSocketURL());
ws.onmessage = function(e) {
var obj = $.parseJSON(e.data);
if (obj.op == 'minitx') {
var tx = obj.x;
$('<tr><td><div><a href="'+root+'tx-index/'+tx.txIndex+'/'+tx.hash+'">'+tx.hash.substring(0, 25)+'...</a></div></td><td class="hidden-phone" data-time="'+tx.time+'"><div>< 1 minute</div></td><td><div><button class="btn btn-success" onclick="toggleSymbol()">'+ formatMoney(tx.value, true) +'</button></div></td></tr>').insertAfter($('#txs tr:first')).find('div').hide().slideDown('slow');
$('#txs tr:last-child').remove();
} else if (obj.op == 'block') {
console.log('on block');
var block = BlockFromJSON(obj.x);
var foundByTxt = 'Unknown';
if (block.foundBy != null) {
foundByTxt = '<a href="'+block.foundBy.link+'">'+block.foundBy.description+'</a>';
}
$('<tr><td><div><a href="'+root+'block-index/'+block.blockIndex+'/'+block.hash+'">'+block.height+'</a></div></td><td data-time="'+block.time+'"><div>< 1 minute</div></td><td class="hidden-phone"><div>'+block.txIndex.length+'</div></td><td class="hidden-phone"><div>'+formatMoney(block.totalBTCSent, true)+'</div></td><td><div>'+foundByTxt+'</div></td><td class="hidden-phone"><div>'+parseInt(block.size / 1024)+'</div></td></tr>').insertAfter($('#blocks tr:first')).find('div').hide().slideDown('slow');
$('#blocks tr:last-child').remove();
}
};
ws.onopen = function() {
retry = 0;
ws.send('{"op":"set_tx_mini"}{"op":"unconfirmed_sub"}{"op":"blocks_sub"}');
};
ws.onclose = function() {
console.log('On close');
if(document.hasFocus && retry < 3) {
setTimeout(connect, 1000*retry);
++retry;
}
};
} catch (e) {
console.log(e);
}
}
$(document).ready(function() {
setInterval(updateTimes, 1000);
connect();
});