lichess_widgets/lichess_widgets.js

84 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-11-07 07:24:43 -08:00
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
2014-11-07 07:06:36 -08:00
var lichess = {};
var serial = 0;
2014-11-07 07:40:39 -08:00
lichess.__make_online = function(id) {
$(id).addClass("lichess_online");
$(id + " > img").attr("src", "http://rubenwardy.github.io/lichess_widgets/lichess_online.png");
}
2014-11-07 07:06:36 -08:00
lichess.challenge_me = function(author, text) {
2014-11-07 07:40:39 -08:00
serial++;
var id = serial;
2014-11-07 07:06:36 -08:00
if (text == null)
text = author;
2014-11-07 07:40:39 -08:00
var tmp = "<a id=\"lichess_widget_" + id + "\" class=\"lichess_widget\" href=\"http://lichess.org/@/" + author + "\">";
2014-11-07 07:06:36 -08:00
tmp += "<img src=\"http://en.lichess.org/assets/images/favicon-32-white.png\" alt=\"lichess\" />"
tmp += "<span>" + text + "</span></a>";
document.write(tmp);
2014-11-07 07:40:39 -08:00
$.ajax({
url: "http://en.lichess.org/api/user/" + author,
dataType: "jsonp",
jsonp: "callback",
success: function(data) {
if (data.online)
lichess.__make_online("#lichess_widget_" + id);
}
});
2014-11-07 07:06:36 -08:00
};
lichess.all_scores = function(author, text) {
serial++;
var id = serial;
if (text == undefined)
text = author;
var tmp = "<a id=\"lichess_widget_" + id + "\" class=\"lichess_widget\" href=\"http://lichess.org/@/" + author + "\">";
tmp += "<img src=\"http://en.lichess.org/assets/images/favicon-32-white.png\" alt=\"lichess\" />"
tmp += "<span>Loading...</span></a>";
document.write(tmp);
$.ajax({
url: "http://en.lichess.org/api/user/" + author,
dataType: "jsonp",
jsonp: "callback",
success: function(data) {
2014-11-07 07:40:39 -08:00
if (data.online)
lichess.__make_online("#lichess_widget_" + id);
2014-11-07 07:06:36 -08:00
if (text && text != "")
text = text + " | ";
$("#lichess_widget_" + id + " > span").text(text + "Classical: " + data.perfs.classical.rating + " | Bullet: " + data.perfs.bullet.rating);
}
});
};
2014-11-07 07:24:43 -08:00
lichess.long_details = function(author, text) {
2014-11-07 07:06:36 -08:00
serial++;
var id = serial;
2014-11-07 07:24:43 -08:00
if (text == undefined)
text = author + " on Lichess";
var tmp = "<a id=\"lichess_widget_" + id + "\" class=\"lichess_widget lichess_long\" href=\"http://lichess.org/@/" + author + "\">";
tmp += "<img src=\"http://en.lichess.org/assets/images/favicon-32-white.png\" alt=\"lichess\" />" + text + "<hr />"
2014-11-07 07:06:36 -08:00
tmp += "<span>One: two<br />Three: four</span></a>";
document.write(tmp);
$.ajax({
url: "http://en.lichess.org/api/user/" + author,
dataType: "jsonp",
jsonp: "callback",
success: function(data) {
2014-11-07 07:40:39 -08:00
if (data.online)
lichess.__make_online("#lichess_widget_" + id);
2014-11-07 07:24:43 -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 += key.capitalize() + " rating: " + data.perfs[key].rating;
}
}
$("#lichess_widget_" + id + " > span").html(res);
2014-11-07 07:06:36 -08:00
}
});
};