godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.59k stars 21.27k forks source link

[bug][web] USER ERROR: Clipboard is not supported by this display server #83752

Open alanenggb opened 1 year ago

alanenggb commented 1 year ago

Godot version

4.2.beta2.official

System information

Godot v4.2.beta2 - Windows 10.0.22635 - GLES3 (Compatibility) - NVIDIA GeForce RTX 3060 Ti (NVIDIA; 31.0.15.3758) - AMD Ryzen 9 7900X 12-Core Processor (24 Threads)

Issue description

Hello! I'm making an application that should be able to get an image from the the clipboard and put it in a TextureRect. It works when running the project directly from the engine, but when we export to the web and run the html in a local server, the application is unable to run the code below, throwing an error.

var img = DisplayServer.clipboard_get_image()

Error: USER ERROR: Clipboard is not supported by this display server image

Steps to reproduce

Minimal reproduction project

Source only bug_clipboard_web_4.2.beta2_source.zip

With web export bug_clipboard_web_4.2.beta2_with_build.zip

alanenggb commented 1 year ago

I found a barely decent workaround using JavaScriptBridge.eval to define a clipboard-reading function (that also asks the user to grant clipboard read permission), executing it when the user press the action "ui_paste", then "listen" to a global variable with the clipboard data. There are also a few shenanigans to convert the data to a texture if anyone needs (I'm using some TextureRect's in my project).

Minimal reproduction project: bug_clipboard_web_4.2_workaround.zip

alanenggb commented 1 year ago

Related to #81252

TinyTakinTeller commented 3 months ago

I found a barely decent workaround using JavaScriptBridge.eval to define a clipboard-reading function (that also asks the user to grant clipboard read permission), executing it when the user press the action "ui_paste", then "listen" to a global variable with the clipboard data. There are also a few shenanigans to convert the data to a texture if anyone needs (I'm using some TextureRect's in my project).

Minimal reproduction project: bug_clipboard_web_4.2_workaround.zip

Thank you. Inspired by this, I implemented a workaround for my game (I needed clipboard to export and import save data, a long chunk of text). I Basically made a _await_native_js_popup_command for two-way communication via a custom javascript/html dialog document.createElement("div"); .... Related commit: (project is open source, code is MIT) https://github.com/TinyTakinTeller/GodotProjectZero/commit/3144b207a19d017addf891823b51835f0f39e8ac

image

EDIT: A bit cleaner is to export *.js files and read them into a string variable, instead of directly writing js in the variable: https://github.com/TinyTakinTeller/GodotProjectZero/commit/aa439a24e7bb306f4aa4a8c171e82ad4dde3fa6a