godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.16k stars 97 forks source link

Add a method to get the URL's query parameters and/or fragment in HTML5 #3113

Open Calinou opened 3 years ago

Calinou commented 3 years ago

Describe the project you are working on

The Godot editor :slightly_smiling_face:

Describe the problem or limitation you are having in your project

Users are frequently struggling with accessing query (GET) parameters from their exported project: https://godotforums.org/discussion/27136/pass-input-parameter-to-web-based-game#latest

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add a method that can be used to read query parameters in HTML5.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

This could be done by exposing query parameters as a string when calling OS.get_cmdline_args() in HTML5, or (likely better) by adding a dedicated method that handles the splitting for you (e.g. with a Dictionary return value). On other platforms, the new method would return an empty Dictionary.

We may also want to add another method to access the URL fragment (what's placed after #). This would likely be returned as a single String. Depending on your use case, a fragment may be a better idea than using a query parameter, since the fragment isn't sent to the server. This is better for user privacy, but sometimes, the server has to know the value of the query parameter (e.g. to handle it in a PHP script used on the same page).

If this enhancement will not be used often, can it be worked around with a few lines of script?

Maybe, but since a lot of people are having trouble with this, I figure this should be a built-in feature.

Is there a reason why this should be core and not an add-on in the asset library?

See above.

foxydevloper commented 3 years ago

Query parameters can have multiple entries with the same key. I think a dictionary would be a bad way to access query parameters. I think it would be better to have an API exposed similar to JavaScript's URLSearchParams where you explicitly ask for the parameters such as url.get('myparam') and url.get_list('myparam') The equivalent code in GDScript would be: JavaScript.eval("new URLSearchParams(window.location.search).get('myparam')") JavaScript.eval("new URLSearchParams(window.location.search).getAll('myparam')")

foxydevloper commented 3 years ago

OS.get_cmdline_args() and query parameters should stay separate. You can currently already pass in "command line args" to your Godot game in HTML5 using the args property in EngineConfig that's passed in when doing Engine.startGame(config) in JavaScript. https://docs.godotengine.org/en/stable/tutorials/platform/html5_shell_classref.html#args

IceflowRE commented 2 years ago

The equivalent code in GDScript would be:

The equivalent code in GDScript would be: JavaScript.eval("new URLSearchParams(window.location.search).get('myparam')") JavaScript.eval("new URLSearchParams(window.location.search).getAll('myparam')")

You might consider JavaScript.eval("new URL(window.location.href).searchParams.get('myparam')")

chaojian-zhang commented 6 months ago

Seems the posted link (https://godotforums.org/discussion/27136/pass-input-parameter-to-web-based-game#latest) is no longer valid.