Skip to content

Instantly share code, notes, and snippets.

@LinusU
Last active December 28, 2015 04:59
Show Gist options
  • Save LinusU/7446896 to your computer and use it in GitHub Desktop.
Save LinusU/7446896 to your computer and use it in GitHub Desktop.
Eligius javascript api, backed by jsonp.
(function (window, document) {
'use strict';
var api = function (cmd, opts, cb) {
opts.cmd = cmd;
var id = 'APICB' + Math.round(Math.random() * 1e9),
url = 'http://eligius.st/~wizkid057/newstats/api.php',
head = document.querySelector('head'),
script = document.createElement('script'),
qs = Object.keys(opts).map(function (key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(opts[key]);
}).join('&');
window[id] = function (data) {
delete window[id];
head.removeChild(script);
if (data.error) {
cb(new Error(data.error));
} else if (data.cmd !== opts.cmd) {
cb(new Error('Server returned wrong command'));
} else {
cb(null, data.output);
}
};
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', url + '?' + qs + '&format=jsonp&callback=' + id);
script.addEventListener('error', function () {
window[id]({ error: 'Network error' });
});
head.appendChild(script);
};
api.getacceptedcount = function (opts, cb) { api('getacceptedcount', opts, cb); };
api.getuseroptions = function (opts, cb) { api('getuseroptions', opts, cb); };
api.gethashrate = function (opts, cb) { api('gethashrate', opts, cb); };
api.getuserstat = function (opts, cb) { api('getuserstat', opts, cb); };
window.Eligius = api;
}(this, this.document));
var u = prompt('What is you username?');
Eligius.gethashrate({ username: u }, function (err, data) {
if (err) {
alert('Something went wrong!\n\n(' + err.message + ')');
} else {
alert('Your average hashing rate is: ' + data.av256.pretty);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment