js: Percent-encode game IDs in URLs and decode them again on input

This is necessary to allow all random seeds to round-trip properly.
It's probably not currently necessary for descriptive game IDs, but it
won't hurt.

I've deliberately gone for encoding only those characters that are not
valid in fragment identifiers to minimise the ugliness of the generated
URLs.  For slightly interesting historical reasons, '#' is not valid in
a fragment identifier, so all random seed links end up a little bit
ugly.
This commit is contained in:
Ben Harris
2022-10-21 09:55:35 +01:00
parent 373dadacc0
commit 0197ca4359
2 changed files with 3 additions and 3 deletions

View File

@ -165,13 +165,13 @@ mergeInto(LibraryManager.library, {
* the random seed permalink. * the random seed permalink.
*/ */
js_update_permalinks: function(desc, seed) { js_update_permalinks: function(desc, seed) {
desc = UTF8ToString(desc); desc = encodeURI(UTF8ToString(desc)).replaceAll("#", "%23");
permalink_desc.href = "#" + desc; permalink_desc.href = "#" + desc;
if (seed == 0) { if (seed == 0) {
permalink_seed.style.display = "none"; permalink_seed.style.display = "none";
} else { } else {
seed = UTF8ToString(seed); seed = encodeURI(UTF8ToString(seed)).replaceAll("#", "%23");;
permalink_seed.href = "#" + seed; permalink_seed.href = "#" + seed;
permalink_seed.style.display = ""; permalink_seed.style.display = "";
} }

View File

@ -515,7 +515,7 @@ function initPuzzle() {
// Run the C setup function, passing argv[1] as the fragment // Run the C setup function, passing argv[1] as the fragment
// identifier (so that permalinks of the form puzzle.html#game-id // identifier (so that permalinks of the form puzzle.html#game-id
// can launch the specified id). // can launch the specified id).
Module.callMain([location.hash]); Module.callMain([decodeURIComponent(location.hash)]);
// And if we get here with everything having gone smoothly, i.e. // And if we get here with everything having gone smoothly, i.e.
// we haven't crashed for one reason or another during setup, then // we haven't crashed for one reason or another during setup, then