scratch-extension-docs/weather_extension.js
2014-05-17 00:07:40 -04:00

39 lines
No EOL
1.2 KiB
JavaScript

/* 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);
})();