Optimized global client-side javascript code

-Javascript in the layout.pug has been moved around in an attempt to only load code into the client based on certain settings such as the type of menu (side or top) in order to slim down the loading of pages by not including unnecessary code
-Fixed a bug on the richlist page where the last updated date and logo spinning animation would fail to load if the distribution chart was disabled in the settings
-The .ext/getsummary api has been modified to only return the connection and block counts when necessary
-The getmasternodecount rpc api will no longer fetch data unless it is enabled in settings
This commit is contained in:
Joe Uhren
2023-11-03 18:42:34 -06:00
parent 5ca9602c74
commit 7ce3b5477e
4 changed files with 832 additions and 791 deletions
+70 -66
View File
@@ -440,82 +440,86 @@ app.use('/ext/getaddresstxs/:address/:start/:length', function(req, res) {
res.end('This method is disabled');
});
app.use('/ext/getsummary', function(req, res) {
// check if the getsummary api is enabled or else check the headers to see if it matches an internal ajax request from the explorer itself (TODO: come up with a more secure method of whitelisting ajax calls from the explorer)
if ((settings.api_page.enabled == true && settings.api_page.public_apis.ext.getsummary.enabled == true) || (req.headers['x-requested-with'] != null && req.headers['x-requested-with'].toLowerCase() == 'xmlhttprequest' && req.headers.referer != null && req.headers.accept.indexOf('text/javascript') > -1 && req.headers.accept.indexOf('application/json') > -1)) {
function get_connection_and_block_counts(get_data, cb) {
// check if the connection and block counts should be returned
if (get_data) {
lib.get_connectioncount(function(connections) {
lib.get_blockcount(function(blockcount) {
// check if this is a footer-only method that should only return the connection count and block count
if (req.headers['footer-only'] != null && req.headers['footer-only'] == 'true') {
// only return the connection count and block count
res.send({
connections: (connections ? connections : '-'),
blockcount: (blockcount ? blockcount : '-')
});
} else {
lib.get_hashrate(function(hashrate) {
db.get_stats(settings.coin.name, function (stats) {
lib.get_masternodecount(function(masternodestotal) {
lib.get_difficulty(function(difficulty) {
difficultyHybrid = '';
return cb(connections, blockcount);
});
});
} else
return cb(null, null);
}
if (difficulty && difficulty['proof-of-work']) {
if (settings.shared_pages.difficulty == 'Hybrid') {
difficultyHybrid = 'POS: ' + difficulty['proof-of-stake'];
difficulty = 'POW: ' + difficulty['proof-of-work'];
} else if (settings.shared_pages.difficulty == 'POW')
difficulty = difficulty['proof-of-work'];
else
difficulty = difficulty['proof-of-stake'];
app.use('/ext/getsummary', function(req, res) {
const isInternal = (req.headers['x-requested-with'] != null && req.headers['x-requested-with'].toLowerCase() == 'xmlhttprequest' && req.headers.referer != null && req.headers.accept.indexOf('text/javascript') > -1 && req.headers.accept.indexOf('application/json') > -1);
// check if the getsummary api is enabled or else check the headers to see if it matches an internal ajax request from the explorer itself (TODO: come up with a more secure method of whitelisting ajax calls from the explorer)
if ((settings.api_page.enabled == true && settings.api_page.public_apis.ext.getsummary.enabled == true) || isInternal) {
// check if this is a footer-only method that should only return the connection count and block count
if (req.headers['footer-only'] != null && req.headers['footer-only'] == 'true') {
// only return the connection count and block count
get_connection_and_block_counts(true, function(connections, blockcount) {
res.send({
connections: (connections ? connections : '-'),
blockcount: (blockcount ? blockcount : '-')
});
});
} else {
// get the connection and block counts only if this is NOT an internal call
get_connection_and_block_counts(!isInternal, function(connections, blockcount) {
lib.get_hashrate(function(hashrate) {
db.get_stats(settings.coin.name, function (stats) {
lib.get_masternodecount(function(masternodestotal) {
lib.get_difficulty(function(difficulty) {
let difficultyHybrid = '';
if (difficulty && difficulty['proof-of-work']) {
if (settings.shared_pages.difficulty == 'Hybrid') {
difficultyHybrid = 'POS: ' + difficulty['proof-of-stake'];
difficulty = 'POW: ' + difficulty['proof-of-work'];
} else if (settings.shared_pages.difficulty == 'POW')
difficulty = difficulty['proof-of-work'];
else
difficulty = difficulty['proof-of-stake'];
}
if (hashrate == 'There was an error. Check your console.')
hashrate = 0;
let mn_total = 0;
let mn_enabled = 0;
// check if the masternode count api is enabled
if (settings.api_page.public_apis.rpc.getmasternodecount.enabled == true && settings.api_cmds['getmasternodecount'] != null && settings.api_cmds['getmasternodecount'] != '') {
// masternode count api is available
if (masternodestotal) {
if (masternodestotal.total)
mn_total = masternodestotal.total;
if (masternodestotal.enabled)
mn_enabled = masternodestotal.enabled;
}
}
if (hashrate == 'There was an error. Check your console.')
hashrate = 0;
// check if the masternode count api is enabled
if (settings.api_page.public_apis.rpc.getmasternodecount.enabled == true && settings.api_cmds['getmasternodecount'] != null && settings.api_cmds['getmasternodecount'] != '') {
// masternode count api is available
var mn_total = 0;
var mn_enabled = 0;
if (masternodestotal) {
if (masternodestotal.total)
mn_total = masternodestotal.total;
if (masternodestotal.enabled)
mn_enabled = masternodestotal.enabled;
}
res.send({
difficulty: (difficulty ? difficulty : '-'),
difficultyHybrid: difficultyHybrid,
supply: (stats == null || stats.supply == null ? 0 : stats.supply),
hashrate: hashrate,
lastPrice: (stats == null || stats.last_price == null ? 0 : stats.last_price),
connections: (connections ? connections : '-'),
masternodeCountOnline: (masternodestotal ? mn_enabled : '-'),
masternodeCountOffline: (masternodestotal ? Math.floor(mn_total - mn_enabled) : '-'),
blockcount: (blockcount ? blockcount : '-')
});
} else {
// masternode count api is not available
res.send({
difficulty: (difficulty ? difficulty : '-'),
difficultyHybrid: difficultyHybrid,
supply: (stats == null || stats.supply == null ? 0 : stats.supply),
hashrate: hashrate,
lastPrice: (stats == null || stats.last_price == null ? 0 : stats.last_price),
connections: (connections ? connections : '-'),
blockcount: (blockcount ? blockcount : '-')
});
}
res.send({
difficulty: (difficulty ? difficulty : '-'),
difficultyHybrid: difficultyHybrid,
supply: (stats == null || stats.supply == null ? 0 : stats.supply),
hashrate: hashrate,
lastPrice: (stats == null || stats.last_price == null ? 0 : stats.last_price),
connections: (connections ? connections : '-'),
blockcount: (blockcount ? blockcount : '-'),
masternodeCountOnline: (masternodestotal && mn_enabled != 0 ? mn_enabled : '-'),
masternodeCountOffline: (masternodestotal && mn_total != 0 ? Math.floor(mn_total - mn_enabled) : '-')
});
});
});
});
}
});
});
});
}
} else
res.end('This method is disabled');
});