var lichess_widgets = (function() {
var callback_serials = 0;
function fetchJson(url, callback) {
callback_serials++;
var script = document.createElement('script');
lichess_widgets.callbacks["cb" + callback_serials] = callback;
script.src = url + "?callback=lichess_widgets.callbacks.cb" + callback_serials;
document.body.appendChild(script);
}
function make_online(id) {
var ele = document.getElementById(id);
ele.className = ele.className + " lichess_online";
ele.getElementsByTagName('img')[0].src = "http://rubenwardy.github.io/lichess_widgets/lichess_online.png";
}
function capitalize(inp) {
return inp.charAt(0).toUpperCase() + inp.slice(1);
}
var serial = 0;
return {
callbacks: {},
profile: function(theme, author, text) {
serial++;
var id = serial;
if (text == null)
text = author;
var tmp = "";
tmp += "
"
tmp += "" + text + "";
document.write(tmp);
fetchJson("http://en.lichess.org/api/user/" + author,function(data) {
if (data.online)
make_online("lichess_widget_" + id);
});
},
profile_scores: function(theme, author, text) {
serial++;
var id = serial;
if (text == undefined)
text = author;
var tmp = "";
tmp += "
"
tmp += "" + text + "";
document.write(tmp);
fetchJson("http://en.lichess.org/api/user/" + author, function(data) {
if (data.online)
make_online("lichess_widget_" + id);
if (text && text != "")
text = text + " | ";
var res = text + "Classical " + data.perfs.classical.rating;
res += " | Bullet " + data.perfs.bullet.rating + "";
document.getElementById("lichess_widget_" + id).getElementsByTagName('span')[0].innerHTML = res;
});
},
profile_big: function(theme, author, text) {
serial++;
var id = serial;
if (text == undefined)
text = author + " on Lichess";
var tmp = "";
tmp += "
" + text + "
"
tmp += "";
document.write(tmp);
fetchJson("http://en.lichess.org/api/user/" + author, function(data) {
if (data.online)
make_online("lichess_widget_" + id);
var res = "";
for (var key in data.perfs) {
if (data.perfs.hasOwnProperty(key) && data.perfs[key].games > 0) {
if (res!="")
res += "
";
res += capitalize(key) + " " + data.perfs[key].rating + " / " + data.perfs[key].games + " Games";
}
}
document.getElementById("lichess_widget_" + id).getElementsByTagName('span')[0].innerHTML = res;
});
}
}
})();