Skip to content

Instantly share code, notes, and snippets.

@12Me21
Created May 15, 2019 23:46
Show Gist options
  • Save 12Me21/f65fc9ef21f20e0dea5de6210b5faf28 to your computer and use it in GitHub Desktop.
Save 12Me21/f65fc9ef21f20e0dea5de6210b5faf28 to your computer and use it in GitHub Desktop.
var keyregex = /\b3*([A-HJ-NP-TV-Z1-9]{1,8})\b/g;
function sanitize(text){
return text.replace(/&/g,"&amp;").replace(/</g,"&lt;");
}
function size(bytes){
var suffix = "B";
if(bytes/1024 >= 1){
suffix = "KB";
bytes /= 1024;
if(bytes/1024 >= 1){
bytes /= 1024;
suffix = "MiB";
}
}
return bytes.toFixed(1)+" "+suffix;
}
function makeKeyInfo(info){
console.log(info);
if (info == null || !info.available) {
return "<h1>" + sanitize(key) + " is not available.</h1><br/><br/>";
}
var res = "";
var t = info.filename[0];
var type = t == "P" ? "Project" : t == "B" ? "DAT" : t == "T" ? "TXT" : "UNK";
res += "<h1>" + sanitize(key) + " (" + sanitize(info.filename.substr(1)) + ")</h1>";
res += "<ol>";
res += "<li><b>Type</b>: " + sanitize(type) + "</li>";
res += "<li><b>Size</b>: " + sanitize(size(info.size)) + "</li>";
res += "<li><b>Author</b>: " + sanitize(info.author.name) + "</li>";
res += "<li><b>Uploaded</b>: " + sanitize(new Date(info.uploaded).toLocaleString()) + "</li>";
res += "<li><b>Downloads</b>: " + sanitize(info.downloads.toString()) + "</li>";
var baseURL = "https://sbapi.me/get/" + key;
switch (type) {
case "TXT":
res += "<li><b>Links</b>: <a href=\"" + baseURL + "/text\">text</a> <a href=\"" + baseURL + "/code\">code</a></li>";
break;
case "DAT":
res += "<li><b>Data type</b>: " + sanitize(info.extInfo.type) + "</li>";
res += "<li><b>Dimensions</b>: " + sanitize(info.extInfo.dims.join("x")) + "</li>";
if (info.extInfo.dims.length <= 2) {
res += "<li><b>Links</b>: <a href=\"" + baseURL + "/csv\">csv</a>";
if (info.extInfo.dims.length == 2 && info.extInfo.type == "col") {
res += " <a href=\"" + baseURL + "/png\">png</a>";
}
res += "</li>";
}
break;
case "Project":
var filelist = info.extInfo.files;
res += "<li><b>Files</b>: <ol>";
for(var i=0;i<filelist.length;i++){
var file=filelist[i];
res += "<li><a href=\"" + baseURL + "/" + sanitize(file.name) + "/info\">" + sanitize(file.name.substr(1)) + "</a> (" + sanitize(size(file.size)) + ")</li>";
}
res += "</ol></li>";
res += "<li><b>Links</b>: <a href=\"" + baseURL + "/zip\">zip</a></li>";
break;
}
res += "</ol><br>";
return res;
}
function genericXHRSimple2_JSON(url,type,callback,arg){
var xhr = new XMLHttpRequest();
xhr.open(type, url);
xhr.onload=function(event){
//alert("request finishjed");
try{
callback(JSON.parse(event.target.response),arg);
}catch(e){
console.log("Oops, XHR callback didn't work. Dumping exception");
console.log(e);
callback(null,arg);
}
};
xhr.send();
}
if (location.pathname == "/page") {
var keys = document.getElementsByClassName("publickey");
var content = document.querySelector("page-area");
var lists = document.querySelector("dl.lists");
if (keys.length > 0) {
var key = keys[0].textContent.toUpperCase();
var extractedKeys = [];
var t = null;
while ((t = keyregex.exec(key))) {
if (t[1] != "DATA" || t[1] != "GAME")
extractedKeys.push(t[1]);
}
var infobox = document.createElement("text-block");
infobox.className = "bbcode";
infobox.innerHTML = "<text-title>SBAPI Information</text-title><text-content>Loading, could take up to 1 minute...</text-content>";
content.insertBefore(infobox, lists);
var cont = document.createElement("text-content");
var infos = {};
for (var i = 0; i < extractedKeys.length; i++) {
var key = extractedKeys[i];
genericXHRSimple2_JSON("http://sbapi.me/get/"+key+"/info?json=1","GET",function(response,arg){
cont.innerHTML += makeKeyInfo(response);
if(arg == extractedKeys.length-1){
//last item
infobox.innerHTML = "<text-title>SBAPI Information</text-title>";
infobox.appendChild(cont);
}
}, i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment