lichess_widgets/lichess_widgets.js

89 lines
3.2 KiB
JavaScript
Raw Normal View History

2014-11-11 09:56:39 -08:00
var lichess_widgets = (function() {
2014-11-28 09:24:28 -08:00
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);
}
2014-11-07 09:10:07 -08:00
function make_online(id) {
2014-11-28 09:24:28 -08:00
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";
2014-11-07 09:10:07 -08:00
}
2014-11-28 09:24:28 -08:00
2014-11-07 09:10:07 -08:00
function capitalize(inp) {
return inp.charAt(0).toUpperCase() + inp.slice(1);
}
2014-11-28 09:24:28 -08:00
2014-11-07 09:10:07 -08:00
var serial = 0;
return {
2014-11-28 09:24:28 -08:00
callbacks: {},
2014-11-07 09:10:07 -08:00
profile: function(theme, author, text) {
serial++;
var id = serial;
if (text == null)
text = author;
var tmp = "<a id=\"lichess_widget_" + id + "\" class=\"lichess_widget lichess_theme_" + theme;
tmp += "\" href=\"http://lichess.org/@/" + author + "\">";
tmp += "<img src=\"http://lichess1.org/assets/images/favicon-32-white.png\" alt=\"lichess\" />"
tmp += "<span>" + text + "</span></a>";
document.write(tmp);
2014-11-28 09:24:28 -08:00
fetchJson("http://en.lichess.org/api/user/" + author,function(data) {
if (data.online)
make_online("lichess_widget_" + id);
2014-11-07 09:10:07 -08:00
});
},
profile_scores: function(theme, author, text) {
serial++;
var id = serial;
if (text == undefined)
text = author;
var tmp = "<a id=\"lichess_widget_" + id + "\" class=\"lichess_widget lichess_theme_" + theme;
tmp += "\" href=\"http://lichess.org/@/" + author + "\">";
tmp += "<img src=\"http://lichess1.org/assets/images/favicon-32-white.png\" alt=\"lichess\" />"
tmp += "<span>" + text + "</span></a>";
document.write(tmp);
2014-11-28 09:24:28 -08:00
fetchJson("http://en.lichess.org/api/user/" + author, function(data) {
if (data.online)
make_online("lichess_widget_" + id);
2014-11-07 07:40:39 -08:00
2014-11-28 09:24:28 -08:00
if (text && text != "")
text = text + " | ";
var res = text + "Classical <b>" + data.perfs.classical.rating;
res += "</b> | Bullet <b>" + data.perfs.bullet.rating + "</b>";
document.getElementById("lichess_widget_" + id).getElementsByTagName('span')[0].innerHTML = res;
2014-11-07 09:10:07 -08:00
});
},
profile_big: function(theme, author, text) {
serial++;
var id = serial;
if (text == undefined)
text = author + " on Lichess";
2014-11-28 09:24:28 -08:00
var tmp = "<a id=\"lichess_widget_" + id + "\" class=\"lichess_widget lichess_theme_" + theme;
tmp += " lichess_long\" href=\"http://lichess.org/@/" + author + "\">";
2014-11-07 09:10:07 -08:00
tmp += "<img src=\"http://lichess1.org/assets/images/favicon-32-white.png\" alt=\"lichess\" />" + text + "<hr />"
tmp += "<span></span></a>";
document.write(tmp);
2014-11-28 09:24:28 -08:00
fetchJson("http://en.lichess.org/api/user/" + author, function(data) {
if (data.online)
make_online("lichess_widget_" + id);
2014-11-07 07:40:39 -08:00
2014-11-28 09:24:28 -08:00
var res = "";
for (var key in data.perfs) {
if (data.perfs.hasOwnProperty(key) && data.perfs[key].games > 0) {
if (res!="")
res += "<br />";
res += capitalize(key) + " <b>" + data.perfs[key].rating + "</b> / " + data.perfs[key].games + " Games";
2014-11-07 09:10:07 -08:00
}
2014-11-07 07:24:43 -08:00
}
2014-11-28 09:24:28 -08:00
document.getElementById("lichess_widget_" + id).getElementsByTagName('span')[0].innerHTML = res;
2014-11-07 09:10:07 -08:00
});
2014-11-07 07:06:36 -08:00
}
2014-11-07 09:10:07 -08:00
}
})();