Run arbitrary javascript and show output on a page.
4fbb3f73-7289-40f1-bf2f-d4812ffd2e4b.webm
If you are having trouble viewing the video on GitHub, you can watch it on YouTube.
Warning
Under the hood, it just does an eval
of the javascript. So be careful with what you run.
Add a runjs
block to a page and write your javascript inside it. To
set the content of the block, you can call setOutput
function with
the output string as the argument.
setOutput("Hello LogSeq!")
Here is an example use case. You can use the below code snippet to fetch and show a random quote. This can maybe go into the daily page template so that you have a new quote in your journal.
fetch("https://api.quotable.io/random")
.then((response) => response.json())
.then((data) => {
setOutput(data.content + "\n -" + data.author);
});
You can make use of all the things you know and love from js and
browsers here, as I am just doing an eval
of the javascript within
the plugin.
Here is an example of using LogSeq graph data. You can learn more about it here.
logseq.Editor.getCurrentPage().then((page) => {
setOutput(page.originalName + "\nLast updated: " + new Date(page.updatedAt).toDateString());
});
Or you can use it to show the current weather.
fetch("https://wttr.in/?format=%t")
.then((resp) => resp.text())
.then((data) => setOutput("Current temperature: " + data));
- Support of outputting more than just text
- Persisting output of a run across page loads
- Should we drop
Processing...
? (maybe users don't want to usesetOutput
) - Option for output that can be continuously updated
-
Took a lot of inspiration from xyhp915/logseq-fenced-code-plus