https://github.com/meain/logseq-plugin-runjs
Run arbitrary JS in your LogSeq pages
https://github.com/meain/logseq-plugin-runjs
logseq-plugin
Last synced: 11 months ago
JSON representation
Run arbitrary JS in your LogSeq pages
- Host: GitHub
- URL: https://github.com/meain/logseq-plugin-runjs
- Owner: meain
- License: apache-2.0
- Created: 2023-10-13T19:20:38.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-14T05:14:52.000Z (over 2 years ago)
- Last Synced: 2025-03-19T02:22:43.674Z (11 months ago)
- Topics: logseq-plugin
- Language: TypeScript
- Homepage:
- Size: 43.9 KB
- Stars: 9
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# Logseq Run JS
Run arbitrary javascript and show output on a page.
[](https://github.com/meain/logseq-plugin-runjs/assets/14259816/92d257e8-d99e-4742-ac73-d7d262473be5)
*If you are having trouble viewing the video on GitHub, you can watch it on [YouTube](https://youtu.be/3nKkpMuS4gw).*
> [!WARNING]
> Under the hood, it just does an `eval` of the javascript. So be careful with what you run.
## Usage
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.
``` javascript
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.
``` javascript
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](https://plugins-doc.logseq.com/).
``` javascript
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.
``` javascript
fetch("https://wttr.in/?format=%t")
.then((resp) => resp.text())
.then((data) => setOutput("Current temperature: " + data));
```
### TODOs
- [ ] 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 use `setOutput`)
- [ ] Option for output that can be continuously updated
---
### Thanks
- Took a lot of inspiration from [xyhp915/logseq-fenced-code-plus](https://github.com/xyhp915/logseq-fenced-code-plus)