Skip to content

Instantly share code, notes, and snippets.

@ajc2
Created September 16, 2019 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajc2/a526e501d49783763d0dbc3ad204ff69 to your computer and use it in GitHub Desktop.
Save ajc2/a526e501d49783763d0dbc3ad204ff69 to your computer and use it in GitHub Desktop.
Fixed sbapi-sbs.js for SB4 mincompat check
// SBAPI for SBS
// By 12Me21 and MasterR3C0RD, 2019
// sb4 key fix by snail_
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(info.filename.substr(1)) + " (" + sanitize(key) + ")</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");
// detect sb4 key
var isswitch = document.getElementsByTagName("supported-system")[0].textContent.includes("Switch");
if (keys.length > 0) {
// create sbapi infobox
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);
// fail if switch key
if(isswitch) {
infobox.innerHTML = "<text-title>SBAPI Information</text-title><text-content>SBAPI unsupported for Switch.</text-content>";
}
else {
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 cont = document.createElement("text-content");
var infos = {};
for (var i = 0; i < extractedKeys.length; i++) {
var key = extractedKeys[i];
genericXHRSimple2_JSON("//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