forked from GolosChain/golos-explorer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.js
55 lines (51 loc) · 1.59 KB
/
init.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
let golosJsVersion = '0.7.3';
let blockchainVersion;
let $nodeAddress = document.getElementById('node-address');
let $nodeAddressInput = $nodeAddress.querySelector('.form-control[name="node-address"]');
if (localStorage && localStorage.nodeAddress) $nodeAddressInput.value = localStorage.nodeAddress;
let nodeAddress = $nodeAddressInput.value;
let loadGolosJsLib = () => {
let golosJsLib = document.createElement('script');
golosJsLib.src = `https://cdn.jsdelivr.net/npm/golos-js@${golosJsVersion}/dist/golos.min.js`;
golosJsLib.onload = () => {
let appLib = document.createElement('script');
appLib.src = 'app.js';
(document.head || document.documentElement).appendChild(appLib);
};
(document.head || document.documentElement).appendChild(golosJsLib);
};
let getBlockchainVersion = (nodeAddress, callback) => {
let socket;
try {
socket = new WebSocket(nodeAddress);
}
catch (e) {}
if (socket) {
socket.onopen = (event) => {
socket.send(JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'call',
params: ['database_api', 'get_config', [], ]
}));
socket.onmessage = (raw) => {
let data = JSON.parse(raw.data);
socket.close();
callback(null, data.result);
};
};
socket.onerror = (event) => {
console.error('onerror', event);
callback(event.code);
}
}
else callback(true);
};
getBlockchainVersion(nodeAddress, (err, result) => {
if (result) {
blockchainVersion = result.STEEMIT_BLOCKCHAIN_VERSION;
if (blockchainVersion < '0.18.0') golosJsVersion = '0.6.3';
else if (blockchainVersion < '0.18.4') golosJsVersion = '0.7.0';
}
loadGolosJsLib();
});