Skip to content

Commit e7fda41

Browse files
New extension: Upload and Download Text Files (Thanks @planktonfun!) (#718)
Allows users to upload/download text content to a filename and vice versa. (The current gdevelop only supports desktop version, this one is compatible with browsers)
1 parent ef7dca4 commit e7fda41

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
{
2+
"author": "",
3+
"category": "General",
4+
"extensionNamespace": "",
5+
"fullName": "Upload Download Text File",
6+
"helpPath": "https://developer.mozilla.org/en-US/docs/Web/API/File_API/Using_files_from_web_applications",
7+
"iconUrl": "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgdmVyc2lvbj0iMS4xIiBpZD0ibWRpLXVwbG9hZCIgd2lkdGg9IjI0IiBoZWlnaHQ9IjI0IiB2aWV3Qm94PSIwIDAgMjQgMjQiPjxwYXRoIGQ9Ik05LDE2VjEwSDVMMTIsM0wxOSwxMEgxNVYxNkg5TTUsMjBWMThIMTlWMjBINVoiIC8+PC9zdmc+",
8+
"name": "UploadDownloadTextFile",
9+
"previewIconUrl": "https://asset-resources.gdevelop.io/public-resources/Icons/16a8e2514d1c9a57f65f506bb7a420bf63e53dc56c58dfceff63178893031c69_upload.svg",
10+
"shortDescription": "Allows users to upload/download text content to a filename and vice versa. ",
11+
"version": "1.0.0",
12+
"description": [
13+
"Allows users to upload/download text content to a filename and vice versa. ",
14+
"(The current gdevelop only supports desktop version, this one is compatible with browsers)"
15+
],
16+
"tags": [
17+
"download",
18+
"upload",
19+
"text",
20+
"file",
21+
"browser",
22+
"DOM apis"
23+
],
24+
"authorIds": [
25+
"rotBq28wITdtfsrE7McHQri4k2w2"
26+
],
27+
"dependencies": [],
28+
"eventsFunctions": [
29+
{
30+
"description": "Download file with text content.",
31+
"fullName": "Download file with text content",
32+
"functionType": "Action",
33+
"name": "DownloadTextFile",
34+
"sentence": "Download text with filename _PARAM1_",
35+
"events": [
36+
{
37+
"type": "BuiltinCommonInstructions::JsCode",
38+
"inlineCode": [
39+
"function DownloadTextContent(filename, mimeType,content) {",
40+
" var link = document.createElement('a')",
41+
" var blob = new Blob([content], {type: mimeType})",
42+
" var url = URL.createObjectURL(blob);",
43+
"",
44+
" link.setAttribute('href', url);",
45+
" link.setAttribute('download', filename);",
46+
" link.click();",
47+
" link.remove();",
48+
"}",
49+
"",
50+
"const FileName = eventsFunctionContext.getArgument(\"FileName\");",
51+
"const TextContent = eventsFunctionContext.getArgument(\"TextContent\");",
52+
"",
53+
"DownloadTextContent(FileName, \"text/plain\", TextContent);"
54+
],
55+
"parameterObjects": "",
56+
"useStrict": true,
57+
"eventsSheetExpanded": true
58+
}
59+
],
60+
"parameters": [
61+
{
62+
"description": "FileName",
63+
"name": "FileName",
64+
"type": "string"
65+
},
66+
{
67+
"description": "TextContent",
68+
"name": "TextContent",
69+
"type": "string"
70+
}
71+
],
72+
"objectGroups": []
73+
},
74+
{
75+
"description": "Upload a text file to text input.",
76+
"fullName": "Upload a text file to text input",
77+
"functionType": "Action",
78+
"name": "UploadTextFile",
79+
"sentence": "Upload a text file to text input _PARAM1_",
80+
"events": [
81+
{
82+
"type": "BuiltinCommonInstructions::JsCode",
83+
"inlineCode": [
84+
"const allowedFileTypes = [\"text/plain\", \"application/json\"];",
85+
"",
86+
"const InputElement = document.createElement('input');",
87+
"InputElement.type = \"file\";",
88+
"",
89+
"InputElement.addEventListener(\"change\", handleFiles, false);",
90+
"InputElement.click();",
91+
"",
92+
"const SceneVariable = eventsFunctionContext.getArgument(\"SceneVariable\");",
93+
"const Uploading = runtimeScene.getVariables().get(\"__UploadDownloadTextFile\");",
94+
"",
95+
"Uploading.fromJSObject({Uploading:false});",
96+
"",
97+
"async function handleFiles() {",
98+
" const files = this.files;",
99+
" ",
100+
" if (!files.length) {",
101+
" SceneVariable.setString(\"No Files Selected!\");",
102+
" } else if(!allowedFileTypes.includes(files[0].type)) {",
103+
" SceneVariable.setString(\"Please choose text files only.\");",
104+
" } else {",
105+
" SceneVariable.setString(await files[0].text());",
106+
" }",
107+
"",
108+
" Uploading.fromJSObject({Uploading:true});",
109+
" InputElement.remove();",
110+
"}"
111+
],
112+
"parameterObjects": "TextInput",
113+
"useStrict": true,
114+
"eventsSheetExpanded": true
115+
}
116+
],
117+
"parameters": [
118+
{
119+
"description": "SceneVariable",
120+
"name": "SceneVariable",
121+
"type": "scenevar"
122+
}
123+
],
124+
"objectGroups": []
125+
},
126+
{
127+
"description": "Uploading text file finished.",
128+
"fullName": "Uploading text file finished?",
129+
"functionType": "Condition",
130+
"name": "UploadFinished",
131+
"sentence": "Uploading text file finished?",
132+
"events": [
133+
{
134+
"type": "BuiltinCommonInstructions::Standard",
135+
"conditions": [
136+
{
137+
"type": {
138+
"value": "SceneVariableAsBoolean"
139+
},
140+
"parameters": [
141+
"__UploadDownloadTextFile.Uploading",
142+
"False"
143+
]
144+
}
145+
],
146+
"actions": [
147+
{
148+
"type": {
149+
"value": "SetReturnBoolean"
150+
},
151+
"parameters": [
152+
"False"
153+
]
154+
}
155+
]
156+
},
157+
{
158+
"type": "BuiltinCommonInstructions::Standard",
159+
"conditions": [
160+
{
161+
"type": {
162+
"value": "SceneVariableAsBoolean"
163+
},
164+
"parameters": [
165+
"__UploadDownloadTextFile.Uploading",
166+
"True"
167+
]
168+
}
169+
],
170+
"actions": [
171+
{
172+
"type": {
173+
"value": "SetReturnBoolean"
174+
},
175+
"parameters": [
176+
"True"
177+
]
178+
}
179+
]
180+
}
181+
],
182+
"parameters": [],
183+
"objectGroups": []
184+
}
185+
],
186+
"eventsBasedBehaviors": [],
187+
"eventsBasedObjects": []
188+
}

0 commit comments

Comments
 (0)