Skip to content

Instantly share code, notes, and snippets.

@hnOsmium0001
Created October 27, 2018 18:51
Show Gist options
  • Save hnOsmium0001/3afccad2185d60244facdfc32804639e to your computer and use it in GitHub Desktop.
Save hnOsmium0001/3afccad2185d60244facdfc32804639e to your computer and use it in GitHub Desktop.
Scratch 3 Extension - test #1
(function(ext) {
function isUndefined(v) {
return typeof v === 'undefined';
}
function originalOrEmptyStr(v) {
return isUndefined(v) ? '' : v;
}
ext._shutdown = function() {};
ext._getStatus = function() {
return {status: 2, msg: 'Ready'};
};
ext.valueVoid = function() {
};
var maps = {};
ext.defineMap = function(name) {
if(!isUndefined(maps[name])) {
maps[name] = {};
}
};
ext.deleteMap = function(name) {
maps[name] = null;
};
ext.getValueFromMap = function(key, name) {
var map = maps[name];
if(!isUndefined(map)) {
var value = map[key];
return originalOrEmptyStr(value);
} else {
return '';
}
};
ext.setValueInMap = function(key, name, value) {
var map = maps[name];
if(!isUndefined(map)) {
var oldValue = map[key];
map[key] = value;
return originalOrEmptyStr(value);
} else {
return '';
}
};
ext.containsKey = function(name, key) {
var map = maps[name];
if(!isUndefined(map)) {
return !isUndefined(map[key]);
}
return false;
};
var descriptor = {
blocks: [
// [type, displayName, func, defaultParams...]
[' ', 'run %n', 'valueVoid', ''],
[' ', 'create map named %s', 'defineMap', 'map'],
[' ', 'delete map named %s', 'deleteMap', 'map'],
['r', 'get value with key %s in map %s', 'getValueFromMap', 'key', 'map'],
['r', 'set value with key %s in map %s to %s', 'setValueInMap', 'key', 'map', 'value'],
['b', 'map %s contains %s as a key?', 'containsKey', 'map', 'key']
],
url: '',
displayName: 'Test Extension'
};
ScratchExtensions.register('testext1', descriptor, ext);
})({});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment