forked from mdaquin/Web-Object-Scratch-Extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wose.js
57 lines (53 loc) · 1.95 KB
/
wose.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
(function(ext) {
// basic function to define the scratch extension
ext._shutdown = function() {};
ext._getStatus = function() {return {status: 2, msg: 'Ready'};};
// variables
var uris=new Array();
var props=new Array();
var un="";
var pw="";
// the main function making an AJAX call at the
// URL (at index u) and getting the value of the
// property (JSONPath) at index p
ext.get_value=function(p,u,cb) {
$.ajax({
url:uris[u],
success: function(data) {
var st="data";
if (props[p].indexOf("[")!=0) st+='.';
st+=props[p];
console.log(st);
value=eval(st);
cb(value);
},
error: function(jqXHR, textStatus, errorThrown){
cb(textStatus+" "+errorThrown);
},
username: un,
password: pw,
dataType: "json",
xhrFields: {withCredentials: true},
crossDomain: true
});
};
ext.set_uri=function(i,u){uris[i]=u;};
ext.set_prop=function(i,p){props[i]=p;};
ext.get_uri=function(i){return uris[i];};
ext.get_prop=function(i){return props[i];};
ext.set_uname=function(u){un=u};
ext.set_pass=function(p){pw=p};
// definition of extension (scratch blocks)
var descriptor = {
blocks: [
[' ', 'set address of object %n to %s', 'set_uri', 1, 'https://data.beta.mksmart.org/entity/'],
[' ', 'define property %n as %s', 'set_prop', 1, 'live'],
['R', 'value of property %n for object %n', 'get_value', 1, 1],
['r', 'address of object %n', 'get_uri', 1],
['r', 'definition of property %n', 'get_prop', 1],
[' ', 'set username to %s', 'set_uname', ''],
[' ', 'set password to %s', 'set_pass', ''],
]
};
ScratchExtensions.register('Web Object Extension', descriptor, ext);
})({});