-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.js
98 lines (90 loc) · 2.71 KB
/
eval.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
(function(Scratch) {
'use strict';
class Extension {
constructor() {
this.currentScript = "";
this.scriptOutput = "";
}
getInfo() {
return {
id: "dumosEval",
name: "Dumos JS",
"color1": "#c3d100",
"color2": "#000000",
"color3": "#000000",
blocks: [
{
opcode: 'runJSCommand',
text: 'Run the current script',
blockType: Scratch.BlockType.COMMAND
},
{
opcode: 'downloadAndRun',
text: "Download and run script from [URL]",
blockType: Scratch.BlockType.COMMAND,
arguments: {
URL: {
type: Scratch.ArgumentType.STRING
}
}
},
{
opcode: 'clearScript',
text: 'Clear the current script',
blockType: Scratch.BlockType.COMMAND
},
{
opcode: 'addLineScript',
text: 'Add [TEXT] to new line of script',
blockType: Scratch.BlockType.COMMAND,
arguments: {
TEXT: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'alert("Hello World!")'
}
}
},
{
opcode: 'getscriptoutput',
text: 'get script output',
blockType: Scratch.BlockType.REPORTER
},
{
opcode: 'getscript',
text: 'get script',
blockType: Scratch.BlockType.REPORTER
}
]
};
}
runJSCommand() {
this.scriptOutput = eval(this.currentScript);
}
clearScript() {
this.currentScript = "";
}
addLineScript(args) {
this.currentScript += args.TEXT + "\n";
}
getscriptoutput() {
return this.scriptOutput;
}
getscript() {
return this.currentScript;
}
downloadAndRun(args) {
return new Promise((resolve, reject) => {
let newscript = document.createElement("script");
newscript.onload = function() { //instead of using arrow functions, im using classic functions cuz idk if arrow functions work with these
resolve()
}
newscript.onerror = function() {
reject()
}
newscript.src = args.URL;
document.body.appendChild(newscript);
});
}
}
Scratch.extensions.register(new Extension());
})(Scratch);