-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitPenguin-mod-by-RRaccoon.js
More file actions
198 lines (185 loc) · 6.91 KB
/
GitPenguin-mod-by-RRaccoon.js
File metadata and controls
198 lines (185 loc) · 6.91 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
(function (Scratch) {
let status = "", encodeMode = true;
class GitPenguin {
getInfo() {
return {
id: "gitpenguin",
name: "GitHub File Extension",
color1: "#303030",
color2: "#212121",
color3: "#212121",
blocks: [
{ blockType: Scratch.BlockType.LABEL, text: "Get" },
{
opcode: "getFileContents",
blockType: Scratch.BlockType.REPORTER,
text: "get contents of file [FILE] from repository [REPO] of user [NAME] using token [TOKEN]",
arguments: {
FILE: { type: Scratch.ArgumentType.STRING, defaultValue: "README.md" },
REPO: { type: Scratch.ArgumentType.STRING, defaultValue: "repository" },
NAME: { type: Scratch.ArgumentType.STRING, defaultValue: "username" },
TOKEN: { type: Scratch.ArgumentType.STRING, defaultValue: "YOUR_GITHUB_TOKEN" }
}
},
{ blockType: Scratch.BlockType.LABEL, text: "File control" },
{
opcode: "createFile",
blockType: Scratch.BlockType.COMMAND,
text: "create file [FILE] with content [CONTENT] in repository [REPO] of user [NAME] using token [TOKEN]",
arguments: {
FILE: { type: Scratch.ArgumentType.STRING, defaultValue: "newFile.txt" },
CONTENT: { type: Scratch.ArgumentType.STRING, defaultValue: "Hello, world!" },
REPO: { type: Scratch.ArgumentType.STRING, defaultValue: "repository" },
NAME: { type: Scratch.ArgumentType.STRING, defaultValue: "username" },
TOKEN: { type: Scratch.ArgumentType.STRING, defaultValue: "YOUR_GITHUB_TOKEN" }
}
},
{
opcode: "editFileContent",
blockType: Scratch.BlockType.COMMAND,
text: "edit content of file [FILE] in repository [REPO] of user [NAME] to [CONTENT] using token [TOKEN]",
arguments: {
FILE: { type: Scratch.ArgumentType.STRING, defaultValue: "newFile.txt" },
CONTENT: { type: Scratch.ArgumentType.STRING, defaultValue: "Hello, world!" },
REPO: { type: Scratch.ArgumentType.STRING, defaultValue: "repository" },
NAME: { type: Scratch.ArgumentType.STRING, defaultValue: "username" },
TOKEN: { type: Scratch.ArgumentType.STRING, defaultValue: "YOUR_GITHUB_TOKEN" }
}
},
{
opcode: "getStatus",
blockType: Scratch.BlockType.REPORTER,
text: "recent file status"
},
{ blockType: Scratch.BlockType.LABEL, text: "DANGEROUS" },
{
opcode: "deleteFile",
blockType: Scratch.BlockType.COMMAND,
text: "delete file [FILE] from repository [REPO] of user [NAME] using token [TOKEN]",
arguments: {
FILE: { type: Scratch.ArgumentType.STRING, defaultValue: "newFile.txt" },
REPO: { type: Scratch.ArgumentType.STRING, defaultValue: "repository" },
NAME: { type: Scratch.ArgumentType.STRING, defaultValue: "username" },
TOKEN: { type: Scratch.ArgumentType.STRING, defaultValue: "YOUR_GITHUB_TOKEN" }
}
},
{
opcode: "toggleEncode",
blockType: Scratch.BlockType.COMMAND,
text: "toggle file encoding [TYPE]",
arguments: {
TYPE: { type: Scratch.ArgumentType.STRING, menu: "TOGGLE" }
}
},
],
menus: {
TOGGLE: ["on", "off"]
}
};
}
async getFileContents({ FILE, REPO, NAME, TOKEN }) {
const apiUrl = `https://api.github.com/repos/${NAME}/${REPO}/contents/${FILE}`;
// Запрос с токеном, если токен передан
if (TOKEN && TOKEN !== "") {
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `token ${TOKEN}`
}
});
status = response.status;
if (!response.ok) {
console.error(`Error: ${status}`);
return '';
}
const data = await response.json();
const content = atob(data.content);
return content;
} else {
// Запрос без токена
const response = await Scratch.fetch(apiUrl);
const data = await response.json();
status = response.status;
if (response.ok) {
return atob(data.content);
} else {
return '';
}
}
}
async createFile({ FILE, CONTENT, REPO, NAME, TOKEN }) {
const apiUrl = `https://api.github.com/repos/${NAME}/${REPO}/contents/${FILE}`;
const requestBody = JSON.stringify({
message: `Create ${FILE}`,
content: encodeMode ? btoa(CONTENT) : CONTENT
});
const response = await fetch(apiUrl, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `token ${TOKEN}`
},
body: requestBody
});
status = response.status;
if (!response.ok) return;
}
async editFileContent({ FILE, CONTENT, REPO, NAME, TOKEN }) {
const apiUrl = `https://api.github.com/repos/${NAME}/${REPO}/contents/${FILE}`;
const getResponse = await fetch(apiUrl, {
headers: {
'Authorization': `token ${TOKEN}`
}
});
status = getResponse.status;
if (!getResponse.ok) return;
const fileData = await getResponse.json();
const putResponse = await fetch(apiUrl, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `token ${TOKEN}`
},
body: JSON.stringify({
message: `Edit ${FILE}`,
content: encodeMode ? btoa(CONTENT) : CONTENT,
sha: fileData.sha
})
});
status = putResponse.status;
if (!putResponse.ok) return;
}
async deleteFile({ FILE, REPO, NAME, TOKEN }) {
const apiUrl = `https://api.github.com/repos/${NAME}/${REPO}/contents/${FILE}`;
const getResponse = await fetch(apiUrl, {
headers: {
'Authorization': `token ${TOKEN}`
}
});
status = getResponse.status;
if (!getResponse.ok) return;
const fileData = await getResponse.json();
const deleteResponse = await fetch(apiUrl, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Authorization': `token ${TOKEN}`
},
body: JSON.stringify({
message: `Delete ${FILE}`,
sha: fileData.sha
})
});
status = deleteResponse.status;
if (!deleteResponse.ok) return;
}
getStatus() {
return status;
}
toggleEncode({ TYPE }) {
encodeMode = TYPE === "on";
}
}
Scratch.extensions.register(new GitPenguin());
})(Scratch);