[WIP] Brew Scripts, a new tab for scripts that is specifically targeted to manipulating CSVs and modifying brew text#4881
Conversation
First checkin with a functioning user scripts. Lives very close to snippets. Allows executing scripts (plain javascript) with a limited api to manipulate the brew text. Useful for doing things such as pulling from a csv. Various API functions are stubbed out.
Instead of working through a chain of React props, now talks directly to the editor. And the API itself lives in its own file, to be converted to a React Class later (with the dangling snippetUploadFile, which is still to be extracted).
Prevents onload script attacks and has a nice benefit of only seeing parse errors on execute. Error is wrapped in try-catch so that we can override it later.
…te list File uploading is the most awkward part of this, so feeding it through a dialog for now.
…to be too destructive for its own good.
… much erroneous stuff ends up in the yawml
…portError and doReplaceSelected
…ze these functions
… matches the order in the API
… make args validation clearer
…persist across tabs
… out a little more difficult
Don't let script requests to sit and block the termination of a worker. Script requests will go away after 60 seconds and prevent further requests. Entire worker goes away 10 seconds later (preventing case where a user late clicks the request from not having enough time to actually process). Errors are still persisted even after the worker dies.
…s source policy. This also requires splitting scriptWorker and scriptEditor so that we can reuse the worker.
…test of the worker
This still leaves todo for some actual validation
Especially around error validation. This also lead to the validator getting simplified a bit.
|
Just as a note so we can be clear in our expectations: as was discussed in the Gitter chat, I think it will be highly unlikely that the Homebrewery will ever support user-authored scripting. Even with sandboxing, this introduces a large and ongoing security surface for a very niche workflow, and it pushes the project outside its intended scope. The Homebrewery is deliberately a Markdown‑to‑HTML tool, not a data‑processing or templating engine, and features like CSV manipulation or custom script execution tread into a territory that feels like it would be better suited to an external tool (or a specialized fork). For example there are other sites out there that read from JSON input for D&D Monsters (from the SRD, etc.), have a GUI to customize the values, and then emit Homebrewery-compatible Markdown. We just have to be somewhat selective in accepting new features into the Homebrewery itself if we want to keep the it focused and purposeful about what it provides. If there is a specific use case for importing CSVs that you have in mind, it would be helpful to open an issue describing that root question so we can evaluate whether there’s a simpler, non‑scripting solution that fits the project’s goals, and have a record of any decisions. |
|
While I am not in favor of this feature, if integrated, the scripts should be part of the Brew Snippets structure as to properly mimic the Static Theme snippets, |
Indeed. Though some of the built in snippets only insert static text, some go further and insert the results of running a script (e.g. TOC). |
Would you be able to clarify what you mean by this? Do you mean specifically for the UI, for the Editor page, and/or for how the brew is stored (i.e. the yaml file)? Or something else? |
Description
Adds a new feature that allows user to author small scripts that can modify their brew.
The primary functionality for this is CSV manipulation and templating from it.
How to use
There's a new tab that lives near snippets with editable javascript.

Names for scripts must appear after \script
Everything on other lines is evaluated as javascript.
Once a script is authored, it's available in a menu near snippets. This is currently stuffed into a larger header called "This Brew" (this is intended to be changed later).

Clicking this runs the script!

get functions in the API rely on a Promise-like interface. They also require a more quarantined set of interactions for certain get functions:


In the specific case above, the csv was a large math scratch sheet. The user could of manipulated this into place instead.

Errors are displayed over the editor. This is especially important for the sanitized api functions, which aim to prevent data injection. For example, if we tried to send the JSON block as is instead of as a string, we would've an invalid type error:

Significant Technical Details
The concept of scripts are fraught with, extremely valid, worries of XSS. Much of the implementation nuance was spent in providing multiple layers of security against that. In order:
API Functions
Known Development TODO