Bug: In JavaScript objects get printed as [object Object] rather than their actual value.
However this is likely a limitation of the results output being limited to one line - a workaround is that instead of returning the object directly you can stringify it instead e.g. return JSON.stringify(myObject);
The potential for this is enormous, but I found very few examples and practical use cases here or elsewhere… Is there a thread somewhere (or should there be)?
More specifically what I’m looking for are ways in which people have been using JS within logseq to implement new ways to view, create and manipulate their knowledge base (blocks, pages, properties etc.).
I understand. I wasn’t really expecting code… Just short descriptions (maybe with a couple of screenshots), to have a feel for what people have been doing!
I am also interested in seeing other examples, so I will start. (I’m unsure if this is starting a subtopic, please spin it off if so).
I use kits to create “functions” as templates. To make them more usable, I specify what is input at the beginning. It seems like a workaround for which there is likely a better option, but I do not know better.
For instance, one that may be of broader user:
/* INPUT */
let present = 5000, intrate = 5, n = 1, depo = 200, ndepo = 12, time = 8;
let type = 0; // 0 = ordinary, 1 = due
/* METHOD */
let perrate = intrate / 100 / n;
let perdepo = depo * ndepo;
let USD = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD',});
let fut = present * (Math.pow((1 + perrate), n * time));
let futper = perdepo * (Math.pow((1 + perrate), n * time) - 1) / perrate * (1 + perrate * type);
let future = fut + futper;
/* OUTPUT */
return("FV of Principal = " + USD.format(fut) + "\n"
+ "FV of Periodic Deposits = " + USD.format(futper) + "\n"
+ "FV = " + USD.format(future));
If you would like us to tailor something more attractive to other users, open a new thread and provide a detailed description of how this is supposed to be used.
Starting from the desired usage, we can work back to a preferable approach that is easier to be adopted and customized.
It works great! (once I started to get the hang of it ) Thank you for the awesome work!
The only thing I find a bit inconvenient is that every macro needs it’s own page. If I understand it right, they cannot even have their own namespace (like kits/macro1), because the entry function needs to have the same name as the page name, right? That means a lot of extra pages on my graph if I want many small macros.
Or maybe I am missing something, or understanding something wrong?
Thank you again for the great work!
Also thanks to @adxsoft for the demo graph (super helpful!) and to @dannylin108 for the question below which saved me quite a bit of headache )
I’ve noticed that from time to time, the buttons I have defined using your suggested code stop working. This seems to happen more often on my iPhone than on my Mac, but it does happen on both.
It only happens in an already-running Logseq instance, usually after it’s been running for awhile, and after I’ve been doing other things on the device. Restarting Logseq always fixes the problem. Any idea what may be causing this, or how to diagnose and/or fix it?
Thanks. I was about to update my question, because I realized that I’m having two separate issues.
On the Mac, it’s this one, specifically related to an updated version of my query and your MassDelete script. My other button continues to always work on the Mac.
So the issue with buttons in general stopping working is so far limited to the iPhone.
I’ll check this next time it happens on the iPhone
Not in the same page.
No
No (now that we’re just talking about the iPhone)
I’ll keep my eye out for anything reportable. There’s no way I know of to access the console on the iPhone, so options are limited.
Is there a way to embed one of these macro buttons within HTML and have it run the JS on another page when clicked or create a slash command that can run the JS on another page?
When I paste this button tag within LogSeq, it runs when the button is rendered but not when I click on it. Is there a way to have it run only when clicked? <button class='kit run' data-kit='JS/Hello World'>Click Me</button>
Edit: The below button setup only runs when clicked. <button class='kit run' data-kit='runpage' data-page-name='JS/Hello World'>Hello World</button>
I’m still curious if there’s a way to run a page using a slash command instead of needing to click a button
Using {{runpage JS/Hello World}} is a decent solution for what I’ve been doing. Many times, it requires accessing the “current block”, so using the console isn’t ideal - console also isn’t supported on mobile.
I was curious if there was a way to register a slash command through logseq’s native API (which is different from the plugin API) to run a JS page. Having a slash command reduces the steps from adding the button and clicking on it to running the slash command.
Consider implementing a keyboard listener (e.g. like this one) that makes the above call, potentially reading any arguments (e.g. page name) from the textarea.