Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
changes for script execution (parameters etc.)
azivner005480059ab2
diff --git a/public/javascripts/server.js b/public/javascripts/server.jsindex 95c3a404b8..796c218745 100644--- a/public/javascripts/server.js+++ b/public/javascripts/server.js@@ -29,12 +29,12 @@ const server = (function() {return await call('DELETE', url);}- async function exec(script) {+ async function exec(script, params = []) {if (typeof script === "function") {script = script.toString();}- return await post('script/exec', { script: script });+ return await post('script/exec', { script: script, params: params });}let i = 1;diff --git a/routes/api/script.js b/routes/api/script.jsindex 9bdded223d..920d7ed82d 100644--- a/routes/api/script.js+++ b/routes/api/script.js@@ -4,18 +4,14 @@ const express = require('express');const router = express.Router();const auth = require('../../services/auth');const wrap = require('express-promise-wrap').wrap;-const log = require('../../services/log');const sql = require('../../services/sql');const notes = require('../../services/notes');const protected_session = require('../../services/protected_session');const attributes = require('../../services/attributes');+const script = require('../../services/script');router.post('/exec', auth.checkApiAuth, wrap(async (req, res, next) => {- log.info('Executing script: ' + req.body.script);-- const ret = await eval("(" + req.body.script + ")()");-- log.info('Execution result: ' + ret);+ const ret = await script.executeScript(req, req.body.script, req.body.params);res.send(ret);}));diff --git a/services/attributes.js b/services/attributes.jsindex 1f1eac4553..7ae6dd4232 100644--- a/services/attributes.js+++ b/services/attributes.js@@ -3,16 +3,30 @@const sql = require('./sql');const utils = require('./utils');const sync_table = require('./sync_table');+const protected_session = require('./protected_session');async function getNoteAttributeMap(noteId) {return await sql.getMap(`SELECT name, value FROM attributes WHERE note_id = ?`, [noteId]);}async function getNoteIdWithAttribute(name, value) {- return await sql.getFirstValue(`SELECT DISTINCT notes.note_id FROM notes JOIN attributes USING(note_id)+ return await sql.getFirstValue(`SELECT notes.note_id FROM notes JOIN attributes USING(note_id)WHERE notes.is_deleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);}+async function getNoteWithAttribute(dataKey, name, value) {+ const note = await sql.getFirst(`SELECT notes.* FROM notes JOIN attributes USING(note_id)+ WHERE notes.is_deleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);++ if (!note) {+ return note;+ }++ protected_session.decryptNote(dataKey, note);++ return note;+}+async function getNoteIdsWithAttribute(name) {return await sql.getFirstColumn(`SELECT DISTINCT notes.note_id FROM notes JOIN attributes USING(note_id)WHERE notes.is_deleted = 0 AND attributes.name = ?`, [name]);@@ -37,6 +51,7 @@ async function createAttribute(noteId, name, value = null, sourceId = null) {module.exports = {getNoteAttributeMap,getNoteIdWithAttribute,+ getNoteWithAttribute,getNoteIdsWithAttribute,createAttribute};\ No newline at end of filediff --git a/services/date_notes.js b/services/date_notes.jsindex 3cecdd41e9..6e1fcffeab 100644--- a/services/date_notes.js+++ b/services/date_notes.js@@ -81,7 +81,11 @@ async function getMonthNoteId(dateTimeStr, rootNoteId) {return monthNoteId;}-async function getDateNoteId(dateTimeStr, rootNoteId) {+async function getDateNoteId(dateTimeStr, rootNoteId = null) {+ if (!rootNoteId) {+ rootNoteId = await getRootNoteId();+ }+const dateStr = dateTimeStr.substr(0, 10);const dayNumber = dateTimeStr.substr(8, 2);diff --git a/services/script.js b/services/script.jsnew file mode 100644index 0000000000..d9fa4b3f97--- /dev/null+++ b/services/script.js@@ -0,0 +1,28 @@+const log = require('./log');+const protected_session = require('./protected_session');++async function executeScript(dataKey, script, params) {+ log.info('Executing script: ' + script);++ const ctx = {+ dataKey: protected_session.getDataKey(dataKey)+ };++ params.unshift(ctx);++ const paramsStr = getParams(params);++ const ret = await eval(`(${script})(${paramsStr})`);++ log.info('Execution result: ' + ret);++ return ret;+}++function getParams(params) {+ return params.map(p => JSON.stringify(p)).join(",");+}++module.exports = {+ executeScript+};\ No newline at end of file