javascript_bridge.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. .. _doc_web_javascript_bridge:
  2. The JavaScriptBridge singleton
  3. ==============================
  4. In web builds, the :ref:`JavaScriptBridge <class_JavaScriptBridge>` singleton
  5. allows interaction with JavaScript and web browsers, and can be used to implement some
  6. functionalities unique to the web platform.
  7. Interacting with JavaScript
  8. ---------------------------
  9. Sometimes, when exporting Godot for the Web, it might be necessary to interface
  10. with external JavaScript code like third-party SDKs, libraries, or
  11. simply to access browser features that are not directly exposed by Godot.
  12. The ``JavaScriptBridge`` singleton provides methods to wrap a native JavaScript object into
  13. a Godot :ref:`JavaScriptObject <class_JavaScriptObject>` that tries to feel
  14. natural in the context of Godot scripting (e.g. GDScript and C#).
  15. The :ref:`JavaScriptBridge.get_interface() <class_JavaScriptBridge_method_get_interface>`
  16. method retrieves an object in the global scope.
  17. .. code-block:: gdscript
  18. extends Node
  19. func _ready():
  20. # Retrieve the `window.console` object.
  21. var console = JavaScriptBridge.get_interface("console")
  22. # Call the `window.console.log()` method.
  23. console.log("test")
  24. The :ref:`JavaScriptBridge.create_object() <class_JavaScriptBridge_method_create_object>`
  25. creates a new object via the JavaScript ``new`` constructor.
  26. .. code-block:: gdscript
  27. extends Node
  28. func _ready():
  29. # Call the JavaScript `new` operator on the `window.Array` object.
  30. # Passing 10 as argument to the constructor:
  31. # JS: `new Array(10);`
  32. var arr = JavaScriptBridge.create_object("Array", 10)
  33. # Set the first element of the JavaScript array to the number 42.
  34. arr[0] = 42
  35. # Call the `pop` function on the JavaScript array.
  36. arr.pop()
  37. # Print the value of the `length` property of the array (9 after the pop).
  38. print(arr.length)
  39. As you can see, by wrapping JavaScript objects into ``JavaScriptObject`` you can
  40. interact with them like they were native Godot objects, calling their methods,
  41. and retrieving (or even setting) their properties.
  42. Base types (int, floats, strings, booleans) are automatically converted (floats
  43. might lose precision when converted from Godot to JavaScript). Anything else
  44. (i.e. objects, arrays, functions) are seen as ``JavaScriptObjects`` themselves.
  45. Callbacks
  46. ---------
  47. Calling JavaScript code from Godot is nice, but sometimes you need to call a
  48. Godot function from JavaScript instead.
  49. This case is a bit more complicated. JavaScript relies on garbage collection,
  50. while Godot uses reference counting for memory management. This means you have
  51. to explicitly create callbacks (which are returned as ``JavaScriptObjects``
  52. themselves) and you have to keep their reference.
  53. Arguments passed by JavaScript to the callback will be passed as a single Godot
  54. ``Array``.
  55. .. code-block:: gdscript
  56. extends Node
  57. # Here we create a reference to the `_my_callback` function (below).
  58. # This reference will be kept until the node is freed.
  59. var _callback_ref = JavaScriptBridge.create_callback(_my_callback)
  60. func _ready():
  61. # Get the JavaScript `window` object.
  62. var window = JavaScriptBridge.get_interface("window")
  63. # Set the `window.onbeforeunload` DOM event listener.
  64. window.onbeforeunload = _callback_ref
  65. func _my_callback(args):
  66. # Get the first argument (the DOM event in our case).
  67. var js_event = args[0]
  68. # Call preventDefault and set the `returnValue` property of the DOM event.
  69. js_event.preventDefault()
  70. js_event.returnValue = ''
  71. .. warning::
  72. The number of arguments accepted by the callback method (``_my_callback`` in the above example)
  73. **must** match the number of arguments sent by JavaScript. Otherwise, the callback method will
  74. not be called.
  75. Here is another example that asks the user for the `Notification permission <https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API>`__
  76. and waits asynchronously to deliver a notification if the permission is
  77. granted:
  78. .. code-block:: gdscript
  79. extends Node
  80. # Here we create a reference to the `_on_permissions` function (below).
  81. # This reference will be kept until the node is freed.
  82. var _permission_callback = JavaScriptBridge.create_callback(_on_permissions)
  83. func _ready():
  84. # NOTE: This is done in `_ready` for simplicity, but SHOULD BE done in response
  85. # to user input instead (e.g. during `_input`, or `button_pressed` event, etc.),
  86. # otherwise it might not work.
  87. # Get the `window.Notification` JavaScript object.
  88. var notification = JavaScriptBridge.get_interface("Notification")
  89. # Call the `window.Notification.requestPermission` method which returns a JavaScript
  90. # Promise, and bind our callback to it.
  91. notification.requestPermission().then(_permission_callback)
  92. func _on_permissions(args):
  93. # The first argument of this callback is the string "granted" if the permission is granted.
  94. var permission = args[0]
  95. if permission == "granted":
  96. print("Permission granted, sending notification.")
  97. # Create the notification: `new Notification("Hi there!")`
  98. JavaScriptBridge.create_object("Notification", "Hi there!")
  99. else:
  100. print("No notification permission.")
  101. Can I use my favorite library?
  102. ------------------------------
  103. You most likely can. First, you have to
  104. include your library in the page. You can customize the
  105. :ref:`Head Include <doc_javascript_export_options>` during export (see below),
  106. or even :ref:`write your own template <doc_customizing_html5_shell>`.
  107. In the example below, we customize the ``Head Include`` to add an external library
  108. (`axios <https://axios-http.com/>`__) from a content delivery network, and a
  109. second ``<script>`` tag to define our own custom function:
  110. .. code-block:: html
  111. <!-- Axios -->
  112. <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  113. <!-- Custom function -->
  114. <script>
  115. function myFunc() {
  116. alert("My func!");
  117. }
  118. </script>
  119. We can then access both the library and the function from Godot, like we did in
  120. previous examples:
  121. .. code-block:: gdscript
  122. extends Node
  123. # Here create a reference to the `_on_get` function (below).
  124. # This reference will be kept until the node is freed.
  125. var _callback = JavaScriptBridge.create_callback(_on_get)
  126. func _ready():
  127. # Get the `window` object, where globally defined functions are.
  128. var window = JavaScriptBridge.get_interface("window")
  129. # Call the JavaScript `myFunc` function defined in the custom HTML head.
  130. window.myFunc()
  131. # Get the `axios` library (loaded from a CDN in the custom HTML head).
  132. var axios = JavaScriptBridge.get_interface("axios")
  133. # Make a GET request to the current location, and receive the callback when done.
  134. axios.get(window.location.toString()).then(_callback)
  135. func _on_get(args):
  136. OS.alert("On Get")
  137. The eval interface
  138. ------------------
  139. The ``eval`` method works similarly to the JavaScript function of the same
  140. name. It takes a string as an argument and executes it as JavaScript code.
  141. This allows interacting with the browser in ways not possible with script
  142. languages integrated into Godot.
  143. .. tabs::
  144. .. code-tab:: gdscript
  145. func my_func():
  146. JavaScriptBridge.eval("alert('Calling JavaScript per GDScript!');")
  147. .. code-tab:: csharp
  148. private void MyFunc()
  149. {
  150. JavaScriptBridge.Eval("alert('Calling JavaScript per C#!');")
  151. }
  152. The value of the last JavaScript statement is converted to a GDScript value and
  153. returned by ``eval()`` under certain circumstances:
  154. * JavaScript ``number`` is returned as :ref:`class_float`
  155. * JavaScript ``boolean`` is returned as :ref:`class_bool`
  156. * JavaScript ``string`` is returned as :ref:`class_String`
  157. * JavaScript ``ArrayBuffer``, ``TypedArray``, and ``DataView`` are returned as :ref:`PackedByteArray<class_PackedByteArray>`
  158. .. tabs::
  159. .. code-tab:: gdscript
  160. func my_func2():
  161. var js_return = JavaScriptBridge.eval("var myNumber = 1; myNumber + 2;")
  162. print(js_return) # prints '3.0'
  163. .. code-tab:: csharp
  164. private void MyFunc2()
  165. {
  166. var jsReturn = JavaScriptBridge.Eval("var myNumber = 1; myNumber + 2;");
  167. GD.Print(jsReturn); // prints '3.0'
  168. }
  169. Any other JavaScript value is returned as ``null``.
  170. HTML5 export templates may be :ref:`built <doc_compiling_for_web>` without
  171. support for the singleton to improve security. With such templates, and on
  172. platforms other than HTML5, calling ``JavaScriptBridge.eval`` will also return
  173. ``null``. The availability of the singleton can be checked with the
  174. ``web`` :ref:`feature tag <doc_feature_tags>`:
  175. .. tabs::
  176. .. code-tab:: gdscript
  177. func my_func3():
  178. if OS.has_feature('web'):
  179. JavaScriptBridge.eval("""
  180. console.log('The JavaScriptBridge singleton is available')
  181. """)
  182. else:
  183. print("The JavaScriptBridge singleton is NOT available")
  184. .. code-tab:: csharp
  185. private void MyFunc3()
  186. {
  187. if (OS.HasFeature("web"))
  188. {
  189. JavaScriptBridge.Eval("console.log('The JavaScriptBridge singleton is available')");
  190. }
  191. else
  192. {
  193. GD.Print("The JavaScriptBridge singleton is NOT available");
  194. }
  195. }
  196. .. tip:: GDScript's multi-line strings, surrounded by 3 quotes ``"""`` as in
  197. ``my_func3()`` above, are useful to keep JavaScript code readable.
  198. The ``eval`` method also accepts a second, optional Boolean argument, which
  199. specifies whether to execute the code in the global execution context,
  200. defaulting to ``false`` to prevent polluting the global namespace:
  201. .. tabs::
  202. .. code-tab:: gdscript
  203. func my_func4():
  204. # execute in global execution context,
  205. # thus adding a new JavaScript global variable `SomeGlobal`
  206. JavaScriptBridge.eval("var SomeGlobal = {};", true)
  207. .. code-tab:: csharp
  208. private void MyFunc4()
  209. {
  210. // execute in global execution context,
  211. // thus adding a new JavaScript global variable `SomeGlobal`
  212. JavaScriptBridge.Eval("var SomeGlobal = {};", true);
  213. }
  214. .. _doc_web_downloading_files:
  215. Downloading files
  216. -----------------
  217. Downloading files (e.g. a save game) from the Godot Web export to the user's computer can be done by directly interacting with JavaScript, but given it is a
  218. very common use case, Godot exposes this functionality to scripting via
  219. a dedicated :ref:`JavaScriptBridge.download_buffer() <class_JavaScriptBridge_method_download_buffer>`
  220. function which lets you download any generated buffer.
  221. Here is a minimal example on how to use it:
  222. extends Node
  223. .. code-block:: gdscript
  224. func _ready():
  225. # Asks the user download a file called "hello.txt" whose content will be the string "Hello".
  226. JavaScriptBridge.download_buffer("Hello".to_utf8_buffer(), "hello.txt")
  227. And here is a more complete example on how to download a previously saved file:
  228. .. code-block:: gdscript
  229. extends Node
  230. # Open a file for reading and download it via the JavaScript singleton.
  231. func _download_file(path):
  232. var file = FileAccess.open(path, FileAccess.READ)
  233. if file == null:
  234. push_error("Failed to load file")
  235. return
  236. # Get the file name.
  237. var fname = path.get_file()
  238. # Read the whole file to memory.
  239. var buffer = file.get_buffer(file.get_len())
  240. # Prompt the user to download the file (will have the same name as the input file).
  241. JavaScriptBridge.download_buffer(buffer, fname)
  242. func _ready():
  243. # Create a temporary file.
  244. var config = ConfigFile.new()
  245. config.set_value("option", "one", false)
  246. config.save("/tmp/test.cfg")
  247. # Download it
  248. _download_file("/tmp/test.cfg")