-
-
Notifications
You must be signed in to change notification settings - Fork 20
JS Engine
Use JS Engine to execute JavaScript with this item type.
- Install and enable the JS Engine plugin ↗ to execute JavaScript from Note Toolbar.
- Note Toolbar's Scripting option must be enabled in order to use this feature.
Tip
Example scripts that work with Note Toolbar can be found in this folder ↗
Use JavaScript expressions in labels, tooltips, and URIs, to create dynamic toolbars.
For example, set your label to {{js: return new Date().toLocaleDateString('en-US', { month: 'short', day: 'numeric' })}} to show today's date, right in your toolbar.
After creating a new JS Engine toolbar item, select from one of the below functions.
- Interprets the provided JavaScript expression (which can be multiple lines) and returns the result.
Expression example:
console.log("👋 Hello console!");
new Notice("👋 Hello notice!");- Executes the provided script, optionally putting output in the provided Output callout. File path should be relative to your vault's root.
- Uses the
internal.executeFileAPI method.
JavaScript file example:
console.log("👋 Hello console!");
new Notice("👋 Hello notice!");
return "👋 Hello note!";JavaScript file with markdown rendering:
return engine.markdown.create('*test*'); // must use Output callout ID for this to render intoThe JS Engine Markdown Builder works as well. See the JS Engine documentation ↗
If input parameters are passed to the script, such as:
param1: "value1", param2: 123...they can be accessed within the script like so:
if (input) {
({param1, param2} = input)
console.log(param1); // "value1"
}- Imports the provided JavaScript file, then executes a function within it. File path should be relative to your vault's root.
- Parameters can be passed in JSON format, e.g.,
{ "name": "Chris" }. See above for how to use them. ⚠️ Note that the JavaScript file is only imported once. You may have to restart Obsidian in order to pick up changes.- Uses the
importJsAPI method.
JavaScript file with function and arguments example:
export function Hello(engine, args) {
console.log(`👋 Hello ${args['name']}`);
new Notice(`👋 Hello ${args['name']}`);
}For Note Toolbar Callouts, use these data attributes:
| Function | data-js-engine |
data-src |
data-expr |
data-func |
data-args |
data-callout |
|---|---|---|---|---|---|---|
| Evaluate JavaScript | evaluate |
n/a | expression | n/a | n/a | callout ID (optional) |
| Execute JavaScript | exec |
JavaScript filename | n/a | n/a | n/a | callout ID (optional) |
| Import and execute JavaScript | importExec |
JavaScript filename | n/a | function name (optional) | arguments (optional) | n/a |
Example:
> [!note-toolbar] JS Engine Toolbar
> - [Hello World with JS Engine]()<data data-js-engine="exec" data-src="Scripts/JsEngine/HelloWorld.js"/>Use this to create a callout for where script output can be sent.
See Executing scripts, which also covers how errors are handled.
After a script is executed, focus is placed in the editor by default. If set to false, it won't be.
This is useful for scripts that don't have an impact on the content of your notes, such as opening a menu with the ntb.menu API.
Learn more: Executing scripts
Read next: Templater
User Guide • Gallery • Note Toolbar API • Support • Discussions ↗ • Release Notes ↗ • Roadmap
Note Toolbar by Chris Gurney • Buy me a coffee ☕️