Skip to content

Instantly share code, notes, and snippets.

@timelsass
Created April 16, 2018 00:20
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 timelsass/a1464a9206a27f2042b08925e8f35c0a to your computer and use it in GitHub Desktop.
Save timelsass/a1464a9206a27f2042b08925e8f35c0a to your computer and use it in GitHub Desktop.
new ( function() {
var ext = this
list = [],
generated = false;
ext._shutdown = function() {};
ext._getStatus = function() {
return {
status: 2,
msg: 'Ready'
};
};
ext.getWord = function( n ) {
$.get( 'http://api.wordnik.com/v4/words.json/randomWords?hasDictionaryDef=true&includePartOfSpeech=noun&minCorpusCount=8000&maxCorpusCount=-1&minDictionaryCount=3&maxDictionaryCount=-1&minLength=6&maxLength=12&api_key=a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5&limit=' + n, function( response ) {
$.each( response, function( index, match ) {
list.push( match.word );
} );
} ).done( function() {
generated = true;
} );
};
ext.wordsGenerated = function() {
if ( generated === true ) {
generated = false;
return true;
}
return false;
};
ext.clearWordList = function() {
list = [];
};
ext.getList = function( index ) {
return list[ index - 1 ];
};
ext.scrambleWord = function( word ) {
var a = word.split(''),
n = word.length;
for ( var i = n - 1; i > 0; i-- ) {
var j = Math.floor( Math.random() * ( i + 1 ) );
var tmp = a[ i ];
a[ i ] = a[ j ];
a[ j ] = tmp;
}
return a.join( '' );
};
// Block and block menu descriptions
var descriptor = {
blocks: [
[ '', 'Get %n words', 'getWord', '3' ],
[ 'h', 'When words generated', 'wordsGenerated' ],
[ 'r', 'Get word %n from list', 'getList', '1' ],
[ '', 'Clear Word List', 'clearWordList' ],
[ 'r', 'Scramble %s word', 'scrambleWord', '' ]
]
};
// Register the extension
ScratchExtensions.register( 'Random words extension', descriptor, ext );
} )();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment