commit 57427de6b26c9cecb4106f338222d4e3a6bb5677 Author: Sayamindu Dasgupta Date: Sat May 17 00:07:40 2014 -0400 Initial import of sample extensions diff --git a/alarm_extension.js b/alarm_extension.js new file mode 100644 index 0000000..89239d6 --- /dev/null +++ b/alarm_extension.js @@ -0,0 +1,44 @@ +/* Extension demonstrating a hat block */ +/* Sayamindu Dasgupta , May 2014 */ + +new (function() { + var ext = this; + var alarm_went_off = false; // This becomes true after the alarm goes off + + // Cleanup function when the extension is unloaded + ext._shutdown = function() {}; + + // Status reporting code + // Use this to report missing hardware, plugin or unsupported browser + ext._getStatus = function() { + return {status: 2, msg: 'Ready'}; + }; + + ext.set_alarm = function(time) { + window.setTimeout(function() { + alarm_went_off = true; + }, time*1000); + }; + + ext.when_alarm = function() { + // Reset alarm_went_off if it is true, and return true + // otherwise, return false. + if (alarm_went_off === true) { + alarm_went_off = false; + return true; + } + + return false; + }; + + // Block and block menu descriptions + var descriptor = { + blocks: [ + ['', 'run alarm after %n seconds', 'set_alarm', '2'], + ['h', 'when alarm goes off', 'when_alarm'], + ] + }; + + // Register the extension + ScratchExtensions.register('Alarm extension', descriptor, ext); +})(); \ No newline at end of file diff --git a/localstorage_extension.js b/localstorage_extension.js new file mode 100644 index 0000000..2f42433 --- /dev/null +++ b/localstorage_extension.js @@ -0,0 +1,46 @@ +/* Extension using the JavaScript localStorage API */ +/* Sayamindu Dasgupta , April 2014 */ + +new (function() { + var ext = this; + + ext.set_localstorage = function (data) { + localStorage.setItem(app.projectModel.id.toString(), data); + }; + + ext.change_localstorage = function(change) { + var data = localStorage.getItem(app.projectModel.id.toString()); + if (!isNaN(parseFloat(data))) { + localStorage.setItem(app.projectModel.id.toString(), parseFloat(data) + change); + } + }; + + ext.get_localstorage = function () { + return localStorage.getItem(app.projectModel.id.toString()); + }; + + ext._shutdown = function() {}; + + + ext._getStatus = function() { + var test = 'test'; + try { + localStorage.setItem(test, test); + localStorage.removeItem(test); + } catch(e) { + return {status: 1, msg: 'Your browser does not support the localStorage API'}; + } + + return {status: 2, msg: 'Ready'}; + }; + + var descriptor = { + blocks: [ + ['', 'set local data to %s', 'set_localstorage', '0'], + ['', 'change local data by %d', 'change_localstorage', 1], + ['r', 'local data', 'get_localstorage'], + ], + }; + + ScratchExtensions.register('Local Storage', descriptor, ext); +})(); \ No newline at end of file diff --git a/power_extension.js b/power_extension.js new file mode 100644 index 0000000..f4b3870 --- /dev/null +++ b/power_extension.js @@ -0,0 +1,30 @@ +/* Extension demonstrating a reporter block */ +/* Sayamindu Dasgupta , May 2014 */ + +new (function() { + var ext = this; + + // Cleanup function when the extension is unloaded + ext._shutdown = function() {}; + + // Status reporting code + // Use this to report missing hardware, plugin or unsupported browser + ext._getStatus = function() { + return {status: 2, msg: 'Ready'}; + }; + + ext.power = function(base, exponent) { + return Math.pow(base, exponent); + }; + + // Block and block menu descriptions + var descriptor = { + blocks: [ + // Block type, block name, function name, param1 default value, param2 default value + ['r', '%n ^ %n', 'power', 2, 3], + ] + }; + + // Register the extension + ScratchExtensions.register('Sample extension', descriptor, ext); +})(); \ No newline at end of file diff --git a/random_wait_extension.js b/random_wait_extension.js new file mode 100644 index 0000000..23b5f71 --- /dev/null +++ b/random_wait_extension.js @@ -0,0 +1,36 @@ +/* Extension demonstrating a blocking command block */ +/* Sayamindu Dasgupta , May 2014 */ + +new (function() { + var ext = this; + + // Cleanup function when the extension is unloaded + ext._shutdown = function() {}; + + // Status reporting code + // Use this to report missing hardware, plugin or unsupported browser + ext._getStatus = function() { + return {status: 2, msg: 'Ready'}; + }; + + // Functions for block with type 'w' will get a callback function as the + // final argument. This should be called to indicate that the block can + // stop waiting. + ext.wait_random = function(callback) { + wait = Math.random(); + console.log('Waiting for ' + wait + ' seconds'); + window.setTimeout(function() { + callback(); + }, wait*1000); + }; + + // Block and block menu descriptions + var descriptor = { + blocks: [ + ['w', 'wait for random time', 'wait_random'], + ] + }; + + // Register the extension + ScratchExtensions.register('Random wait extension', descriptor, ext); +})(); \ No newline at end of file diff --git a/speech_to_text_extension.js b/speech_to_text_extension.js new file mode 100644 index 0000000..38fb956 --- /dev/null +++ b/speech_to_text_extension.js @@ -0,0 +1,39 @@ +/* Extension using the JavaScript Speech API for speech to text */ +/* Sayamindu Dasgupta , April 2014 */ + +new (function() { + var ext = this; + + var recognized_speech = ''; + + ext.recognize_speech = function (callback) { + var recognition = new webkitSpeechRecognition(); + recognition.onresult = function(event) { + if (event.results.length > 0) { + recognized_speech = event.results[0][0].transcript; + if (typeof callback=="function") callback(); + } + }; + recognition.start(); + }; + + ext.recognized_speech = function () {return recognized_speech;}; + + ext._shutdown = function() {}; + + ext._getStatus = function() { + if (window.webkitSpeechRecognition === undefined) { + return {status: 1, msg: 'Your browser does not support speech recognition. Try using Google Chrome.'}; + } + return {status: 2, msg: 'Ready'}; + }; + + var descriptor = { + blocks: [ + ['w', 'wait and recognize speech', 'recognize_speech'], + ['r', 'recognized speech', 'recognized_speech'] + ], + }; + + ScratchExtensions.register('Speech To Text', descriptor, ext); +})(); \ No newline at end of file diff --git a/text_to_speech_extension.js b/text_to_speech_extension.js new file mode 100644 index 0000000..fd26964 --- /dev/null +++ b/text_to_speech_extension.js @@ -0,0 +1,51 @@ +/* Extension using the JavaScript Speech API for text to speech */ +/* Sayamindu Dasgupta , April 2014 */ + +new (function() { + var ext = this; + + /*function _get_voices() { + var ret = []; + var voices = speechSynthesis.getVoices(); + + for(var i = 0; i < voices.length; i++ ) { + ret.push(voices[i].name); + console.log(voices.toString()); + } + + return ret; + } + + ext.set_voice = function() { + };*/ + + ext.speak_text = function (text, callback) { + var u = new SpeechSynthesisUtterance(text.toString()); + u.onend = function(event) { + if (typeof callback=="function") callback(); + }; + + speechSynthesis.speak(u); + }; + + ext._shutdown = function() {}; + + ext._getStatus = function() { + if (window.SpeechSynthesisUtterance === undefined) { + return {status: 1, msg: 'Your browser does not support text to speech. Try using Google Chrome or Safari.'}; + } + return {status: 2, msg: 'Ready'}; + }; + + var descriptor = { + blocks: [ + //['', 'set voice to %m.voices', 'set_voice', ''], + ['w', 'speak %s', 'speak_text', 'Hello!'], + ], + /*menus: { + voices: _get_voices(), + },*/ + }; + + ScratchExtensions.register('Text to Speech', descriptor, ext); +})(); \ No newline at end of file diff --git a/text_to_speech_simple_extension.js b/text_to_speech_simple_extension.js new file mode 100644 index 0000000..fd02d0f --- /dev/null +++ b/text_to_speech_simple_extension.js @@ -0,0 +1,30 @@ +/* Extension demonstrating a simple version of the Text to Speech block */ +/* Sayamindu Dasgupta , May 2014 */ + +new (function() { + var ext = this; + + // Cleanup function when the extension is unloaded + ext._shutdown = function() {}; + + // Status reporting code + // Use this to report missing hardware, plugin or unsupported browser + ext._getStatus = function() { + return {status: 2, msg: 'Ready'}; + }; + + ext.speak = function(text) { + msg = new SpeechSynthesisUtterance(text); + window.speechSynthesis.speak(msg); + }; + + // Block and block menu descriptions + var descriptor = { + blocks: [ + ['', 'speak %s', 'speak', "Hello!"], + ] + }; + + // Register the extension + ScratchExtensions.register('Simple text to speech extension', descriptor, ext); +})(); \ No newline at end of file diff --git a/weather_extension.js b/weather_extension.js new file mode 100644 index 0000000..d530045 --- /dev/null +++ b/weather_extension.js @@ -0,0 +1,39 @@ +/* Extension demonstrating a blocking reporter block */ +/* Sayamindu Dasgupta , May 2014 */ + + +new (function() { + var ext = this; + + // Cleanup function when the extension is unloaded + ext._shutdown = function() {}; + + // Status reporting code + // Use this to report missing hardware, plugin or unsupported browser + ext._getStatus = function() { + return {status: 2, msg: 'Ready'}; + }; + + ext.get_temp = function(location, callback) { + // Make an AJAX call to the Open Weather Maps API + $.ajax({ + url: 'http://api.openweathermap.org/data/2.5/weather?q='+location+'&units=imperial', + dataType: 'jsonp', + success: function( weather_data ) { + // Got the data - parse it and return the temperature + temperature = weather_data['main']['temp']; + callback(temperature); + } + }); + }; + + // Block and block menu descriptions + var descriptor = { + blocks: [ + ['R', 'current temperature in city %s', 'get_temp', 'Boston, MA'], + ] + }; + + // Register the extension + ScratchExtensions.register('Weather extension', descriptor, ext); +})(); \ No newline at end of file