Gemini Scripting Revisited
20250709
First off, this works with any Gemini client. It's an experiment but it's also a way for someone with a capsule on a static server to add dynamic content - even games - to their pages.
How is this possible? With Scriptonite!
Scriptonite is a service allowing anyone to create a Gemini page and add a JavaScript code block:
print(new Date());
You can "run" the page by submitting it to gemini://ultimatumlabs.com/scriptonite. The server will fetch your page, resolve any relative links, run the code and then return the rendered page to your favorite client.
Once you have the code working, you can copy the url of the returned page and share it on BBS or your own capsule, etc. Here's how a Scriptonite link looks:
gemini://ultimatumlabs.com/scriptonite/encoded-link
The 'encoded-link' is a url-encoded link to a Gemini page with a script block. Simple. But wait, there's more. You can pass arguments into your script:
gemini://ultimatumlabs.com/scriptonite/encoded-link;firstName=Joe;lastName=Smith
This gives your script access to the firstName and lastName variables.
print('Why, hello there, ' + firstName + ' ' + lastName);
What if you want to prompt the user for their name? Do this instead.
gemini://ultimatumlabs.com/scriptonite/encoded-link;firstName;lastName
For variable names without a value, Scriptonite will prompt the user for each value before running the script. It's okay if some variables have values and others don't. In fact, this can be useful for maintaining state.
Examples
###Emoji Clock
Here's a page that prompts for your timezone (US/Central, UTF, etc) and locale (en-US, en-GB, etc) and returns the closest clock emoji to your current time. The first link is the unprocessed page where you can view the code. The second is Scriptonized.
You'll notice the second link prompts for timezone and locale from the page itself. It doesn't have to. Here's a link to the same page that auto-prompts for timezone and locale.
You can look at the code to see how the two situations are handled. This page also has a separate code block for the footer. Despite the fact that the original code appears in preformatted text, you can output gemtext with links, etc. If you do want preformatted text, just print('```') from your code.
Pick A Number (Game)
Here's a very simple game that maintains state. The first link is the unprocessed page.
The random number and the number of guesses are preserved in subsequent requests. There's even a bit of obfuscation so you can't see the answer in your client's address bar.
Additional Info
Scriptonite runs JavaScript in a sandboxed environment. The code can't access the network or the server file system. There are memory, instruction count and time constraints. If you bump into one of these limits, let me know and I'll look into modifying the settings. I actually don't think this will get much use but if the server gets bogged down, I may make adjustments. As mentioned, this is an experiment. If you find an issue or have an idea for an enhancement, you can find my e-mail address on ultimatumlabs.com.
Scriptonite is fully compatible with the EMCA Script 2024 Specification.
Source