mirror of
https://github.com/scratchfoundation/scratch-extension-docs.git
synced 2024-11-23 07:58:01 -05:00
Initial import of sample extensions
This commit is contained in:
commit
57427de6b2
8 changed files with 315 additions and 0 deletions
44
alarm_extension.js
Normal file
44
alarm_extension.js
Normal file
|
@ -0,0 +1,44 @@
|
|||
/* Extension demonstrating a hat block */
|
||||
/* Sayamindu Dasgupta <sayamindu@media.mit.edu>, 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);
|
||||
})();
|
46
localstorage_extension.js
Normal file
46
localstorage_extension.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
/* Extension using the JavaScript localStorage API */
|
||||
/* Sayamindu Dasgupta <sayamindu@media.mit.edu>, 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);
|
||||
})();
|
30
power_extension.js
Normal file
30
power_extension.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
/* Extension demonstrating a reporter block */
|
||||
/* Sayamindu Dasgupta <sayamindu@media.mit.edu>, 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);
|
||||
})();
|
36
random_wait_extension.js
Normal file
36
random_wait_extension.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
/* Extension demonstrating a blocking command block */
|
||||
/* Sayamindu Dasgupta <sayamindu@media.mit.edu>, 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);
|
||||
})();
|
39
speech_to_text_extension.js
Normal file
39
speech_to_text_extension.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
/* Extension using the JavaScript Speech API for speech to text */
|
||||
/* Sayamindu Dasgupta <sayamindu@media.mit.edu>, 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);
|
||||
})();
|
51
text_to_speech_extension.js
Normal file
51
text_to_speech_extension.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
/* Extension using the JavaScript Speech API for text to speech */
|
||||
/* Sayamindu Dasgupta <sayamindu@media.mit.edu>, 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);
|
||||
})();
|
30
text_to_speech_simple_extension.js
Normal file
30
text_to_speech_simple_extension.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
/* Extension demonstrating a simple version of the Text to Speech block */
|
||||
/* Sayamindu Dasgupta <sayamindu@media.mit.edu>, 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);
|
||||
})();
|
39
weather_extension.js
Normal file
39
weather_extension.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
/* Extension demonstrating a blocking reporter block */
|
||||
/* Sayamindu Dasgupta <sayamindu@media.mit.edu>, 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);
|
||||
})();
|
Loading…
Reference in a new issue