-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyberexplorer's JavaScript.js
More file actions
135 lines (124 loc) · 4.93 KB
/
Cyberexplorer's JavaScript.js
File metadata and controls
135 lines (124 loc) · 4.93 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Name: Cyberexplorer's JavaScript
// ID: cyberexplorersJavaScript
// Author: Cyberexplorer
// License: MIT
Scratch.translate.setup({
"zh-cn": {
"_cyberexplorersJavaScript": "赛博猫猫的 JavaScript",
"_Run JavaScript": "运行 JavaScript [code]",
"_Show Result": "显示结果",
"_Last Result": "上次结果",
"_Evaluate Condition": "条件判断 [condition]",
"_return": "运行并返回 [code]"
},
"en": {
"_cyberexplorersJavaScript": "Cyberexplorer's JavaScript",
"_Run JavaScript": "Run JavaScript [code]",
"_Show Result": "Show Result",
"_Last Result": "Last Result",
"_Evaluate Condition": "Evaluate condition [condition]",
"_return": "Run and return [code]"
},
"ja": {
"_cyberexplorersJavaScript": "サイバーエクスプローラーの JavaScript",
"_Run JavaScript": "JavaScript を実行する [code]",
"_Show Result": "結果を表示する",
"_Last Result": "最後の結果",
"_Evaluate Condition": "条件を評価する [condition]",
"_return": "実行して返す [code]"
}
});
(function (Scratch) {
"use strict";
const EXTENSION_ICON = "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+PHJlY3Qgd2lkdGg9IjEwMCIgaGVpZ2h0PSIxMDAiIGZpbGw9IiMxOTc2RDIiLz48dGV4dCB4PSI1MCIgeT0iNTAiIGZvbnQtZmFtaWx5PSJBcmlhbCIgZm9udC1zaXplPSIyMCIgdGV4dC1hbmNob3I9Im1pZGRsZSIgZmlsbD0id2hpdGUiPlNjcmlwdDwvdGV4dD48L3N2Zz4=";
class JSRunner {
constructor(runtime) {
this.runtime = runtime;
this.lastResult = "";
}
getInfo() {
return {
id: "cyberexplorersJavaScript",
name: Scratch.translate("cyberexplorersJavaScript"),
menuIconURI: EXTENSION_ICON,
color1: "#4285F4",
color2: "#34A853",
color3: "#FBBC05",
blocks: [
{
opcode: "runJavaScript",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("Run JavaScript"),
arguments: {
code: {
type: Scratch.ArgumentType.STRING,
defaultValue: "console.log('Hello World')"
}
}
},
{
opcode: "evaluateJavaScript",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("return"),
arguments: {
code: {
type: Scratch.ArgumentType.STRING,
defaultValue: "'Hello World'"
}
}
},
{
opcode: "evaluateCondition",
blockType: Scratch.BlockType.BOOLEAN,
text: Scratch.translate("Evaluate condition"),
arguments: {
condition: {
type: Scratch.ArgumentType.STRING,
defaultValue: "1 === 1"
}
}
},
{
opcode: "getLastResult",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("Last Result")
}
]
};
}
runJavaScript(args) {
try {
const code = args.code;
const func = new Function(code);
this.lastResult = func();
} catch (error) {
console.error("Error executing JavaScript:", error);
this.lastResult = "Error: " + error.message;
}
}
evaluateJavaScript(args) {
try {
const code = args.code;
const func = new Function("return " + code);
return func();
} catch (error) {
console.error("Error evaluating JavaScript:", error);
return "Error: " + error.message;
}
}
evaluateCondition(args) {
try {
const condition = args.condition;
const func = new Function("return " + condition);
return func();
} catch (error) {
console.error("Error evaluating condition:", error);
return false;
}
}
getLastResult() {
return this.lastResult;
}
}
Scratch.extensions.register(new JSRunner(Scratch.vm.runtime));
})(Scratch);