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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment