Discuss Scratch
- Discussion Forums
- » Developing Scratch Extensions
- » Help with my extension
- Zatnik
-
100+ posts
Help with my extension
The function from Stack Overflow was specifically for SoundCloud (although you don't seem to be using getJSONP correctly anyway). For your extension, you should probably just use XMLHttpRequest to get the JSON, and then JSON.parse it. You'll have to have “R” as the block type instead of “r”, which will let you use a callback to return the data (note that it would be a good idea to make it call the callback even if the request errors, or if it returns nothing). Also, you're using switch wrong; your current switch statement says “switch(profile)”, which checks to see whether profile is equal to ‘About’, ‘Country’, or ‘What I\’m working on'. Since you actually want to check the “stuff” parameter, it should be “switch(stuff)” instead.
I don't know how much sense that explanation made, but it should work to change the block type to “R” and replace ext.userid with something like this:
There might be a better way, but this seems to work pretty well.
I don't know how much sense that explanation made, but it should work to change the block type to “R” and replace ext.userid with something like this:
function(stuff, user, callback) {
var jsonurl = 'https://scratch.mit.edu/api/v1/user/' + user + '/?format=json';
var r = new XMLHttpRequest();
r.addEventListener("load", function() {
if (r.responseText) {
var obj = JSON.parse(r.responseText);
var profile = obj.userprofile;
switch(stuff) {
case 'About':
callback(profile.bio);
break;
case 'Country':
callback(profile.country);
break;
case 'What I\'m working on':
callback(profile.status);
}
} else {
callback("");
}
});
r.addEventListener("error", function() {callback("")});
r.open("get", jsonurl, true);
r.send();
}
There might be a better way, but this seems to work pretty well.
Last edited by Zatnik (Dec. 26, 2015 19:40:04)
- Zatnik
-
100+ posts
Help with my extension
Sorry, yeah, you do still need to set ext.userid to the function–when I said “replace ext.userid,” I just meant that you should replace the function itself, not the property that you're storing it in.
It looks like the problem with your file now is that you left out the part that sets jsonurl.
It looks like the problem with your file now is that you left out the part that sets jsonurl.
- Discussion Forums
- » Developing Scratch Extensions
-
» Help with my extension