Skip to content

Instantly share code, notes, and snippets.

@lantto
Last active August 14, 2016 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lantto/2e56e6820c3431f94ab7aac28156be43 to your computer and use it in GitHub Desktop.
Save lantto/2e56e6820c3431f94ab7aac28156be43 to your computer and use it in GitHub Desktop.
Show Steem Power
var isVisible = function(obj) {
return obj.offsetWidth > 0 && obj.offsetHeight > 0;
};
var curry = function(uncurried) {
var parameters = Array.prototype.slice.call(arguments, 1);
return function() {
return uncurried.apply(this, parameters.concat(Array.prototype.slice.call(arguments, 0)));
};
};
var getAndAddToCallbacks = function(socket, callbacks, method, params, callback) {
callbacks.push(callback);
var data = {id: callbacks.length - 1, method: method, params: params};
socket.send(JSON.stringify(data));
};
var runFun = function(funs, id, data) {
funs[id](data);
};
var socket = new WebSocket('wss://steemit.com/wspa'),
callbacks = [];
var getResult = curry(getAndAddToCallbacks, socket, callbacks),
runCallback = curry(runFun, callbacks);
socket.onopen = function(event) {
getResult('get_dynamic_global_properties', [], function(data) {
var totalVestingShares = data.result.total_vesting_shares.split(' ')[0],
totalVestingFundSteem = data.result.total_vesting_fund_steem.split(' ')[0];
var nodes = [].slice.call(document.querySelectorAll('[itemprop=author] strong')).reduce(function(nodes, node) {
if (!isVisible(node) || node.ssp__hasSp) {
return nodes;
}
nodes.push(node);
return nodes;
}, []);
var accounts = nodes.map(function(node) {
return node.innerHTML;
});
getResult('get_accounts', [accounts], function(data) {
data.result.forEach(function(account, i) {
var vestingShares = account.vesting_shares.split(' ')[0];
var steemPower = totalVestingFundSteem * (vestingShares / totalVestingShares);
console.log(account.name + ' ' + steemPower);
nodes[i].innerHTML = '' + account.name + ' <span style="font-size: 12px; font-weight: normal;">[' + Math.round(steemPower) + ' SP]</span>';
nodes[i].ssp__hasSp = true;
});
socket.close();
});
});
};
socket.onmessage = function(event) {
var data = JSON.parse(event.data);
runCallback(data.id, data);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment