-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
113 lines (106 loc) · 3.45 KB
/
extension.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class CommandExecutor {
constructor(runtime) {
this.runtime = runtime;
this.shell = '/usr/bin/bash';
this.port = '55000';
}
getInfo() {
return {
id: 'commandexecutor',
name: '执行命令',
color1: '#29cc00',
color2: '#21a300',
color3: '#21a300',
blocks: [
{
opcode: 'setShell',
blockType: Scratch.BlockType.COMMAND,
text: '设置 shell 为 [SHELL]',
arguments: {
SHELL: {
type: Scratch.ArgumentType.STRING,
defaultValue: '/usr/bin/bash'
}
}
},
{
opcode: 'setPort',
blockType: Scratch.BlockType.COMMAND,
text: '设置端口为 [PORT]',
arguments: {
PORT: {
type: Scratch.ArgumentType.STRING,
defaultValue: '55000'
}
}
},
{
opcode: 'isServiceRunning',
blockType: Scratch.BlockType.BOOLEAN,
text: '端口 [PORT] 上的命令服务是否运行',
arguments: {
PORT: {
type: Scratch.ArgumentType.STRING,
defaultValue: '55000'
}
}
},
'---',
{
opcode: 'toNull',
blockType: Scratch.BlockType.COMMAND,
text: '抛弃输出 [CMD]',
},
{
opcode: 'executeCommand',
blockType: Scratch.BlockType.REPORTER,
text: '执行命令 [CMD]',
arguments: {
CMD: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'echo Hello World'
}
}
}
]
};
}
setShell({ SHELL }) {
this.shell = String(SHELL);
}
setPort({ PORT }) {
this.port = String(PORT);
}
toNull() { }
isServiceRunning({ PORT }) {
return fetch(`http://localhost:${PORT}/health`)
.then(response => {
if (response.ok) {
return response.json().then(data => data.service === 'Command Executor');
}
return false;
})
.catch(() => false);
}
executeCommand({ CMD }) {
const command = CMD;
const shell = this.shell;
return fetch(`http://localhost:${this.port}/execute`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ command, shell })
})
.then(response => response.text())
.then(data => {
console.log('Command output:', data);
return data;
})
.catch(error => {
console.error('Error:', error);
return 'Error executing command';
});
}
}
Scratch.extensions.register(new CommandExecutor());