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'); res.end('This method is disabled');
}); });
app.use('/ext/getsummary', function(req, res) { function get_connection_and_block_counts(get_data, cb) {
// 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) // check if the connection and block counts should be returned
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)) { if (get_data) {
lib.get_connectioncount(function(connections) { lib.get_connectioncount(function(connections) {
lib.get_blockcount(function(blockcount) { lib.get_blockcount(function(blockcount) {
// check if this is a footer-only method that should only return the connection count and block count return cb(connections, blockcount);
if (req.headers['footer-only'] != null && req.headers['footer-only'] == 'true') { });
// only return the connection count and block count });
res.send({ } else
connections: (connections ? connections : '-'), return cb(null, null);
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 = '';
if (difficulty && difficulty['proof-of-work']) { app.use('/ext/getsummary', function(req, res) {
if (settings.shared_pages.difficulty == 'Hybrid') { 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);
difficultyHybrid = 'POS: ' + difficulty['proof-of-stake'];
difficulty = 'POW: ' + difficulty['proof-of-work']; // 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)
} else if (settings.shared_pages.difficulty == 'POW') if ((settings.api_page.enabled == true && settings.api_page.public_apis.ext.getsummary.enabled == true) || isInternal) {
difficulty = difficulty['proof-of-work']; // check if this is a footer-only method that should only return the connection count and block count
else if (req.headers['footer-only'] != null && req.headers['footer-only'] == 'true') {
difficulty = difficulty['proof-of-stake']; // 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.') res.send({
hashrate = 0; difficulty: (difficulty ? difficulty : '-'),
difficultyHybrid: difficultyHybrid,
// check if the masternode count api is enabled supply: (stats == null || stats.supply == null ? 0 : stats.supply),
if (settings.api_page.public_apis.rpc.getmasternodecount.enabled == true && settings.api_cmds['getmasternodecount'] != null && settings.api_cmds['getmasternodecount'] != '') { hashrate: hashrate,
// masternode count api is available lastPrice: (stats == null || stats.last_price == null ? 0 : stats.last_price),
var mn_total = 0; connections: (connections ? connections : '-'),
var mn_enabled = 0; blockcount: (blockcount ? blockcount : '-'),
masternodeCountOnline: (masternodestotal && mn_enabled != 0 ? mn_enabled : '-'),
if (masternodestotal) { masternodeCountOffline: (masternodestotal && mn_total != 0 ? Math.floor(mn_total - mn_enabled) : '-')
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 : '-')
});
}
}); });
}); });
}); });
}); });
} });
}); });
}); }
} else } else
res.end('This method is disabled'); res.end('This method is disabled');
}); });
+25 -19
View File
@@ -347,27 +347,33 @@ module.exports = {
}, },
get_masternodecount: function(cb) { get_masternodecount: function(cb) {
var cmd = prepareRpcCommand(settings.api_cmds.getmasternodecount); // 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'] != '') {
var cmd = prepareRpcCommand(settings.api_cmds.getmasternodecount);
if (!(cmd.method == '' && cmd.parameters.length == 0)) { if (!(cmd.method == '' && cmd.parameters.length == 0)) {
if (settings.api_cmds.use_rpc) { if (settings.api_cmds.use_rpc) {
rpcCommand([{method:cmd.method, parameters: cmd.parameters}], function(response) { rpcCommand([{method:cmd.method, parameters: cmd.parameters}], function(response) {
// check if an error msg was received from the rpc server // check if an error msg was received from the rpc server
if (response == 'There was an error. Check your console.') if (response == 'There was an error. Check your console.')
return cb(null); return cb(null);
else else
return cb(response); return cb(response);
}); });
} else {
var uri = base_url + 'getmasternodecount';
request({uri: uri, json: true}, function (error, response, body) {
// check if an error msg was received from the web api server
if (body == 'There was an error. Check your console.')
return cb(null);
else
return cb(body);
});
}
} else { } else {
var uri = base_url + 'getmasternodecount'; // cmd not in use. return null.
return cb(null);
request({uri: uri, json: true}, function (error, response, body) {
// check if an error msg was received from the web api server
if (body == 'There was an error. Check your console.')
return cb(null);
else
return cb(body);
});
} }
} else { } else {
// cmd not in use. return null. // cmd not in use. return null.
+720 -690
View File
File diff suppressed because it is too large Load Diff
+17 -16
View File
@@ -2,7 +2,24 @@ extends layout
block content block content
include ./includes/common.pug include ./includes/common.pug
script.
$(document).ready(function() {
if ('#{settings.richlist_page.page_header.show_last_updated}' == 'true') {
var lastUpdatedDate = #{(last_updated == null || last_updated == '0' ? 0 : last_updated)};
if (lastUpdatedDate != 0) {
$('span#lastUpdatedDate').html(' ' + format_unixtime(lastUpdatedDate));
if (#{settings.shared_pages.date_time.enable_alt_timezone_tooltips} == true) {
$('span#lastUpdatedDate').attr('data-bs-toggle', 'tooltip').attr('data-bs-placement', 'auto').attr('title', format_unixtime(lastUpdatedDate, true));
enableTooltips();
}
} else
$('span#lastUpdatedDate').html(' N/A');
}
if (#{settings.shared_pages.page_header.page_title_image.enable_animation} == true && #{settings.richlist_page.page_header.show_img} == true)
startRotateElement('img#header-img');
});
if settings.richlist_page.wealth_distribution.show_distribution_chart == true if settings.richlist_page.wealth_distribution.show_distribution_chart == true
script. script.
$(document).ready(function() { $(document).ready(function() {
@@ -42,22 +59,6 @@ block content
} }
} }
}); });
if ('#{settings.richlist_page.page_header.show_last_updated}' == 'true') {
var lastUpdatedDate = #{(last_updated == null || last_updated == '0' ? 0 : last_updated)};
if (lastUpdatedDate != 0) {
$('span#lastUpdatedDate').html(' ' + format_unixtime(lastUpdatedDate));
if (#{settings.shared_pages.date_time.enable_alt_timezone_tooltips} == true) {
$('span#lastUpdatedDate').attr('data-bs-toggle', 'tooltip').attr('data-bs-placement', 'auto').attr('title', format_unixtime(lastUpdatedDate, true));
enableTooltips();
}
} else
$('span#lastUpdatedDate').html(' N/A');
}
if (#{settings.shared_pages.page_header.page_title_image.enable_animation} == true && #{settings.richlist_page.page_header.show_img} == true)
startRotateElement('img#header-img');
}); });
- var theadClasses = []; - var theadClasses = [];
if settings.shared_pages.table_header_bgcolor != null && settings.shared_pages.table_header_bgcolor != '' if settings.shared_pages.table_header_bgcolor != null && settings.shared_pages.table_header_bgcolor != ''