Skip to content

Commit 5fbb69f

Browse files
authored
[Clipboard][Update] Make the clipboard extension async✨ (#748) (Thanks @arthuro555!)
1 parent 3e01ef2 commit 5fbb69f

File tree

1 file changed

+140
-33
lines changed

1 file changed

+140
-33
lines changed

extensions/reviewed/Clipboard.json

Lines changed: 140 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
{
22
"author": "@Bouh, @arthuro555",
33
"category": "User interface",
4-
"description": "This extension adds tools to access the clipboard.",
54
"extensionNamespace": "",
65
"fullName": "Clipboard",
76
"helpPath": "",
87
"iconUrl": "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgdmVyc2lvbj0iMS4xIiBpZD0ibWRpLWNsaXBib2FyZC10ZXh0LW11bHRpcGxlLW91dGxpbmUiIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0Ij48cGF0aCBkPSJNNCA3VjIxSDE4VjIzSDRDMi45IDIzIDIgMjIuMSAyIDIxVjdINE0yMCAzQzIxLjEgMyAyMiAzLjkgMjIgNVYxN0MyMiAxOC4xIDIxLjEgMTkgMjAgMTlIOEM2LjkgMTkgNiAxOC4xIDYgMTdWNUM2IDMuOSA2LjkgMyA4IDNIMTEuMThDMTEuNiAxLjg0IDEyLjcgMSAxNCAxQzE1LjMgMSAxNi40IDEuODQgMTYuODIgM0gyME0xNCAzQzEzLjQ1IDMgMTMgMy40NSAxMyA0QzEzIDQuNTUgMTMuNDUgNSAxNCA1QzE0LjU1IDUgMTUgNC41NSAxNSA0QzE1IDMuNDUgMTQuNTUgMyAxNCAzTTEwIDdWNUg4VjE3SDIwVjVIMThWN00xNSAxNUgxMFYxM0gxNU0xOCAxMUgxMFY5SDE4VjExWiIgLz48L3N2Zz4=",
98
"name": "Clipboard",
109
"previewIconUrl": "https://resources.gdevelop-app.com/assets/Icons/clipboard-text-multiple-outline.svg",
1110
"shortDescription": "Read and write the clipboard.",
12-
"version": "1.0.0",
11+
"version": "2.0.0",
12+
"description": "This extension allows to access the clipboard.",
13+
"origin": {
14+
"identifier": "Clipboard",
15+
"name": "gdevelop-extension-store"
16+
},
1317
"tags": [
1418
"clipboard",
1519
"pasteboard",
@@ -31,33 +35,69 @@
3135
],
3236
"eventsFunctions": [
3337
{
34-
"description": "Read the text from the clipboard asynchronously. As this is \"asynchronous\", this means that the variable won't be immediately filled with the text from the clipboard. Instead, it will be filled a few milliseconds later.\n\nNote also that on web browsers, the user might be asked for permissions to read from the clipboard.",
38+
"async": true,
39+
"description": "Read the text from the clipboard asynchronously. \n\nNote also that on web browsers, the user might be asked for permissions to read from the clipboard.",
3540
"fullName": "Get text from the clipboard",
3641
"functionType": "Action",
37-
"name": "ReadTextCrossPlaform",
38-
"private": false,
42+
"name": "AsynchronouslyReadTextCrossPlaform",
3943
"sentence": "Read clipboard and store text in _PARAM1_",
4044
"events": [
4145
{
42-
"disabled": false,
43-
"folded": false,
4446
"type": "BuiltinCommonInstructions::JsCode",
45-
"inlineCode": "const electron = runtimeScene.getGame().getRenderer().getElectron();\nconst callback =\n runtimeScene\n .getVariables()\n .get(eventsFunctionContext.getArgument(\"callback\"));\n\nif (electron !== null && electron.clipboard)\n callback.setString(electron.clipboard.readText());\nelse if (\n typeof cordova !== \"undefined\" &&\n cordova.plugins &&\n cordova.plugins.clipboard\n) cordova.plugins.clipboard.paste(text => callback.setString(text));\nelse if (\n typeof navigator !== \"undefined\" &&\n navigator.clipboard &&\n navigator.clipboard.readText\n) {\n navigator.clipboard.readText()\n .then(text => callback.setString(text))\n .catch(err =>\n console.error(\"Error occured while getting clipboard content: \", err.message)\n );\n} else console.error(\"Unable to read from the clipboard: no method found for this platform.\")\n",
47+
"inlineCode": [
48+
"const electron = runtimeScene.getGame().getRenderer().getElectron();",
49+
"const callback = eventsFunctionContext.getArgument(\"callback\");",
50+
"const { task } = eventsFunctionContext;",
51+
"const logger = this.logger || (this.logger = new gdjs.Logger(\"Clipboard extension\"));",
52+
"",
53+
"if (electron !== null && electron.clipboard) {",
54+
" callback.setString(electron.clipboard.readText());",
55+
" task.resolve();",
56+
"} else if (",
57+
" typeof cordova !== \"undefined\" &&",
58+
" cordova.plugins &&",
59+
" cordova.plugins.clipboard",
60+
") {",
61+
" cordova.plugins.clipboard.paste(",
62+
" text => {",
63+
" callback.setString(text);",
64+
" task.resolve();",
65+
" },",
66+
" error => {",
67+
" logger.error(\"An error occured while getting clipboard content: \", error);",
68+
" task.resolve();",
69+
" }",
70+
" );",
71+
"} else if (",
72+
" typeof navigator !== \"undefined\" &&",
73+
" navigator.clipboard &&",
74+
" navigator.clipboard.readText",
75+
") {",
76+
" navigator.clipboard.readText()",
77+
" .then(text => {",
78+
" callback.setString(text);",
79+
" task.resolve();",
80+
" })",
81+
" .catch(error => {",
82+
" logger.error(\"An error occured while getting clipboard content: \", error.message);",
83+
" task.resolve();",
84+
" });",
85+
"} else {",
86+
" logger.error(\"Unable to read from the clipboard: no method found for this platform.\");",
87+
" task.resolve();",
88+
"}",
89+
""
90+
],
4691
"parameterObjects": "",
4792
"useStrict": true,
4893
"eventsSheetExpanded": false
4994
}
5095
],
5196
"parameters": [
5297
{
53-
"codeOnly": false,
54-
"defaultValue": "",
55-
"description": "Callback variable where to store the result",
56-
"longDescription": "",
98+
"description": "Callback variable where to store the clipboard contents",
5799
"name": "callback",
58-
"optional": false,
59-
"supplementaryInformation": "",
60-
"type": "string"
100+
"type": "scenevar"
61101
}
62102
],
63103
"objectGroups": []
@@ -67,44 +107,105 @@
67107
"fullName": "Write text to the clipboard",
68108
"functionType": "Action",
69109
"name": "WriteText",
70-
"private": false,
71110
"sentence": "Write _PARAM1_ to clipboard",
72111
"events": [
73112
{
74-
"disabled": false,
75-
"folded": false,
76113
"type": "BuiltinCommonInstructions::JsCode",
77-
"inlineCode": "const electron = runtimeScene.getGame().getRenderer().getElectron();\nconst text = eventsFunctionContext.getArgument(\"text\");\n\nif (electron !== null && electron.clipboard)\n electron.clipboard.writeText(text);\nelse if (\n typeof cordova !== \"undefined\" &&\n cordova.plugins &&\n cordova.plugins.clipboard\n) cordova.plugins.clipboard.copy(text);\nelse if (\n typeof navigator !== \"undefined\" &&\n navigator.clipboard &&\n navigator.clipboard.writeText\n) navigator.clipboard\n .writeText(text)\n .catch(e => console.error(\"Error while writing clipboard: \", e));\nelse console.error(\"Unable to write to the clipboard: no method found for this platform.\"); \n",
114+
"inlineCode": [
115+
"const electron = runtimeScene.getGame().getRenderer().getElectron();",
116+
"const text = eventsFunctionContext.getArgument(\"text\");",
117+
"",
118+
"if (electron !== null && electron.clipboard)",
119+
" electron.clipboard.writeText(text);",
120+
"else if (",
121+
" typeof cordova !== \"undefined\" &&",
122+
" cordova.plugins &&",
123+
" cordova.plugins.clipboard",
124+
") cordova.plugins.clipboard.copy(text);",
125+
"else if (",
126+
" typeof navigator !== \"undefined\" &&",
127+
" navigator.clipboard &&",
128+
" navigator.clipboard.writeText",
129+
") navigator.clipboard",
130+
" .writeText(text)",
131+
" .catch(e => console.error(\"Error while writing clipboard: \", e));",
132+
"else console.error(\"Unable to write to the clipboard: no method found for this platform.\"); ",
133+
""
134+
],
78135
"parameterObjects": "",
79136
"useStrict": true,
80137
"eventsSheetExpanded": false
81138
}
82139
],
83140
"parameters": [
84141
{
85-
"codeOnly": false,
86-
"defaultValue": "",
87142
"description": "Text to write to clipboard",
88-
"longDescription": "",
89143
"name": "text",
90-
"optional": false,
91-
"supplementaryInformation": "",
92144
"type": "string"
93145
}
94146
],
95147
"objectGroups": []
96148
},
97149
{
98-
"description": "Read the text from the clipboard (Windows, macOS, Linux only)",
99-
"fullName": "Get text from the clipboard (Windows, macOS, Linux)",
150+
"description": "Read the text from the clipboard asynchronously. As this is \"asynchronous\", the variable won't be immediately filled with the text from the clipboard. You will have to wait a few frames before it will be. If you want your subsequent actions and subevents to automatically wait for the read to finish, use the waiting version of this action instead (recomended). \n\nNote also that on web browsers, the user might be asked for permissions to read from the clipboard.",
151+
"fullName": "(No waiting) Get text from the clipboard",
152+
"functionType": "Action",
153+
"name": "ReadTextCrossPlaform",
154+
"private": true,
155+
"sentence": "Read clipboard and store text in _PARAM1_",
156+
"events": [
157+
{
158+
"type": "BuiltinCommonInstructions::JsCode",
159+
"inlineCode": [
160+
"const electron = runtimeScene.getGame().getRenderer().getElectron();",
161+
"const callback =",
162+
" runtimeScene",
163+
" .getVariables()",
164+
" .get(eventsFunctionContext.getArgument(\"callback\"));",
165+
"",
166+
"if (electron !== null && electron.clipboard)",
167+
" callback.setString(electron.clipboard.readText());",
168+
"else if (",
169+
" typeof cordova !== \"undefined\" &&",
170+
" cordova.plugins &&",
171+
" cordova.plugins.clipboard",
172+
") cordova.plugins.clipboard.paste(text => callback.setString(text));",
173+
"else if (",
174+
" typeof navigator !== \"undefined\" &&",
175+
" navigator.clipboard &&",
176+
" navigator.clipboard.readText",
177+
") {",
178+
" navigator.clipboard.readText()",
179+
" .then(text => callback.setString(text))",
180+
" .catch(err =>",
181+
" console.error(\"Error occured while getting clipboard content: \", err.message)",
182+
" );",
183+
"} else console.error(\"Unable to read from the clipboard: no method found for this platform.\")",
184+
""
185+
],
186+
"parameterObjects": "",
187+
"useStrict": true,
188+
"eventsSheetExpanded": false
189+
}
190+
],
191+
"parameters": [
192+
{
193+
"description": "Callback variable where to store the result",
194+
"name": "callback",
195+
"type": "string"
196+
}
197+
],
198+
"objectGroups": []
199+
},
200+
{
201+
"description": "[DEPRECATED] Read the text from the clipboard (Windows, macOS, Linux only)",
202+
"fullName": "[DEPRECATED] Get text from the clipboard (Windows, macOS, Linux)",
100203
"functionType": "StringExpression",
101204
"name": "ReadText",
102205
"private": true,
103206
"sentence": "",
104207
"events": [
105208
{
106-
"disabled": false,
107-
"folded": false,
108209
"type": "BuiltinCommonInstructions::Comment",
109210
"color": {
110211
"b": 109,
@@ -114,22 +215,28 @@
114215
"textG": 0,
115216
"textR": 0
116217
},
117-
"comment": "This is here for retrocompatibility. Try to use the asynchronous readTextCrossPlatform action instead.",
218+
"comment": "This is here for retrocompatibility. Try to use the AsynchronouslyReadTextCrossPlatform action instead.",
118219
"comment2": ""
119220
},
120221
{
121-
"disabled": false,
122-
"folded": false,
123222
"type": "BuiltinCommonInstructions::JsCode",
124-
"inlineCode": "const electron = runtimeScene.getGame().getRenderer().getElectron();\nif (electron && electron.clipboard) eventsFunctionContext.returnValue = electron.clipboard.readText();\n",
223+
"inlineCode": [
224+
"const electron = runtimeScene.getGame().getRenderer().getElectron();",
225+
"if (electron && electron.clipboard) eventsFunctionContext.returnValue = electron.clipboard.readText();",
226+
""
227+
],
125228
"parameterObjects": "",
126229
"useStrict": true,
127230
"eventsSheetExpanded": false
128231
}
129232
],
233+
"expressionType": {
234+
"type": "string"
235+
},
130236
"parameters": [],
131237
"objectGroups": []
132238
}
133239
],
134-
"eventsBasedBehaviors": []
240+
"eventsBasedBehaviors": [],
241+
"eventsBasedObjects": []
135242
}

0 commit comments

Comments
 (0)