mirror of
https://github.com/scratchfoundation/scratch-extension-docs.git
synced 2024-11-27 01:45:44 -05:00
Redirect traffic to ScratchX
This commit is contained in:
parent
9e1cec37b1
commit
0c2d604be4
12 changed files with 3 additions and 646 deletions
11
README.md
11
README.md
|
@ -1,9 +1,4 @@
|
|||
scratch-extension-docs
|
||||
======================
|
||||
This repository has moved
|
||||
=========================
|
||||
|
||||
Sample code for creating extensions with Scratch 2.0.
|
||||
|
||||
See http://llk.github.io/scratch-extension-docs for documentation.
|
||||
|
||||
Questions? See the Scratch extension developer's forum:
|
||||
http://scratch.mit.edu/discuss/48/
|
||||
Please find [documentation](https://github.com/LLK/scratchx/wiki), [example extensions](https://github.com/LLK/scratchx/wiki/Example-Extensions) and more at the [ScratchX repository](https://github.com/LLK/scratchx/).
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
/* 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);
|
||||
})();
|
|
@ -1,45 +0,0 @@
|
|||
// Scratch Extension to demonstrate some simple web browser functionality
|
||||
// 2014 Shane M. Clements
|
||||
|
||||
(function(ext) {
|
||||
ext.alert = function(message) {
|
||||
alert(message);
|
||||
};
|
||||
|
||||
ext.confirm = function(question) {
|
||||
return confirm(question);
|
||||
};
|
||||
|
||||
ext.ask = function(question) {
|
||||
return prompt(question);
|
||||
};
|
||||
|
||||
ext.setTitle = function(title) {
|
||||
window.document.title = title;
|
||||
};
|
||||
|
||||
ext.openTab = function(location) {
|
||||
window.open(location, '_blank');
|
||||
};
|
||||
|
||||
ext._shutdown = function() {
|
||||
console.log('Shutting down...');
|
||||
};
|
||||
|
||||
ext._getStatus = function() {
|
||||
return {status: 2, msg: 'Ready'};
|
||||
};
|
||||
|
||||
var descriptor = {
|
||||
blocks: [
|
||||
[' ', 'alert %s', 'alert', ''],
|
||||
['r', 'confirm %s', 'confirm', 'Are you sure?'],
|
||||
['r', 'ask %s', 'ask', 'How are you?'],
|
||||
[' ', 'set window title to %s', 'setTitle', 'title'],
|
||||
[' ', 'open tab with %s', 'openTab', 'https://twitter.com/scratchteam']
|
||||
]
|
||||
};
|
||||
|
||||
ScratchExtensions.register('Browser Stuff', descriptor, ext);
|
||||
})({});
|
||||
|
|
@ -1,76 +0,0 @@
|
|||
// joystickExtension.js
|
||||
// Shane M. Clements, November 2013
|
||||
// Joystick Scratch Extension
|
||||
//
|
||||
// This is an extension for development and testing of the Scratch Javascript Extension API.
|
||||
|
||||
new (function() {
|
||||
var device = null;
|
||||
var input = null;
|
||||
var poller = null;
|
||||
var ext = this;
|
||||
|
||||
ext._deviceConnected = function(dev) {
|
||||
if(device) return;
|
||||
|
||||
device = dev;
|
||||
device.open();
|
||||
|
||||
poller = setInterval(function() {
|
||||
input = device.read(48);
|
||||
}, 10);
|
||||
|
||||
// setInterval(function() { console.log(input); }, 100);
|
||||
};
|
||||
|
||||
ext._deviceRemoved = function(dev) {
|
||||
if(device != dev) return;
|
||||
device = null;
|
||||
stopPolling();
|
||||
};
|
||||
|
||||
function stopPolling() {
|
||||
if(poller) clearInterval(poller);
|
||||
poller = null;
|
||||
}
|
||||
|
||||
ext._shutdown = function() {
|
||||
if(poller) clearInterval(poller);
|
||||
poller = null;
|
||||
|
||||
if(device) device.close();
|
||||
device = null;
|
||||
}
|
||||
|
||||
ext._getStatus = function() {
|
||||
if(!device) return {status: 1, msg: 'Controller disconnected'};
|
||||
return {status: 2, msg: 'Controller connected'};
|
||||
}
|
||||
|
||||
// Converts a byte into a value of the range -1 -> 1 with two decimal places of precision
|
||||
function convertByteStr(byte) { return (parseInt(byte, 16) - 128) / 128; }
|
||||
ext.readJoystick = function(name) {
|
||||
var retval = null;
|
||||
switch(name) {
|
||||
case 'leftX': retval = convertByteStr(input[12] + input[13]); break;
|
||||
case 'leftY': retval = -convertByteStr(input[14] + input[15]); break;
|
||||
case 'rightX': retval = convertByteStr(input[16] + input[17]); break;
|
||||
case 'rightY': retval = -convertByteStr(input[18] + input[19]); break;
|
||||
}
|
||||
|
||||
// If it's hardly off center then treat it as centered
|
||||
if(Math.abs(retval) < 0.1) retval = 0;
|
||||
|
||||
return retval.toFixed(2);
|
||||
}
|
||||
|
||||
var descriptor = {
|
||||
blocks: [
|
||||
['r', 'get joystick %m.joystickPart', 'readJoystick', 'leftX']
|
||||
],
|
||||
menus: {
|
||||
joystickPart: ['leftX', 'leftY', 'rightX', 'rightY']
|
||||
}
|
||||
};
|
||||
ScratchExtensions.register('Joystick', descriptor, ext, {type: 'hid', vendor:0x054c, product:0x0268});
|
||||
})();
|
|
@ -1,46 +0,0 @@
|
|||
/* 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);
|
||||
})();
|
202
picoExtension.js
202
picoExtension.js
|
@ -1,202 +0,0 @@
|
|||
// picoExtension.js
|
||||
// Shane M. Clements, February 2014
|
||||
// PicoBoard Scratch Extension
|
||||
//
|
||||
// This is an extension for development and testing of the Scratch Javascript Extension API.
|
||||
|
||||
(function(ext) {
|
||||
var device = null;
|
||||
var rawData = null;
|
||||
|
||||
// Sensor states:
|
||||
var channels = {
|
||||
slider: 7,
|
||||
light: 5,
|
||||
sound: 6,
|
||||
button: 3,
|
||||
'resistance-A': 4,
|
||||
'resistance-B': 2,
|
||||
'resistance-C': 1,
|
||||
'resistance-D': 0
|
||||
};
|
||||
var inputs = {
|
||||
slider: 0,
|
||||
light: 0,
|
||||
sound: 0,
|
||||
button: 0,
|
||||
'resistance-A': 0,
|
||||
'resistance-B': 0,
|
||||
'resistance-C': 0,
|
||||
'resistance-D': 0
|
||||
};
|
||||
|
||||
ext.resetAll = function(){};
|
||||
|
||||
// Hats / triggers
|
||||
ext.whenSensorConnected = function(which) {
|
||||
return getSensorPressed(which);
|
||||
};
|
||||
|
||||
ext.whenSensorPass = function(which, sign, level) {
|
||||
if (sign == '<') return getSensor(which) < level;
|
||||
return getSensor(which) > level;
|
||||
};
|
||||
|
||||
// Reporters
|
||||
ext.sensorPressed = function(which) {
|
||||
return getSensorPressed(which);
|
||||
};
|
||||
|
||||
ext.sensor = function(which) { return getSensor(which); };
|
||||
|
||||
// Private logic
|
||||
function getSensorPressed(which) {
|
||||
if (device == null) return false;
|
||||
if (which == 'button pressed' && getSensor('button') < 1) return true;
|
||||
if (which == 'A connected' && getSensor('resistance-A') < 10) return true;
|
||||
if (which == 'B connected' && getSensor('resistance-B') < 10) return true;
|
||||
if (which == 'C connected' && getSensor('resistance-C') < 10) return true;
|
||||
if (which == 'D connected' && getSensor('resistance-D') < 10) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function getSensor(which) {
|
||||
return inputs[which];
|
||||
}
|
||||
|
||||
var inputArray = [];
|
||||
function processData() {
|
||||
var bytes = new Uint8Array(rawData);
|
||||
|
||||
inputArray[15] = 0;
|
||||
|
||||
// TODO: make this robust against misaligned packets.
|
||||
// Right now there's no guarantee that our 18 bytes start at the beginning of a message.
|
||||
// Maybe we should treat the data as a stream of 2-byte packets instead of 18-byte packets.
|
||||
// That way we could just check the high bit of each byte to verify that we're aligned.
|
||||
for(var i=0; i<9; ++i) {
|
||||
var hb = bytes[i*2] & 127;
|
||||
var channel = hb >> 3;
|
||||
var lb = bytes[i*2+1] & 127;
|
||||
inputArray[channel] = ((hb & 7) << 7) + lb;
|
||||
}
|
||||
|
||||
if (watchdog && (inputArray[15] == 0x04)) {
|
||||
// Seems to be a valid PicoBoard.
|
||||
clearTimeout(watchdog);
|
||||
watchdog = null;
|
||||
}
|
||||
|
||||
for(var name in inputs) {
|
||||
var v = inputArray[channels[name]];
|
||||
if(name == 'light') {
|
||||
v = (v < 25) ? 100 - v : Math.round((1023 - v) * (75 / 998));
|
||||
}
|
||||
else if(name == 'sound') {
|
||||
//empirically tested noise sensor floor
|
||||
v = Math.max(0, v - 18)
|
||||
v = (v < 50) ? v / 2 :
|
||||
//noise ceiling
|
||||
25 + Math.min(75, Math.round((v - 50) * (75 / 580)));
|
||||
}
|
||||
else {
|
||||
v = (100 * v) / 1023;
|
||||
}
|
||||
|
||||
inputs[name] = v;
|
||||
}
|
||||
|
||||
//console.log(inputs);
|
||||
rawData = null;
|
||||
}
|
||||
|
||||
function appendBuffer( buffer1, buffer2 ) {
|
||||
var tmp = new Uint8Array( buffer1.byteLength + buffer2.byteLength );
|
||||
tmp.set( new Uint8Array( buffer1 ), 0 );
|
||||
tmp.set( new Uint8Array( buffer2 ), buffer1.byteLength );
|
||||
return tmp.buffer;
|
||||
}
|
||||
|
||||
// Extension API interactions
|
||||
var potentialDevices = [];
|
||||
ext._deviceConnected = function(dev) {
|
||||
potentialDevices.push(dev);
|
||||
|
||||
if (!device) {
|
||||
tryNextDevice();
|
||||
}
|
||||
}
|
||||
|
||||
var poller = null;
|
||||
var watchdog = null;
|
||||
function tryNextDevice() {
|
||||
// If potentialDevices is empty, device will be undefined.
|
||||
// That will get us back here next time a device is connected.
|
||||
device = potentialDevices.shift();
|
||||
if (!device) return;
|
||||
|
||||
device.open({ stopBits: 0, bitRate: 38400, ctsFlowControl: 0 });
|
||||
device.set_receive_handler(function(data) {
|
||||
//console.log('Received: ' + data.byteLength);
|
||||
if(!rawData || rawData.byteLength == 18) rawData = new Uint8Array(data);
|
||||
else rawData = appendBuffer(rawData, data);
|
||||
|
||||
if(rawData.byteLength >= 18) {
|
||||
//console.log(rawData);
|
||||
processData();
|
||||
//device.send(pingCmd.buffer);
|
||||
}
|
||||
});
|
||||
|
||||
// Tell the PicoBoard to send a input data every 50ms
|
||||
var pingCmd = new Uint8Array(1);
|
||||
pingCmd[0] = 1;
|
||||
poller = setInterval(function() {
|
||||
device.send(pingCmd.buffer);
|
||||
}, 50);
|
||||
watchdog = setTimeout(function() {
|
||||
// This device didn't get good data in time, so give up on it. Clean up and then move on.
|
||||
// If we get good data then we'll terminate this watchdog.
|
||||
clearInterval(poller);
|
||||
poller = null;
|
||||
device.set_receive_handler(null);
|
||||
device.close();
|
||||
device = null;
|
||||
tryNextDevice();
|
||||
}, 250);
|
||||
};
|
||||
|
||||
ext._deviceRemoved = function(dev) {
|
||||
if(device != dev) return;
|
||||
if(poller) poller = clearInterval(poller);
|
||||
device = null;
|
||||
};
|
||||
|
||||
ext._shutdown = function() {
|
||||
if(device) device.close();
|
||||
if(poller) poller = clearInterval(poller);
|
||||
device = null;
|
||||
};
|
||||
|
||||
ext._getStatus = function() {
|
||||
if(!device) return {status: 1, msg: 'PicoBoard disconnected'};
|
||||
if(watchdog) return {status: 1, msg: 'Probing for PicoBoard'};
|
||||
return {status: 2, msg: 'PicoBoard connected'};
|
||||
}
|
||||
|
||||
var descriptor = {
|
||||
blocks: [
|
||||
['h', 'when %m.booleanSensor', 'whenSensorConnected', 'button pressed'],
|
||||
['h', 'when %m.sensor %m.lessMore %n', 'whenSensorPass', 'slider', '>', 50],
|
||||
['b', 'sensor %m.booleanSensor?', 'sensorPressed', 'button pressed'],
|
||||
['r', '%m.sensor sensor value', 'sensor', 'slider']
|
||||
],
|
||||
menus: {
|
||||
booleanSensor: ['button pressed', 'A connected', 'B connected', 'C connected', 'D connected'],
|
||||
sensor: ['slider', 'light', 'sound', 'resistance-A', 'resistance-B', 'resistance-C', 'resistance-D'],
|
||||
lessMore: ['>', '<']
|
||||
},
|
||||
url: '/info/help/studio/tips/ext/PicoBoard/'
|
||||
};
|
||||
ScratchExtensions.register('PicoBoard', descriptor, ext, {type: 'serial'});
|
||||
})({});
|
|
@ -1,30 +0,0 @@
|
|||
/* 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);
|
||||
})();
|
|
@ -1,36 +0,0 @@
|
|||
/* 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);
|
||||
})();
|
|
@ -1,39 +0,0 @@
|
|||
/* 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);
|
||||
})();
|
|
@ -1,51 +0,0 @@
|
|||
/* 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);
|
||||
})();
|
|
@ -1,30 +0,0 @@
|
|||
/* 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);
|
||||
})();
|
|
@ -1,39 +0,0 @@
|
|||
/* 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