Skip to content

Build your personal knowledge base with Trilium Notes

fixed script support

zadam5741b380f017
diff --git a/src/public/app/services/frontend_script_api.js b/src/public/app/services/frontend_script_api.js
index b12b04382d..f7e3c07a05 100644
--- a/src/public/app/services/frontend_script_api.js
+++ b/src/public/app/services/frontend_script_api.js
@@ -176,11 +176,9 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null, $contain
* @returns {Promise<NoteShort[]>}
*/
this.searchForNotes = async searchString => {
- const noteIds = await this.runOnBackend(async searchString => {
- const notes = await api.searchForNotes(searchString);
-
- return notes.map(note => note.noteId);
- }, [searchString]);
+ const noteIds = await this.runOnBackend(
+ searchString => api.searchForNotes(searchString).map(note => note.noteId),
+ [searchString]);
return await treeCache.getNotes(noteIds);
};
diff --git a/src/routes/api/script.js b/src/routes/api/script.js
index 9c85af4309..53b6ca56c9 100644
--- a/src/routes/api/script.js
+++ b/src/routes/api/script.js
@@ -5,10 +5,18 @@ const attributeService = require('../../services/attributes');
const repository = require('../../services/repository');
const syncService = require('../../services/sync');
-function exec(req) {
+async function exec(req) {
try {
- const result = scriptService.executeScript(req.body.script, req.body.params, req.body.startNoteId,
- req.body.currentNoteId, req.body.originEntityName, req.body.originEntityId);
+ const {body} = req;
+
+ const result = await scriptService.executeScript(
+ body.script,
+ body.params,
+ body.startNoteId,
+ body.currentNoteId,
+ body.originEntityName,
+ body.originEntityId
+ );
return {
success: true,
@@ -21,10 +29,10 @@ function exec(req) {
}
}
-function run(req) {
+async function run(req) {
const note = repository.getNote(req.params.noteId);
- const result = scriptService.executeNote(note, { originEntity: note });
+ const result = await scriptService.executeNote(note, { originEntity: note });
return { executionResult: result };
}
diff --git a/src/services/script.js b/src/services/script.js
index b588fdfcdf..eb9c92f83e 100644
--- a/src/services/script.js
+++ b/src/services/script.js
@@ -34,12 +34,12 @@ async function executeBundle(bundle, apiParams = {}) {
cls.set('sourceId', 'script');
// last \r\n is necessary if script contains line comment on its last line
- const script = "function() {\r\n" + bundle.script + "\r\n}";
+ const script = "async function() {\r\n" + bundle.script + "\r\n}";
const ctx = new ScriptContext(bundle.allNotes, apiParams);
try {
- return execute(ctx, script);
+ return await execute(ctx, script);
}
catch (e) {
log.error(`Execution of script "${bundle.note.title}" (${bundle.note.noteId}) failed with error: ${e.message}`);
@@ -52,7 +52,7 @@ async function executeBundle(bundle, apiParams = {}) {
* This method preserves frontend startNode - that's why we start execution from currentNote and override
* bundle's startNote.
*/
-function executeScript(script, params, startNoteId, currentNoteId, originEntityName, originEntityId) {
+async function executeScript(script, params, startNoteId, currentNoteId, originEntityName, originEntityId) {
const startNote = repository.getNote(startNoteId);
const currentNote = repository.getNote(currentNoteId);
const originEntity = repository.getEntityFromName(originEntityName, originEntityId);
@@ -63,11 +63,11 @@ function executeScript(script, params, startNoteId, currentNoteId, originEntityN
const bundle = getScriptBundle(currentNote);
- return executeBundle(bundle, { startNote, originEntity });
+ return await executeBundle(bundle, { startNote, originEntity });
}
-function execute(ctx, script) {
- return (function() { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx));
+async function execute(ctx, script) {
+ return await (function() { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx));
}
function getParams(params) {