Scratch Project Loader

by frogggeee McFrogggeee

HTML

<body onload="getJSON();">
    <div style='font-weight:bold;font-size:28pt' id='title'>Getting JSON</div>
    <div id='info'></div>
</body>

CSS

span.gry { color: #888; position:absolute; left: 220px; }
div#title>img { height:2em;float:left;padding:6px 16px 0 0; }
div#info { clear:both; }
div.vfnd { color: #f88; padding-left:32px; }
span.prm { color: #88f; }

JavaScript

var projectNo = '48600850';    // 10128407 is paper minecraft
var scan_variable = 'ty';
getJSON();
/*
Hey guys! :) - Did you know that the Scratch Web Site makes available a number of APIs (API means Application Programming Interfaces) to retrieve scratch projects and information about them using standard web requests? This is a little javascript code that requests information on a project from Scratch and displays it in the pane to the right!

http://wiki.scratch.mit.edu/wiki/Scratch_File_Format_(2.0)
http://projects.scratch.mit.edu/internalapi/project/10128407/get/
http://scratch.mit.edu/api/v1/project/10128407/?format=json
*/

function getJSON() {
    $.get('https://scratch.mit.edu/api/v1/project/' + projectNo + '/?format=json', processInfo).fail(
		function( error ) { console.log(error); }
	);
    
	$.get('https://projects.scratch.mit.edu/internalapi/project/' + projectNo + '/get/', process).fail(
		function( error ) { console.log(error); }
	);
}

function processInfo( data ) {
	var $t = $('#title');
    $t.html( "<img src='" + data.thumbnail + "'>" + data.title );
    
    $t = $('#info');
    var txt = "Author: <b>" + data.creator.username;
    txt += "</b><br>Shared: <b>" + (data.datetime_shared ? data.datetime_shared : "Not Shared");
    txt += "</b><br>Finding Usage of Variable: <span style='color:#f88'><b>";
    txt += scan_variable + "</b></span>";
    $t.html( txt );
}

function process( data ) {
	var $b = $(document.body);

	var sprites = [];
	var cmds = [];
	var lists = data.lists;

	var os = data.children;
	for (var i=0; i<os.length; i++) {
		var o = os[i];
		if (o.objName) {
			sprites.push( o );
		} else if (o.cmd) {
			cmds.push( o );
		} else if (o.listName) {    // What's this for? Why are all lists repeated in the JSON?
//			lists.push( o );
		} else {
			console.log( o );
		}
	}
	
    // Sprites

	$b.append("<h3>Sprites (" + sprites.length + ")</h3>");
	$b.append('<ul></ul>');
	
	$l = $b.children(':last-child');
	
    var $tmp = $('<div>');
...