add backend API methods to access options, attachments, revisions, etapi tokens

This commit is contained in:
zadam 2023-10-31 22:26:56 +01:00
parent de8c351e25
commit 7d80fe19e0
6 changed files with 1430 additions and 130 deletions

File diff suppressed because it is too large Load diff

View file

@ -152,7 +152,7 @@
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_backend_script_api.js.html">services/backend_script_api.js</a>, <a href="services_backend_script_api.js.html#line30">line 30</a>
<a href="services_backend_script_api.js.html">services/backend_script_api.js</a>, <a href="services_backend_script_api.js.html#line31">line 31</a>
</li></ul></dd>
@ -612,7 +612,7 @@ not for direct use.
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_backend_script_api.js.html">services/backend_script_api.js</a>, <a href="services_backend_script_api.js.html#line25">line 25</a>
<a href="services_backend_script_api.js.html">services/backend_script_api.js</a>, <a href="services_backend_script_api.js.html#line26">line 26</a>
</li></ul></dd>

View file

@ -48,6 +48,7 @@ const specialNotesService = require("./special_notes");
const branchService = require("./branches");
const exportService = require("./export/zip");
const syncMutex = require("./sync_mutex.js");
const optionsService = require("./options");
/**
@ -134,6 +135,53 @@ function BackendScriptApi(currentNote, apiParams) {
*/
this.getAttribute = attributeId => becca.getAttribute(attributeId);
/**
* @method
* @param {string} attachmentId
* @returns {BAttachment|null}
*/
this.getAttachment = attachmentId => becca.getAttachment(attachmentId);
/**
* @method
* @param {string} revisionId
* @returns {BRevision|null}
*/
this.getRevision = revisionId => becca.getRevision(revisionId);
/**
* @method
* @param {string} etapiTokenId
* @returns {BEtapiToken|null}
*/
this.getEtapiToken = etapiTokenId => becca.getEtapiToken(etapiTokenId);
/**
* @method
* @returns {BEtapiToken[]}
*/
this.getEtapiTokens = () => becca.getEtapiTokens();
/**
* @method
* @param {string} optionName
* @returns {BOption|null}
*/
this.getOption = optionName => becca.getOption(optionName);
/**
* @method
* @returns {BOption[]}
*/
this.getOptions = () => optionsService.getOptions();
/**
* @method
* @param {string} attributeId
* @returns {BAttribute|null}
*/
this.getAttribute = attributeId => becca.getAttribute(attributeId);
/**
* This is a powerful search method - you can search by attributes and their values, e.g.:
* "#dateModified =* MONTH AND #log". See {@link https://github.com/zadam/trilium/wiki/Search} for full documentation for all options
@ -402,7 +450,7 @@ function BackendScriptApi(currentNote, apiParams) {
this.getWeekNote = dateNoteService.getWeekNote;
/**
* Returns month note for given date. If such note doesn't exist, it is created.
* Returns month note for given date. If such a note doesn't exist, it is created.
*
* @method
* @param {string} date in YYYY-MM format
@ -412,7 +460,7 @@ function BackendScriptApi(currentNote, apiParams) {
this.getMonthNote = dateNoteService.getMonthNote;
/**
* Returns year note for given year. If such note doesn't exist, it is created.
* Returns year note for given year. If such a note doesn't exist, it is created.
*
* @method
* @param {string} year in YYYY format
@ -498,7 +546,7 @@ function BackendScriptApi(currentNote, apiParams) {
* @method
* @returns {{syncVersion, appVersion, buildRevision, dbVersion, dataDirectory, buildDate}|*} - object representing basic info about running Trilium version
*/
this.getAppInfo = () => appInfo
this.getAppInfo = () => appInfo;
/**
* Creates a new launcher to the launchbar. If the launcher (id) already exists, it will be updated.
@ -589,7 +637,7 @@ function BackendScriptApi(currentNote, apiParams) {
/**
* Executes given anonymous function on the frontend(s).
* Internally this serializes the anonymous function into string and sends it to frontend(s) via WebSocket.
* Internally, this serializes the anonymous function into string and sends it to frontend(s) via WebSocket.
* Note that there can be multiple connected frontend instances (e.g. in different tabs). In such case, all
* instances execute the given function.
*

View file

@ -462,7 +462,7 @@ available in the JS frontend notes. You can use e.g. <code>api.showMessage(api.s
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line372">line 372</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line409">line 409</a>
</li></ul></dd>
@ -1530,7 +1530,7 @@ See <a href="https://day.js.org">https://day.js.org</a> for documentation
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line380">line 380</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line417">line 417</a>
</li></ul></dd>
@ -1738,7 +1738,7 @@ See <a href="https://day.js.org">https://day.js.org</a> for documentation
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line546">line 546</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line583">line 583</a>
</li></ul></dd>
@ -2157,7 +2157,7 @@ See <a href="https://day.js.org">https://day.js.org</a> for documentation
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line369">line 369</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line406">line 406</a>
</li></ul></dd>
@ -2312,7 +2312,7 @@ See <a href="https://day.js.org">https://day.js.org</a> for documentation
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line283">line 283</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line320">line 320</a>
</li></ul></dd>
@ -2469,7 +2469,7 @@ See <a href="https://day.js.org">https://day.js.org</a> for documentation
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line590">line 590</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line627">line 627</a>
</li></ul></dd>
@ -2624,7 +2624,7 @@ See <a href="https://day.js.org">https://day.js.org</a> for documentation
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line582">line 582</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line619">line 619</a>
</li></ul></dd>
@ -2730,7 +2730,7 @@ See <a href="https://day.js.org">https://day.js.org</a> for documentation
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line392">line 392</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line429">line 429</a>
</li></ul></dd>
@ -2840,7 +2840,7 @@ See <a href="https://day.js.org">https://day.js.org</a> for documentation
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line426">line 426</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line463">line 463</a>
</li></ul></dd>
@ -2946,7 +2946,7 @@ See <a href="https://day.js.org">https://day.js.org</a> for documentation
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line386">line 386</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line423">line 423</a>
</li></ul></dd>
@ -3052,7 +3052,7 @@ See <a href="https://day.js.org">https://day.js.org</a> for documentation
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line441">line 441</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line478">line 478</a>
</li></ul></dd>
@ -3162,7 +3162,7 @@ See <a href="https://day.js.org">https://day.js.org</a> for documentation
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line418">line 418</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line455">line 455</a>
</li></ul></dd>
@ -3268,7 +3268,7 @@ See <a href="https://day.js.org">https://day.js.org</a> for documentation
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line398">line 398</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line435">line 435</a>
</li></ul></dd>
@ -3379,7 +3379,7 @@ implementation of actual widget type.
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line435">line 435</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line472">line 472</a>
</li></ul></dd>
@ -3534,7 +3534,7 @@ implementation of actual widget type.
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line450">line 450</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line487">line 487</a>
</li></ul></dd>
@ -3689,7 +3689,7 @@ implementation of actual widget type.
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line494">line 494</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line531">line 531</a>
</li></ul></dd>
@ -3796,7 +3796,7 @@ if some action needs to happen on only one specific instance.
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line276">line 276</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line313">line 313</a>
</li></ul></dd>
@ -3898,7 +3898,7 @@ if some action needs to happen on only one specific instance.
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line410">line 410</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line447">line 447</a>
</li></ul></dd>
@ -4057,7 +4057,7 @@ if some action needs to happen on only one specific instance.
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line512">line 512</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line549">line 549</a>
</li></ul></dd>
@ -4213,7 +4213,7 @@ if some action needs to happen on only one specific instance.
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line246">line 246</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line283">line 283</a>
</li></ul></dd>
@ -4315,7 +4315,7 @@ if some action needs to happen on only one specific instance.
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line404">line 404</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line441">line 441</a>
</li></ul></dd>
@ -4520,7 +4520,7 @@ otherwise (by e.g. createLink())
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line259">line 259</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line296">line 296</a>
</li></ul></dd>
@ -4626,7 +4626,7 @@ otherwise (by e.g. createLink())
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line485">line 485</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line522">line 522</a>
</li></ul></dd>
@ -4781,7 +4781,7 @@ otherwise (by e.g. createLink())
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line503">line 503</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line540">line 540</a>
</li></ul></dd>
@ -4936,7 +4936,7 @@ otherwise (by e.g. createLink())
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line521">line 521</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line558">line 558</a>
</li></ul></dd>
@ -5086,7 +5086,7 @@ otherwise (by e.g. createLink())
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line601">line 601</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line638">line 638</a>
</li></ul></dd>
@ -5593,7 +5593,7 @@ otherwise (by e.g. createLink())
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line290">line 290</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line327">line 327</a>
</li></ul></dd>
@ -5771,7 +5771,7 @@ otherwise (by e.g. createLink())
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line465">line 465</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line502">line 502</a>
</li></ul></dd>
@ -5945,7 +5945,7 @@ otherwise (by e.g. createLink())
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line475">line 475</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line512">line 512</a>
</li></ul></dd>
@ -6100,7 +6100,7 @@ otherwise (by e.g. createLink())
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line575">line 575</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line612">line 612</a>
</li></ul></dd>
@ -6254,7 +6254,7 @@ otherwise (by e.g. createLink())
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line566">line 566</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line603">line 603</a>
</li></ul></dd>
@ -6409,7 +6409,7 @@ otherwise (by e.g. createLink())
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line267">line 267</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line304">line 304</a>
</li></ul></dd>
@ -6445,7 +6445,7 @@ otherwise (by e.g. createLink())
<h4 class="name" id="runOnBackend"><span class="type-signature"></span>runOnBackend<span class="signature">(script, params)</span><span class="type-signature"> &rarr; {Promise.&lt;any>}</span></h4>
<h4 class="name" id="runAsyncOnBackendWithManualTransactionHandling"><span class="type-signature"></span>runAsyncOnBackendWithManualTransactionHandling<span class="signature">(func, params)</span><span class="type-signature"> &rarr; {Promise.&lt;*>}</span></h4>
@ -6455,6 +6455,11 @@ otherwise (by e.g. createLink())
<div class="description">
Executes given anonymous function on the backend.
Internally this serializes the anonymous function into string and sends it to backend via AJAX.
This function is meant for advanced needs where an async function is necessary.
In this case, the automatic request-scoped transaction management is not applied,
and you need to manually define transaction via api.transactional().
If you have a synchronous function, please use api.runOnBackend().
</div>
@ -6490,16 +6495,16 @@ Internally this serializes the anonymous function into string and sends it to ba
<tr>
<td class="name"><code>script</code></td>
<td class="name"><code>func</code></td>
<td class="type">
<span class="param-type">string</span>
<span class="param-type">function</span>
|
<span class="param-type">function</span>
<span class="param-type">string</span>
@ -6509,7 +6514,7 @@ Internally this serializes the anonymous function into string and sends it to ba
<td class="description last">script to be executed on the backend</td>
<td class="description last">(synchronous) function to be executed on the backend</td>
</tr>
@ -6522,7 +6527,7 @@ Internally this serializes the anonymous function into string and sends it to ba
<td class="type">
<span class="param-type">Array.&lt;any></span>
<span class="param-type">Array.&lt;?></span>
@ -6573,7 +6578,7 @@ Internally this serializes the anonymous function into string and sends it to ba
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line189">line 189</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line241">line 241</a>
</li></ul></dd>
@ -6613,7 +6618,195 @@ Internally this serializes the anonymous function into string and sends it to ba
</dt>
<dd>
<span class="param-type">Promise.&lt;any></span>
<span class="param-type">Promise.&lt;*></span>
</dd>
</dl>
<h4 class="name" id="runOnBackend"><span class="type-signature"></span>runOnBackend<span class="signature">(func, params)</span><span class="type-signature"> &rarr; {Promise.&lt;*>}</span></h4>
<div class="description">
Executes given anonymous function on the backend.
Internally this serializes the anonymous function into string and sends it to backend via AJAX.
Please make sure that the supplied function is synchronous. Only sync functions will work correctly
with transaction management. If you really know what you're doing, you can call api.runAsyncOnBackendWithManualTransactionHandling()
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>func</code></td>
<td class="type">
<span class="param-type">function</span>
|
<span class="param-type">string</span>
</td>
<td class="description last">(synchronous) function to be executed on the backend</td>
</tr>
<tr>
<td class="name"><code>params</code></td>
<td class="type">
<span class="param-type">Array.&lt;?></span>
</td>
<td class="description last">list of parameters to the anonymous function to be sent to backend</td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line218">line 218</a>
</li></ul></dd>
</dl>
<h5>Returns:</h5>
<div class="param-desc">
return value of the executed function on the backend
</div>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type">Promise.&lt;*></span>
</dd>
@ -6733,7 +6926,7 @@ Internally this serializes the anonymous function into string and sends it to ba
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line233">line 233</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line270">line 270</a>
</li></ul></dd>
@ -6889,7 +7082,7 @@ Internally this serializes the anonymous function into string and sends it to ba
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line221">line 221</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line258">line 258</a>
</li></ul></dd>
@ -7044,7 +7237,7 @@ Internally this serializes the anonymous function into string and sends it to ba
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line530">line 530</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line567">line 567</a>
</li></ul></dd>
@ -7195,7 +7388,7 @@ Internally this serializes the anonymous function into string and sends it to ba
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line457">line 457</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line494">line 494</a>
</li></ul></dd>
@ -7350,7 +7543,7 @@ Internally this serializes the anonymous function into string and sends it to ba
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line324">line 324</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line361">line 361</a>
</li></ul></dd>
@ -7509,7 +7702,7 @@ Internally this serializes the anonymous function into string and sends it to ba
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line306">line 306</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line343">line 343</a>
</li></ul></dd>
@ -7646,7 +7839,7 @@ Internally this serializes the anonymous function into string and sends it to ba
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line315">line 315</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line352">line 352</a>
</li></ul></dd>
@ -7801,7 +7994,7 @@ Internally this serializes the anonymous function into string and sends it to ba
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line298">line 298</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line335">line 335</a>
</li></ul></dd>
@ -8033,7 +8226,7 @@ Internally this serializes the anonymous function into string and sends it to ba
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line336">line 336</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line373">line 373</a>
</li></ul></dd>
@ -8215,7 +8408,7 @@ Internally this serializes the anonymous function into string and sends it to ba
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line345">line 345</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line382">line 382</a>
</li></ul></dd>
@ -8375,7 +8568,7 @@ Internally this serializes the anonymous function into string and sends it to ba
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line354">line 354</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line391">line 391</a>
</li></ul></dd>
@ -8467,7 +8660,7 @@ Typical use case is when a new note has been created, we should wait until it is
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line558">line 558</a>
<a href="services_frontend_script_api.js.html">services/frontend_script_api.js</a>, <a href="services_frontend_script_api.js.html#line595">line 595</a>
</li></ul></dd>

View file

@ -206,36 +206,73 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null, $contain
}
/**
* Executes given anonymous function on the backend.
* Internally this serializes the anonymous function into string and sends it to backend via AJAX.
*
* @method
* @param {string|Function} script - script to be executed on the backend
* @param {Array&lt;any>} params - list of parameters to the anonymous function to be sent to backend
* @returns {Promise&lt;any>} return value of the executed function on the backend
* @private
*/
this.runOnBackend = async (script, params = []) => {
if (typeof script === "function") {
script = script.toString();
this.__runOnBackendInner = async (func, params, transactional) => {
if (typeof func === "function") {
func = func.toString();
}
const ret = await server.post('script/exec', {
script: script,
script: func,
params: prepareParams(params),
startNoteId: startNote.noteId,
currentNoteId: currentNote.noteId,
originEntityName: "notes", // currently there's no other entity on the frontend which can trigger event
originEntityId: originEntity ? originEntity.noteId : null
originEntityId: originEntity ? originEntity.noteId : null,
transactional
}, "script");
if (ret.success) {
await ws.waitForMaxKnownEntityChangeId();
return ret.executionResult;
}
else {
} else {
throw new Error(`server error: ${ret.error}`);
}
}
/**
* Executes given anonymous function on the backend.
* Internally this serializes the anonymous function into string and sends it to backend via AJAX.
* Please make sure that the supplied function is synchronous. Only sync functions will work correctly
* with transaction management. If you really know what you're doing, you can call api.runAsyncOnBackendWithManualTransactionHandling()
*
* @method
* @param {function|string} func - (synchronous) function to be executed on the backend
* @param {Array.&lt;?>} params - list of parameters to the anonymous function to be sent to backend
* @returns {Promise&lt;*>} return value of the executed function on the backend
*/
this.runOnBackend = async (func, params = []) => {
if (func?.constructor.name === "AsyncFunction" || func?.startsWith?.("async ")) {
toastService.showError("You're passing an async function to api.runOnBackend() which will likely not work as you intended. "
+ "Either make the function synchronous (by removing 'async' keyword), or use api.runAsyncOnBackendWithManualTransactionHandling()");
}
return await this.__runOnBackendInner(func, params, true);
};
/**
* Executes given anonymous function on the backend.
* Internally this serializes the anonymous function into string and sends it to backend via AJAX.
* This function is meant for advanced needs where an async function is necessary.
* In this case, the automatic request-scoped transaction management is not applied,
* and you need to manually define transaction via api.transactional().
*
* If you have a synchronous function, please use api.runOnBackend().
*
* @method
* @param {function|string} func - (synchronous) function to be executed on the backend
* @param {Array.&lt;?>} params - list of parameters to the anonymous function to be sent to backend
* @returns {Promise&lt;*>} return value of the executed function on the backend
*/
this.runAsyncOnBackendWithManualTransactionHandling = async (func, params = []) => {
if (func?.constructor.name === "Function" || func?.startsWith?.("function")) {
toastService.showError("You're passing a synchronous function to api.runAsyncOnBackendWithManualTransactionHandling(), " +
"while you should likely use api.runOnBackend() instead.");
}
return await this.__runOnBackendInner(func, params, false);
};
/**

View file

@ -20,6 +20,7 @@ const specialNotesService = require("./special_notes");
const branchService = require("./branches");
const exportService = require("./export/zip");
const syncMutex = require("./sync_mutex.js");
const optionsService = require("./options");
/**
@ -106,6 +107,53 @@ function BackendScriptApi(currentNote, apiParams) {
*/
this.getAttribute = attributeId => becca.getAttribute(attributeId);
/**
* @method
* @param {string} attachmentId
* @returns {BAttachment|null}
*/
this.getAttachment = attachmentId => becca.getAttachment(attachmentId);
/**
* @method
* @param {string} revisionId
* @returns {BRevision|null}
*/
this.getRevision = revisionId => becca.getRevision(revisionId);
/**
* @method
* @param {string} etapiTokenId
* @returns {BEtapiToken|null}
*/
this.getEtapiToken = etapiTokenId => becca.getEtapiToken(etapiTokenId);
/**
* @method
* @returns {BEtapiToken[]}
*/
this.getEtapiTokens = () => becca.getEtapiTokens();
/**
* @method
* @param {string} optionName
* @returns {BOption|null}
*/
this.getOption = optionName => becca.getOption(optionName);
/**
* @method
* @returns {BOption[]}
*/
this.getOptions = () => optionsService.getOptions();
/**
* @method
* @param {string} attributeId
* @returns {BAttribute|null}
*/
this.getAttribute = attributeId => becca.getAttribute(attributeId);
/**
* This is a powerful search method - you can search by attributes and their values, e.g.:
* "#dateModified =* MONTH AND #log". See {@link https://github.com/zadam/trilium/wiki/Search} for full documentation for all options
@ -374,7 +422,7 @@ function BackendScriptApi(currentNote, apiParams) {
this.getWeekNote = dateNoteService.getWeekNote;
/**
* Returns month note for given date. If such note doesn't exist, it is created.
* Returns month note for given date. If such a note doesn't exist, it is created.
*
* @method
* @param {string} date in YYYY-MM format
@ -384,7 +432,7 @@ function BackendScriptApi(currentNote, apiParams) {
this.getMonthNote = dateNoteService.getMonthNote;
/**
* Returns year note for given year. If such note doesn't exist, it is created.
* Returns year note for given year. If such a note doesn't exist, it is created.
*
* @method
* @param {string} year in YYYY format
@ -470,7 +518,7 @@ function BackendScriptApi(currentNote, apiParams) {
* @method
* @returns {{syncVersion, appVersion, buildRevision, dbVersion, dataDirectory, buildDate}|*} - object representing basic info about running Trilium version
*/
this.getAppInfo = () => appInfo
this.getAppInfo = () => appInfo;
/**
* Creates a new launcher to the launchbar. If the launcher (id) already exists, it will be updated.
@ -561,7 +609,7 @@ function BackendScriptApi(currentNote, apiParams) {
/**
* Executes given anonymous function on the frontend(s).
* Internally this serializes the anonymous function into string and sends it to frontend(s) via WebSocket.
* Internally, this serializes the anonymous function into string and sends it to frontend(s) via WebSocket.
* Note that there can be multiple connected frontend instances (e.g. in different tabs). In such case, all
* instances execute the given function.
*