You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.6 KiB
JavaScript
81 lines
1.6 KiB
JavaScript
Zerooo = {};
|
|
|
|
Zerooo.getJSON = function (url, callback) {
|
|
var x = new XMLHttpRequest();
|
|
var callbackSent = false;
|
|
|
|
x.open("GET", url, true);
|
|
x.onerror = function (message, source, lineno, colno, error) {
|
|
if (!callbackSent) {
|
|
sendCallback(error);
|
|
}
|
|
};
|
|
|
|
x.onreadystatechange = function () {
|
|
if (x.readyState != 4) return;
|
|
|
|
if (x.status !== 200) {
|
|
sendCallback(new Error(url + " responded with status code " + x.status));
|
|
return;
|
|
}
|
|
|
|
var data = x.responseText;
|
|
var json = null;
|
|
|
|
try {
|
|
json = JSON.parse(data);
|
|
} catch (e) {
|
|
sendCallback(e)
|
|
}
|
|
|
|
sendCallback(null, json);
|
|
};
|
|
x.send();
|
|
|
|
function sendCallback(err, data)
|
|
{
|
|
if (!callbackSent) {
|
|
callbackSent = true;
|
|
callback(err, data || null);
|
|
}
|
|
}
|
|
};
|
|
|
|
Zerooo.whenReady = function (callback) {
|
|
if (document.readyState == "complete") {
|
|
setTimeout(callback, 0);
|
|
return;
|
|
}
|
|
|
|
document.onreadystatechange = function () {
|
|
if (document.readyState == "complete") {
|
|
callback();
|
|
}
|
|
}
|
|
};
|
|
|
|
Zerooo.select = function (sel) {
|
|
return document.querySelector(sel);
|
|
};
|
|
|
|
Zerooo.selectAll = function (sel) {
|
|
return document.querySelectorAll(sel);
|
|
};
|
|
|
|
Zerooo.joinHumanReadable = function (arr) {
|
|
if (arr.length === 0) {
|
|
return "";
|
|
}
|
|
|
|
if (arr.length === 1) {
|
|
return arr.shift();
|
|
}
|
|
|
|
var str = arr.shift();
|
|
|
|
while (arr.length > 1) {
|
|
str += ', ' + arr.shift();
|
|
}
|
|
|
|
return str + " and " + arr.shift();
|
|
}; |