So I wrote another plugin, Plugin REPL (download) to execute JavaScript in Obsidian. There are a few plugins that do something similar, but this is intended to let you run JavaScript, in Obsidian, once, now.
Plugin REPL is inspired by how Emacs executes elisp. Emacs allows you to highlight some text and run it - or execute the current line. I find this very straightforward to get things done.
About Running JavaScript
The “standard” ways to run JavaScript inside Obsidian seem to be Templater for one-off execution or execution at startup - possibly together with scripts or dataview. To me, script user functions in Templater seems like a bit too much work - I need to use an external editor and I have found debugging a bit difficult. The downside with dataview is that the code runs repeatedly thought you can work around this using buttons or commands
The closest analogues to Plugin REPL, seem to either the console in developer tools or obsidian-runjs . I find the developer console a bit fiddly, easy to lose your place, and with the results not saved. There is also- which is kind of similar but instead uses “named blocks” that can be run from a GUI which is too fiddly for me.
Some features that Plugin REPL has
- Run a command on a line and execute that line as javascript
- Do the same by selection regions and running a command
- Pop up a window and run some javascript. Like the developer console by a single key-press rather than a keypress and a bunch of clicking
- A bunch of convenience functions to make doing easy things in Obsidian easy. Things like: inserting, deleting, prompting for input, selecting from a list, exploring objects
- The ability to define new commands by just typing text and running it (rather than clicking through a bunch of menu)
- A startup file but hardcoded as
repl.md
to reduce clicking on settings - Very basic code blocks. This lets you directly use the Javascript DOM - which seems to not be so straight forward in dataview.
Finishing up
Anyway I hope you find this useful. I’m using this daily to automate various things inside Obsidian. I also use plugin repl for plugin development - normally together with plugin-reloader. This lets me write functions in my text editor, pull them into Obsidian (with command("plugin-reloader:my-plugin")
) and then poke at my plugin using plugin("my-plugin").blah
.
Download this if that sounds interesting!