lichess_widgets/lichess_widgets.js

111 lines
3.5 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
function fetchJson(url, callback) {
2018-05-22 10:41:01 -07:00
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(this.responseText);
callback(data);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
var cache = {}
var callbacks = {}
function getAuthor(name, callback) {
if (cache[name]) {
callback(cache[name]);
} else if (callbacks[name]) {
callbacks[name].push(callback);
} else {
callbacks[name] = [ callback ];
fetchJson("https://en.lichess.org/api/user/" + name, function(data) {
cache[name] = data;
for (var i = 0; i < callbacks[name].length; ++i) {
callbacks[name][i](data);
}
callbacks[name] = null;
});
}
2014-11-28 09:24:28 -08:00
}
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";
2017-05-08 16:25:57 -07:00
ele.getElementsByTagName('img')[0].src = "https://rubenwardy.com/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 {
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;
2017-05-08 16:25:57 -07:00
tmp += "\" href=\"https://lichess.org/@/" + author + "\">";
tmp += "<img src=\"https://lichess1.org/assets/images/favicon-32-white.png\" alt=\"lichess\" />"
2014-11-07 09:10:07 -08:00
tmp += "<span>" + text + "</span></a>";
document.write(tmp);
2018-05-22 10:41:01 -07:00
getAuthor(author, function(data) {
2014-11-28 09:24:28 -08:00
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;
2017-05-08 16:25:57 -07:00
tmp += "\" href=\"https://lichess.org/@/" + author + "\">";
tmp += "<img src=\"https://lichess1.org/assets/images/favicon-32-white.png\" alt=\"lichess\" />"
2014-11-07 09:10:07 -08:00
tmp += "<span>" + text + "</span></a>";
document.write(tmp);
2018-05-22 10:41:01 -07:00
getAuthor(author, function(data) {
2014-11-28 09:24:28 -08:00
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
});
},
2018-05-22 10:41:01 -07:00
profile_big: function(theme, author, text) {
2014-11-07 09:10:07 -08:00
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;
2017-05-08 16:25:57 -07:00
tmp += " lichess_long\" href=\"https://lichess.org/@/" + author + "\">";
tmp += "<img src=\"https://lichess1.org/assets/images/favicon-32-white.png\" alt=\"lichess\" />" + text + "<hr />"
2014-11-07 09:10:07 -08:00
tmp += "<span></span></a>";
document.write(tmp);
2018-05-22 10:41:01 -07:00
getAuthor(author, function(data) {
2014-11-28 09:24:28 -08:00
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) {
2018-05-22 10:41:01 -07:00
if (data.perfs.hasOwnProperty(key) && data.perfs[key].games > 0) {
2014-11-28 09:24:28 -08:00
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
}
})();