Skip to content

Commit 1f0d9d3

Browse files
authored
Create JavaScript.js
1 parent 653f6ee commit 1f0d9d3

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Original extension by @LilyMakesThings
2+
3+
(function (Scratch) {
4+
'use strict';
5+
6+
const vm = Scratch.vm;
7+
const runtime = vm.runtime;
8+
const isPackaged = runtime.isPackaged;
9+
10+
let allowJSCode = true;
11+
let ineditor = true;
12+
13+
if (!Scratch.extensions.unsandboxed) {
14+
throw new Error('This extension must run unsandboxed');
15+
}
16+
17+
class CustomJS {
18+
getInfo() {
19+
return {
20+
id: 'JavaScript',
21+
name: 'JavaScript',
22+
color1: '#AAAAAA',
23+
blocks: [
24+
{
25+
opcode: 'execute',
26+
func: 'javascript',
27+
blockType: Scratch.BlockType.COMMAND,
28+
text: 'execute [JS]',
29+
arguments: {
30+
JS: {
31+
type: Scratch.ArgumentType.STRING,
32+
defaultValue: 'alert("Hello World!")'
33+
}
34+
}
35+
},
36+
{
37+
opcode: 'evaluateReporter',
38+
func: 'javascript',
39+
blockType: Scratch.BlockType.REPORTER,
40+
text: 'evaluate [JS]',
41+
arguments: {
42+
JS: {
43+
type: Scratch.ArgumentType.STRING,
44+
defaultValue: 'Math.random()'
45+
}
46+
}
47+
},
48+
{
49+
opcode: 'evaluateBoolean',
50+
func: 'javascript',
51+
blockType: Scratch.BlockType.BOOLEAN,
52+
text: 'evaluate [JS]',
53+
arguments: {
54+
JS: {
55+
type: Scratch.ArgumentType.STRING,
56+
defaultValue: 'Math.round(Math.random()) === 1'
57+
}
58+
}
59+
},
60+
]
61+
};
62+
}
63+
64+
javascript(args, util) {
65+
const output = eval(args.JS);
66+
return (output) ? output : '';
67+
}
68+
}
69+
70+
Scratch.extensions.register(new CustomJS());
71+
})(Scratch);

0 commit comments

Comments
 (0)